如果Selenium服务器未运行,如何跳过PHPUnit中的测试?

如果Selenium服务器未运行,如何跳过PHPUnit中的测试?,selenium,phpunit,Selenium,Phpunit,我想添加一套Selenium测试,作为应用程序全局PHPUnit测试套件的一部分。我已经将Selenium测试套件连接到了globalAllTests.php文件中,当Selenium服务器运行时,一切都正常运行 但是,如果Selenium服务器没有运行,我希望脚本跳过Selenium测试,这样其他开发人员就不会为了运行测试而被迫安装Selenium服务器。我通常会尝试在每个testcase的设置方法中进行连接,如果失败,则将测试标记为跳过,但这似乎会引发RuntimeException,并显示

我想添加一套Selenium测试,作为应用程序全局PHPUnit测试套件的一部分。我已经将Selenium测试套件连接到了global
AllTests.php
文件中,当Selenium服务器运行时,一切都正常运行

但是,如果Selenium服务器没有运行,我希望脚本跳过Selenium测试,这样其他开发人员就不会为了运行测试而被迫安装Selenium服务器。我通常会尝试在每个testcase的
设置
方法中进行连接,如果失败,则将测试标记为跳过,但这似乎会引发RuntimeException,并显示以下消息:

来自Selenium RC服务器的响应无效:错误服务器异常:sessionId不应为null;此会话是否已启动?

是否有人有方法将硒测试标记为在本场景中跳过的测试?

您可以使用PHPUnit 3.4中介绍的方法

基本上

  • 编写一个测试,检查Selenium是否启动
  • 如果没有,请调用$this->markTestAsSkipped()
  • 使所有需要硒的测试都依赖于此测试

  • 我首选的selenium/PHPUnit配置:

    维护集成(selenium)测试可能需要很多工作。我使用FirefoxSeleniumIDE开发测试用例,它不支持将测试套件导出到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!
    } ?>
    

    您可以尝试
    skipWithNoServerRunning()

    有关更多信息,请参见

    我使用的是PHPUnit 3.7.x,此方法称为markTestSkipped($optionalMessage),不包括“as”。