Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser session in setUp(), tearDown(), no per testcase setup?

I've previously written some selenium tests using ruby/rspec, and found it quite powerful. Now, I'm using Selenium with PHPUnit, and there are a couple of things I'm missing, it might just be because of inexperience. In Ruby/RSpec, I'm used to being able to define a "global" setup, for each test case, where I, among other things, open up the browser window and log into my site.

I feel that PHPUnit is a bit lacking here, in that 1) you only have setUp() and tearDown(), which are run before and after each individual test, and that 2) it seems that the actual browser session is set up between setUp() and the test, and closed before tearDown().

This makes for a bit more clutter in the tests themselves, because you explicitly have to open the page at the beginning, and perform cleanups at the end. In every single test. It also seems like unnecessary overhead to close and reopen the browser for every single test, in stead of just going back to the landing page.

Are there any alternative ways of achieving what I'm looking for?

like image 659
rogerkk Avatar asked Mar 31 '11 08:03

rogerkk


People also ask

Where will you use setUp () and teardown () methods?

Prepare and Tear Down State for a Test Class XCTest runs setUp() once before the test class begins. If you need to clean up temporary files or capture any data that you want to analyze after the test class is complete, use the tearDown() class method on XCTestCase .

What does the teardown () function do?

tearDown()Provides an opportunity to perform cleanup after each test method in a test case ends.

What is setUp and teardown?

setUp() — This method is called before the invocation of each test method in the given class. tearDown() — This method is called after the invocation of each test method in given class.

What is setUp and teardown in selenium?

If you are not familiar with setup & teardown concepts, essentially, it allows you to run certain steps before and after your actual test. So, when you run the Setup method, it will trigger the steps BEFORE your test will run and the TearDown will run the steps AFTER your test has finished.


1 Answers

What I have done in the past is to make a protected method that returns an object for the session like so:

protected function initBrowserSession() {
    if (!$this->browserSession) {
        $this->setBrowser('*firefox');
        $this->setBrowserUrl('http://www.example.com/');
        //Initialize Session
        $this->open('http://www.example.com/login.php');
        // Do whatever other setup you need here
    }
    $this->browserSession = true;
}

public function testSomePage() {
    $this->initBrowserSession();
    //Perform your test here
}

You can't really use the setupBefore/AfterClass functions since they are static (and as such you won't have access to the instance).

Now, with that said, I would question your motivation for doing so. By having a test that re-uses a session between tests you're introducing the possibility of having side-effects between the tests. By re-opening a new session for each test you're isolating the effects down to just that of the test. Who cares about the performance (to a reasonable extent at least) of re-opening the browser? Doing so actually increases the validity of the test since it's isolated. Then again, there could be something to be said for testing a prolonged session. But if that was the case, I would make that a separate test case/class to the individual functionality test...

like image 187
ircmaxell Avatar answered Nov 15 '22 09:11

ircmaxell