2017-06-14 85 views
0

我正在Behat上运行一个测试,点击一个打开另一个页面的按钮,我想与该页面进行交互(一开始为屏幕截图)。behat/mink打开窗口并截取它的屏幕截图

我想是这样的

public function iTakeAScreenshotOfTheNewWindow($filename) 
{ 
    $windowNames = $this->getSession()->getWindowNames(); 
    $driver = $this->getSession()->getDriver(); 
    if(count($windowNames) == 1) { 
     throw new \Behat\Mink\Exception\ExpectationException("No extra window found", $driver); 
    } 

    $lastWindow = array_pop($windowNames); 

    $driver->switchToWindow($lastWindow); 

    if (!$driver instanceof PhantomJSDriver) { 
     throw new UnsupportedDriverActionException('This step is only supported by the PhantomJS driver', $driver); 
    } 

    $screenshot = $driver->getScreenshot(); 
    file_put_contents($filename, $screenshot); 

    $this->getSession()->switchToWindow(); //back to main window 

} 

但我得到的错误

Could not find window handle by a given window name: 1 (Behat\Mink\Exception\DriverException) 

之前,我点击按钮(并打开新的窗口),$ windowNames的vardump显示:

array(1) { 
    [0]=> 
    string(1) "0" 
    } 

窗口打开后:

array(2) { 
    [0]=> 
    string(1) "0" 
    [1]=> 
    string(1) "1" 
    } 

如何更改窗口?

感谢

回答

0

(上Symfony的2.8工作)切换到新的窗口,你可以使用这样的事情:

public function switchToNewWindow() 
    { 
     $wdSession = $this->getSession()->getDriver()->getWebDriverSession(); 
     $windows = $wdSession->window_handles(); 
     $this->getSession()->getDriver()->switchToWindow(array_pop($windows)); 
    }
+0

的方法不存在:调用未定义的方法,尊巴舞\ Mink \ Driver \ PhantomJSDriver :: getWebDriverSession() – Jon

+0

将此添加到扩展MinkContext的类中。 – lauda

+0

这就是我添加它的地方:)但是我收到了错误信息 – Jon