5

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?

1
  • What if you use PHPUnit_Extensions_Selenium2TestCase::shareSession(true); in your phpunit bootstrap? Commented Dec 6, 2013 at 2:21

2 Answers 2

4
+150

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I did. There is test code pastebin.com/piXzeeN8. testGoogle2() - is not successful. Output is: .F. There was 1 failure: 1) S2p_GoogleTest:testGoogle2 Failed asserting that true is false.
Thanks, just tested it and it works. Reduced sample code with google.
0

Add following lines to your .xml file so even if one test case fails it will run next test.

<phpunit bootstrap="bootstrap.php"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     stopOnFailure="false">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.