开发者

How do I run a PHPUnit Selenium test case without having the browser open with each function?

Currently, I have a PHPUnit test case that extends PHPUnit_Extensions_SeleniumTestCase. Each function that starts r开发者_JAVA百科equires a $this->setBrowserUrl() and defaults to starting a new Firefox browser window with each function call.

I want to have a test case that launches the browser for specific functions, but not launch the browser for other functions, as to save the resources and time it takes in opening and closing the browser. Is it possible for me to have such a file?


You best option is probably to create two separate test suites, one that uses uses Selenium commands and the other that does not use any Selenium functionality..

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
    {
       ...
    }
    ...
}


Figured out a custom solution using PHPUnit annotations (and wrote a blog post about it!)

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

EDIT: Adding some code here, as to make my answer more complete :)

In short, use custom annotations. In your setUp(), parse the doc block to grab annotations, and tag tests with different qualities. This would allow you to tag certain tests to run with a browser, and certain tests to run without.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜