0

Do you know how to retrieve an input type file name from a Behat/Mink/Element/NodeElement ?

I have a simple test 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>

Assuming I attach a file called goodCoverArt.jpg, I can retrieve it's filename with the following command from the chrome console:

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

But if I use the following function in a context class:

// 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;
}

How can I retrieve the same information from $uploadElement ?

Nick Realdini
  • 67
  • 2
  • 9

1 Answers1

0

Apparently I found the answer: Having a Behat/Mink/Element/NodeElement it is possible to call the getValue as follow:

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

Will return the attached file name goodCoverArt.jpg

Nick Realdini
  • 67
  • 2
  • 9