Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit extension Selenium 2 (webdriver) and multiple browsers

I wrote a test case with PHPUnit extension Selenium2TestCase. It does work nice, but I can't figure out how make automatically run this test on various browsers.

There is a method setBrowser() which it only works in setUp() method. I thought about something like this:

/**
 * @dataProvider browsers
 */
public function loginTest($browser) {
    $this->setBrowser($browser);
    // tests...
}

But I does not work. Try runs default browser (Propably I have a small mess with Safari, last time uses firefox)

RuntimeException: Safari could not be found in the path!
Please add the directory containing ''Safari'' to your PATH environment
variable, or explicitly specify a path to Safari like this:
*safari /blah/blah/Safari

PS. SeleniumTestCase (not based on webdriver) provides a xml config where we can specify browsers. Selenium2TestCase does not support it.

Any suggestions are welcome. Thanks. Marcin

like image 850
Marcin Rogacki Avatar asked Oct 02 '12 13:10

Marcin Rogacki


4 Answers

something like this

    class WebTestCase extends \application\components\test\ExWebTestCase
    {
      // default params
      public $parameters = array(
          'host'                          => 'localhost',
          'port'                          => 4444,
          'seleniumServerRequestsTimeout' => 30000,
          'timeout'                       => 30000,
      );

      // list of browsers with per-browserconfig
      public static $browsers = array(
          array(
              'browserName' => 'firefox',
          ),
          array(
              'browserName' => 'chrome',
          ),
          array(
              'browserName' => 'safari',
          ),
          array(
              'browserName' => 'internet explorer',
              'host' => 'some IP of VirtualBox with IE'
          )
      );
  }
like image 101
lexand Avatar answered Nov 19 '22 12:11

lexand


The fix for this really depends on what your data file looks like. If you could post that we could help you more.

For the time being I'm assuming since phpunit by nature is a one-at-a-time unit testing framework that you're not attempting to run multiple browsers simultaneously, but just want to reserve the option to change browsers as you see fit.

You're right that you should be using setBrowser in setUp. When executed PHPUnit will always run setUp first, and tearDown() last. A good practice here is to make your own custom unit-test-case class where you can customize these methods.

class customUnitTest extends PHPUnit_Extensions_Selenium2TestCase {
   public $browser = "firefox";
   public function setUp() {
      $this->setBrowser("*".$browser);
   }
}

Now when you're writing a test extend your personal test class and set the browser accordingly

class newTest extends customUnitTest {
   $this->browser = "safari";
   public function testBlah {
      blah blah...
   }
}

setUp will be run on execution, and it will pull in the browser variable. By default you'll get firefox but if some tests are more appropriately tested on other browsers you have that option.

If you're looking to be able to change all browsers across all tests simultaneously you should look into assigning the browser value based on an environment variable.

class customUnitTest extends PHPUnit_Extensions_Selenium2TestCase {
   try {
      public $browser = getenv("SELENIUM_BROWSER");
   } catch (Exception $e) {
      public $browser = "firefox";
   }
   public function setUp() {
      $this->setBrowser("*".$browser);
   }
}

With this setup we can change the browser for every test that hasn't hard-coded the browser within itself by changing the environment variable SELENIUM_BROWSER. This way we can run the same code on different servers with different default browsers without having to re-write anything.

Note that multiple inheritence is not good practice. It can lead to brittle code and even security threats if you don't scope you methods/variables correctly. However in this case it's useful because we can define the PHPUnit framework methods as we please, and we get all the base selenium methods within our test. So to run a default selenium method we just write

$this->open("www.google.com");

This is a much different method than the general approach of assigning selenium to an object, as the test you write IS the selenium object, but it seems more php appropriate, especially for this use case.

like image 43
SuperFamousGuy Avatar answered Nov 19 '22 12:11

SuperFamousGuy


To run in multiple browser check this link:

http://phpunit.de/manual/current/en/selenium.html example 17.4(phpunit 3.7) If ur running the testcase in localhost, use 'host ' =>'localhost'. setBrowser() function is not need..

like image 24
smsivaprakaash Avatar answered Nov 19 '22 11:11

smsivaprakaash


Use should user the $browsers property for defining multiple browsers, as defined above.

like image 1
HerrWalter Avatar answered Nov 19 '22 13:11

HerrWalter