2009-09-13 58 views
4

我想添加一套Selenium测试作为应用程序的全局PHPUnit测试套件的一部分。我已将Selenium测试套件连接到全球AllTests.php文件中,并且在Selenium服务器运行时一切正常。如果Selenium服务器未运行,如何跳过PHPUnit中的测试?

但是,如果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测试标记为跳过的方法?

回答

2

您可以使用在PHPUnit 3.4中引入的test dependencies

基本上

  1. 写一个测试,检查是否硒高达。
  2. 如果没有,请调用$ this-> markTestAsSkipped()。
  3. 使所有的硒需要测试取决于这一个。
+0

我使用PHPUnit的3.7.x,这种方法被称为markTestSkipped($ optionalMessage ),不包括“as”。 – 2013-08-27 11:40:55

0

我首选的硒/ PHPUnit的配置:

维护集成(硒)的测试可以是大量的工作。我使用firefox selenium IDE来开发测试用例,它不支持将测试套件导出到PHPUnit,并且仅支持单个测试用例。因此,如果我不得不维护5次测试,那么每次需要更新时都需要大量的手动工作来重新PHPUnit。 这就是为什么我设置PHPUnit来使用Selenium IDE的HTML测试文件!他们可以重新加载& PHPUnit的&硒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! 
} ?> 
0

之间重用你可以尝试skipWithNoServerRunning() 欲了解更多信息,请按照this link

相关问题