【问题标题】:Behat/Mink retrieve input type file nameBehat/Mink 检索输入类型文件名
【发布时间】:2016-08-30 19:33:06
【问题描述】:

你知道如何从 Behat/Mink/Element/NodeElement 中检索输入类型的文件名吗?

我有一个简单的测试html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="file" id="file"/><br/>
        </form>
    </body>
</html>

假设我附加了一个名为 goodCoverArt.jpg 的文件,我可以从 chrome 控制台使用以下命令检索它的文件名:

$x('//input[@type="file"]')[0].files[0].name
"goodCoverArt.jpg"

但如果我在上下文类中使用以下函数:

// Search for that file name label in the page to confirm the execution of the file attachment.
$uploadElement = $this->getElementWhenVisible('xpath', "//div[@id='filename' and contains(.,'$fileName')]");

 /**
 * @param string $selector xpath|css|...
 * @param string $locator link id, title, text or image alt
 * @param int $tries
 *
 * @return NodeElement|mixed|null $element
 */
public function getElementWhenVisible($selector, $locator, $tries = 100)
{
    $element = null;
    $this->spin(
        function () use ($selector, $locator, &$element) {

            $element = $this->find($selector, $locator);
            if(!($element instanceof NodeElement) || !$element->isVisible())
            {
                throw new Exception("Element not visible: $selector $locator");
            }
        }, $tries);

    return $element;
}

如何从 $uploadElement 检索相同的信息?

【问题讨论】:

    标签: file behat mink


    【解决方案1】:

    显然我找到了答案: 拥有 Behat/Mink/Element/NodeElement 可以调用 getValue,如下所示:

    $fileName = $uploadElement->getValue();
    echo $fileName; // This will print string: goodCoverArt.jpg
    

    将返回附件名goodCoverArt.jpg

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      相关资源
      最近更新 更多