Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit & Selenium2 all cases fail IF one case fails

I am using PHPUnit & Selenium2 server. I am using PageObject pattern. To a page object I get an instance of the webdriver and perform necessary functions.

To keep a single browser running I implemented a crude solution which I found in the net where I initialize the driver within a static class:

class SessionHelper {
    public static $first;
}
SessionHelper::$first = 0;

Then in my test case class setup() method;

public function setUp(){
    if (SessionHelper::$first == 0 )
    {
        $this->setHost('localhost');
        $this->setPort((int)4444);            
        $this->setBrowser('firefox');        
        $this->setBrowserUrl('http://domain.com/lucky/web');            
        $this->shareSession(TRUE);
        $this->prepareSession();
        SessionHelper::$first = 1 ;
    }
}

This way I manage to execute all tests in a single browser. However if one test case fails; say by trying to find a non-existent element, all the other test cases fails with a message "Undefined index: browserUrl". If I change it to look for a known element in the page, it works fine. So for example;

test_method_1 : if an element is not found anything after this test fails with the "Undefined index: browserUrl".

if test_method_1 went ok, rest of the tests will execute until another test case fails.

So, what can be the reason for me to get this error? When one test case fails does my session get destroyed?

like image 744
user2058413 Avatar asked Oct 02 '22 11:10

user2058413


1 Answers

When one test case fails your session gets destroyed:

The session will be reset in the case of not successul tests (failed or incomplete); it is up to the user to avoid interactions between tests by resetting cookies or logging out from the application under test (with a tearDown() method)

From Chapter 17. PHPUnit and Selenium (bottom)

Method onNotSuccessfulTest marks session with flag which sets session to null. On next test run PHPUnit calls prepareSession(), but parameters are null. This is the reason to get error 'Undefined index' in SessionStrategy_Shared.

You can write your onNotSuccessfulTest method:

public function onNotSuccessfulTest(Exception $e){
    throw $e;
}

With it session does not get destroyed.

like image 101
rNix Avatar answered Oct 07 '22 18:10

rNix