behat / mink打开窗口并截取它的屏幕截图

时间:2017-06-14 13:50:19

标签: symfony phantomjs behat mink

我在Behat上运行测试,点击一个按钮打开另一个页面,我想与该页面进行交互(一个截图开始)。

我试过这样的事情

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"
   }

如何更改窗口?

感谢

(在Symfony 2.8上工作)

1 个答案:

答案 0 :(得分:0)

要切换到新窗口,您可以使用以下内容:

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