【问题标题】:How do I run a PHPUnit Selenium test case without having the browser open with each function?如何在不使用每个函数打开浏览器的情况下运行 PHPUnit Selenium 测试用例?
【发布时间】:2012-08-10 05:05:01
【问题描述】:

目前,我有一个扩展 PHPUnit_Extensions_SeleniumTestCase 的 PHPUnit 测试用例。每个启动的函数都需要一个 $this->setBrowserUrl() 并且默认为每个函数调用启动一个新的 Firefox 浏览器窗口。

我想要一个测试用例,针对特定功能启动浏览器,但不针对其他功能启动浏览器,以节省打开和关闭浏览器所需的资源和时间。我可以有这样的文件吗?

【问题讨论】:

    标签: selenium phpunit


    【解决方案1】:

    您最好的选择可能是创建两个单独的测试套件,一个使用 Selenium 命令,另一个不使用任何 Selenium 功能..

    class BrowserTests extends PHPUnit_Extensions_SeleniumTestCase
    {
        protected function setUp()
        {
            $this->setBrowser('*firefox /usr/lib/firefox/firefox-bin');
            ...    
        }
    
        public function testOne()
        {
              ...
        }
        ...
    }
    

    class NonBrowsterTests extends PHPUnit_Framework_TestCase
    {
        protected function setUp()
        {
            ...
        }
    
        public function testOne
        {
           ...
        }
        ...
    }
    

    【讨论】:

      【解决方案2】:

      想出一个使用 PHPUnit 注释的自定义解决方案(并写了一篇关于它的博客文章!)

      http://blog.behance.net/dev/custom-phpunit-annotations

      编辑:在此处添加一些代码,以使我的答案更完整:)

      简而言之,使用自定义注释。在您的 setUp() 中,解析 doc 块以获取注释,并标记具有不同质量的测试。这将允许您标记某些测试以使用浏览器运行,而某些测试则无需运行。

      protected function setUp() {
      
        $class      = get_class( $this );
        $method     = $this->getName();
        $reflection = new ReflectionMethod( $class, $method );
        $doc_block  = $reflection->getDocComment();
      
        // Use regex to parse the doc_block for a specific annotation
        $browser = self::parseDocBlock( $doc_block, '@browser' );
      
        if ( !self::isBrowser( $browser )
          return false;
      
        // Start Selenium with the specified browser
      
      } // setup
      
      private static function parseDocBlock( $doc_block, $tag ) {
      
       $matches = array();
      
        if ( empty( $doc_block ) )
          return $matches;
      
        $regex = "/{$tag} (.*)(\\r\\n|\\r|\\n)/U";
        preg_match_all( $regex, $doc_block, $matches );
      
        if ( empty( $matches[1] ) )
          return array();
      
        // Removed extra index
        $matches = $matches[1];
      
        // Trim the results, array item by array item
        foreach ( $matches as $ix => $match )
          $matches[ $ix ] = trim( $match );
      
        return $matches;
      
      } // parseDocBlock
      

      【讨论】:

      • 虽然这在理论上可以回答这个问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      相关资源
      最近更新 更多