【问题标题】:How can I write a Behat step that will capture a screenshot or HTML page?如何编写将捕获屏幕截图或 HTML 页面的 Behat 步骤?
【发布时间】:2014-05-03 01:06:33
【问题描述】:

理想情况下,还可以在图像查看器中自动打开屏幕截图。

【问题讨论】:

    标签: php selenium screenshot behat goutte


    【解决方案1】:

    编辑:感谢 bishop 关于打开应用程序的提示。不过,仍然不确定如何在 Windows 上执行此操作。

    /**
     * This works for Selenium and other real browsers that support screenshots.
     *
     * @Then /^show me a screenshot$/
     */
    public function show_me_a_screenshot() {
    
        $image_data = $this->getSession()->getDriver()->getScreenshot();
        $file_and_path = '/tmp/behat_screenshot.jpg';
        file_put_contents($file_and_path, $image_data);
    
        if (PHP_OS === "Darwin" && PHP_SAPI === "cli") {
            exec('open -a "Preview.app" ' . $file_and_path);
        }
    
    }
    
    /**
     * This works for the Goutte driver and I assume other HTML-only ones.
     *
     * @Then /^show me the HTML page$/
     */
    public function show_me_the_html_page_in_the_browser() {
    
        $html_data = $this->getSession()->getDriver()->getContent();
        $file_and_path = '/tmp/behat_page.html';
        file_put_contents($file_and_path, $html_data);
    
        if (PHP_OS === "Darwin" && PHP_SAPI === "cli") {
            exec('open -a "Safari.app" ' . $file_and_path);
        };
    }
    

    【讨论】:

    • Example of opening in previewer on a Mac。我想该模式可以扩展到 Windows、Linux 等。就我个人而言,我只是将它们转储到一个目录中,然后使用文件系统浏览器打开我想要的那些。
    • @bishop 工作得很好 - 用代码编辑。谢谢!
    【解决方案2】:

    你可以在这里使用这个包https://github.com/forceedge01/behat-fail-aid。这将为您提供失败的屏幕截图等等。它将所有细节注入到现有的异常消息中,因此它将完美地置于失败之下,就像通常的 behat 失败消息一样。最重要的是,您不必编写和维护任何代码,而且它适用于 javascript 和非 javascript 驱动程序!下面是输出的样子:

    你所要做的就是通过 composer 安装包

    $ composer 需要 genesis/behat-fail-aid --dev

    并将上下文添加到您的 behat.yml 文件中

    behat.yml

    default:
      suites:
        default:
          contexts:
            - FailAid\Context\FailureContext
      extensions:
        FailAid\Extension: ~
    

    你可以用这个包做很多其他的事情,但这应该可以解决你的问题,祝测试愉快!

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多