2016-08-30 10 views
0

Behat/Mink/Element/NodeElementから入力タイプのファイル名を取得する方法を知っていますか?Behat/Minkの入力タイプのファイル名を取得する

私は簡単なテストのHTML私はgoodCoverArt.jpgと呼ばれるファイルを添付仮定し

<!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> 

を持って、私はそれがクロムのコンソールから次のコマンドを実行して、ファイル名だ取り出すことができます。

$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から同じ情報を取得するにはどうすればよいですか? が、次のようにのgetValueを呼び出すことが可能であるBehat /ミンク/エレメント/ NodeElementを持つ:

答えて

0

は、どうやら私は答えを見つけた

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

は、添付ファイル名goodCoverArtを返します。 jpg

関連する問題