4

我想添加一套 Selenium 测试作为应用程序的全局 PHPUnit 测试套件的一部分。我已将 Selenium 测试套件连接到全局AllTests.php文件中,并且在 Selenium 服务器运行时一切都运行良好。

但是,如果 Selenium 服务器没有运行,我希望脚本跳过 Selnium 测试,这样其他开发人员就不必为了运行测试而强制安装 Selenium 服务器。我通常会尝试在setUp每个测试用例的方法中进行连接,如果失败,则将测试标记为已跳过,但这似乎会抛出带有消息的 RuntimeException:

The response from the Selenium RC server is invalid: ERROR Server Exception: sessionId should not be null; has this session been started yet?

有没有人有一种方法可以在这种情况下将 Selenium 测试标记为已跳过?

4

3 回答 3

2

您可以使用PHPUnit 3.4 中引入的测试依赖项。

基本上

  1. 编写一个测试来检查 Selenium 是否启动。
  2. 如果没有,请调用 $this->markTestAsSkipped()。
  3. 让所有需要硒的测试都依赖于这个。
于 2009-10-23T19:56:25.743 回答
0

我首选的 selenium / PHPUnit 配置:

维护集成(硒)测试可能需要做很多工作。我使用firefox selenium IDE开发测试用例,不支持将测试套件导出到PHPUnit,只支持个别测试用例。因此 - 如果我不得不维护 5 个测试,那么每次需要更新它们时都需要大量手动工作来重新 PHPUnit。这就是我设置 PHPUnit 以使用 Selenium IDE 的 HTML 测试文件的原因!它们可以在 PHPUnit 和 selenium IDE 之间重新加载和重用

<?php 
class RunSeleniumTests extends PHPUnit_Extensions_SeleniumTestCase {
    protected $captureScreenshotOnFailure = true;
    protected $screenshotPath = 'build/screenshots';
    protected $screenshotUrl = "http://localhost/site-under-test/build/screenshots";
    //This is where the magic happens! PHPUnit will parse all "selenese" *.html files
    public static $seleneseDirectory = 'tests/selenium';
    protected function setUp() {
            parent::setUp();
            $selenium_running = false;
            $fp = @fsockopen('localhost', 4444);
            if ($fp !== false) {
                    $selenium_running = true;
                    fclose($fp);
            }
            if (! $selenium_running)
                $this->markTestSkipped('Please start selenium server');

            //OK to run tests
            $this->setBrowser("*firefox");
    $this->setBrowserUrl("http://localhost/");
    $this->setSpeed(0);
    $this->start();
            //Setup each test case to be logged into WordPress
            $this->open('/site-under-test/wp-login.php');
            $this->type('id=user_login', 'admin');
            $this->type('id=user_pass', '1234');
            $this->click('id=wp-submit');
            $this->waitForPageToLoad();
    }
    //No need to write separate tests here - PHPUnit runs them all from the Selenese files stored in the $seleneseDirectory above!
} ?>
于 2013-02-26T19:06:28.277 回答
0

您可以尝试skipWithNoServerRunning() 有关更多信息,请点击此链接

于 2014-03-21T02:00:05.793 回答