Lucas Gonzalez
Lucas Gonzalez

Reputation: 106

How to use two browsers in selenium with phpunit

I need to make a test using two browser windows at the same time. I am using phpunit with selenium.

Example:

  1. Open browser1 and navigate to some url
  2. Copy some dynamic content
  3. Open browser2, navigate to other url, fill a form with the content of step 2 and submit the form.

I cannot navigate to the url in step 3 from the browser1 because then it will not work.

Right now I cannot open browser2, every attempt I do will use browser1.

Any ideas? Thanks.

Upvotes: 0

Views: 96

Answers (2)

Lucas Gonzalez
Lucas Gonzalez

Reputation: 106

Thanks Keith Tyler. I played a bit with the code and finally I was able to do it.

I will put the code here because it may be usefull for someone.

First thing is to create a class extending PHPUnit_Extensions_Selenium2TestCase:

class Browser extends PHPUnit_Extensions_Selenium2TestCase
{   
    public function __construct(){
        parent::__construct();
        $this->setHost("127.0.0.1");
        $this->setPort(4444);
        $this->setBrowser("firefox");
        $this->setBrowserUrl("url");
        $this->prepareSession(); // this does the trick
    }   
}

Then you can use it like this:

$this->url("url1"); // $this will be the default browser
$browser2 = new Browser(); // $browser2 is the new browser and has all the functions from phpunit and selenium available
$browser2->url("url2");

Hope it will save time to someone.

Upvotes: 0

Keith Tyler
Keith Tyler

Reputation: 825

I've done this. You basically need a second driver object, and use open() on that object. So now you have two driver objects -- one for browser 1 and one for browser 2. You're gonna have to remember which driver object. Because if you want to trigger an action in browser 2, you need to call the desired functions on that second driver object, instead of the default.

It's not quite intuitive since out of the box most Selenium APIs pretty much give you a singleton driver object without really asking.

Upvotes: 1

Related Questions