Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a PHPUnit Selenium test without having a new browser window run for each function?

I am trying to run a selenium test case using PHPUnit. And the first thing I do is trying the login function, this works perfect but then I want to run a function to check information on the page following the login but it opens a new browser instead of continuing in the current browser window. The reason this is a problem is because the page is setup to remove login authentication when the window is closed so if you use $this->url() to go to the page it gives the error that I need to login. This is my code right now, It starts the browser and runs the function to test the login form, then it closes the browser, open a new one and run the link check. This of course results in an error due to the authentication error because the window was closed. I could run all the tests in one function but that is really sloppy coding and I want to avoid this. Anyone know how to solve this?

<?php
    class TestMyTest extends PHPUnit_Extensions_Selenium2TestCase {
        public function setUp()
        {
            $this->setBrowser("firefox");
            $this->setBrowserUrl("https://**************************");
        }

        public function testLoginForm()
        {

            $this->url("login.php");
            $this->byLinkText('Forgot your password?');
            $form = $this->byCssSelector('form');
            $this->byName('username')->value('test');
            $this->byName('password')->value('1234');
            $form->submit();
        }


        public function testCheckForMainMenueLinks ()
        {
            $this->url("index.php");
            $this->byLinkText('Home');
            $this->byLinkText('Products');
            $this->byLinkText('About us');
            $this->byLinkText('Contact');
        }
    }
?>
like image 849
user1593846 Avatar asked May 24 '13 11:05

user1593846


4 Answers

To share browser sessions in Selenium2TestCase, you must set sessionStrategy => 'shared' in your initial browser setup:

public static $browsers = array(
    array(
        '...
        'browserName' => 'iexplorer',
        'sessionStrategy' => 'shared',
        ...
    )
);

The alternative (default) is 'isolated'.

like image 79
Ben Avatar answered Nov 15 '22 03:11

Ben


Okej so I guess you can just call the function directly from another function like so:

public function testOne
{
#code
$this->Two();
}

public function Two()
{
#code
$this->Three();
}

public function Three()
{
#code
}

and so on, this will just run the next function without a new browser, however, if it fails anywhere in any test the whole test is stoped so the feedback wont bee as good as individual tests.

like image 33
user1593846 Avatar answered Nov 15 '22 03:11

user1593846


make assetrions in one function because this is functional test. i am new to phpunit and selenium too, but I successfully test all in one like this:

public function testAuth(){  

$this->open('register.php&XDEBUG_SESSION_START=PHPSTORM');
$this->assertTextPresent('Register');
$this->type('name=email', "...");
$this->type('name=firstname', "...");
$this->type('name=lastname', "...");       
$this->type('name=password', "...");
$this->type('name=verifyPassword', "...");
$this->click("reg-butt");
$this->waitForPageToLoad("5000");
$this->assertTextPresent('Profile');
$this->open('logout.php');
$this->assertTextPresent('text from redirect page');
$this->open('login.php');
.....

}
like image 33
Anatoliy Gusarov Avatar answered Nov 15 '22 03:11

Anatoliy Gusarov


An elegant way to set the session shared is to use PHPUnit's setUpBeforeClass() method:

public static function setUpBeforeClass()
{
    self::shareSession(true);
}
like image 25
Raphaël Doursenaud Avatar answered Nov 15 '22 04:11

Raphaël Doursenaud