【问题标题】:PHPUnit Selenium captureScreenshotOnFailure does not work?PHPUnit Selenium captureScreenshotOnFailure 不起作用?
【发布时间】:2011-02-20 06:45:53
【问题描述】:

我正在使用 PHPUnit 3.4.12 来驱动我的 selenium 测试。我希望能够在测试失败时自动截取屏幕截图。这应该得到支持,如http://www.phpunit.de/manual/current/en/selenium.html#selenium.seleniumtestcase.examples.WebTest2.php

所述
class WebTest 
{
    protected $captureScreenshotOnFailure = true;
    protected $screenshotPath = 'C:\selenium';
    protected $screnshotUrl = 'http://localhost/screenshots';

    public function testLandingPage($selenium)
    {
            $selenium->open("http://www.example.com");
            $selenium->fail("fail");
            ...
    }
}

如您所见,我正在使测试失败,理论上当它失败时,它应该截取屏幕截图并将其放在 C:\selenium 中,因为我在 Windows 上运行 selenium RC 服务器。

但是,当我运行测试时,它只会给我以下信息:

[root@testbox selenium]$ sh run
PHPUnit 3.4.12 by Sebastian Bergmann.

F

Time: 8 seconds, Memory: 5.50Mb

There was 1 failure:

1) WebTest::testLandingPage
fail

/home/root/selenium/WebTest.php:32

FAILURES!
Tests: 1, Assertions: 0, Failures: 1.

我在 C:\selenium 中看不到任何屏幕截图。但是,我可以使用 $selenium->captureScreenshot("C:/selenium/image.png");

欢迎任何想法或建议。

谢谢

【问题讨论】:

  • 怀疑这是问题的原因,但您的 $screnshotUrl 变量应该是 $screenshotUrl 吗?
  • @Dave Hunt:说得好,是的,这确实可能是原因,因为必须填写 $this->screenshotUrl 才能触发捕获。

标签: selenium phpunit selenium-rc screen-capture


【解决方案1】:

phpunit 的错误处理相当糟糕;如果一切都不完美,它会默默地忽略您的其他选项而不会发出警告。

正如 Dave 所提到的,如果任何变量拼写错误,它都不会正常工作,您也可以尝试将它们分配给 setUp 中的实例。

此外,并非每个条件都会触发屏幕截图。尝试使用 $selenium->assertTextPresent("foobarbaz") 而不是 $selenium->fail() 进行完整性检查。

【讨论】:

  • 谢谢大家。我已经弄清楚为什么这不起作用。 1) screnshotUrl vs screenshotUrl 有一个错字 2) $selenium->fail 不会触发截图,断言函数会在失败时触发 3) 我的 tearDown 函数似乎与 phpunit3.4 不兼容,见@ 987654321@ 我必须删除它,直到该问题得到解决。另外,有谁知道我是否可以指定屏幕截图的文件名?谢谢大家
【解决方案2】:

您可以尝试添加这些代码行

尝试 { $this->assertTrue($this->isTextPresent("You searched for \"Brakes\" (2 matches)")); } 捕捉(PHPUnit_Framework_AssertionFailedError $e){ array_push($this->verificationErrors, $e->toString()); $this->drivers[0]->captureEntirePageScreenshot($this->screenshotPath . DIRECTORY_SEPARATOR . rawurlencode($this->getLocation()) . '.png'); }

【讨论】:

    【解决方案3】:

    我最近遇到了这个错误,因为我正在学习教程。

    文档中的第一个示例是针对PHPUnit_Extensions_Selenium2TestCase。页面上的所有其他人都是PHPUnit_Extensions_SeleniumTestCase

    也许会改变

    extends PHPUnit_Extensions_Selenium2TestCase
    

    extends PHPUnit_Extensions_SeleniumTestCase
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-05
      • 2017-05-16
      • 2013-11-05
      • 1970-01-01
      • 2020-01-05
      • 2023-03-10
      • 2013-02-16
      • 2013-10-29
      相关资源
      最近更新 更多