How to prevent Selenium from closing browser after each function?

6.9k views Asked by At

I'm a newbie on selenium and I wrote a little python script using selenium to test some functionalities on our website.

But I notice something strange in my code. I have several functions for example :

  1. Login to website
  2. List item
  3. Click on a link etc.

But, each time selenium hits the bottom of a function it closes the browser and I lose my session. This forces me to put all the test in just one function.

Is there a way to prevent this behavior? I'm using selenium RC not a Webdriver.

2

There are 2 answers

2
user3142208 On BEST ANSWER

I figure it out :) It seems that wen you remove the test_ from the beginning of the function I can setup function wihtout the browser to be closed

4
ddavison On

That is actually by design.

In order to keep a maintainable, independent test suite, you'll need your tests to close your browser.

Picture this:

class MyTestClass:
  def test_method1():
    test_something()

  def test_method2():
    test_something_else()

Once test_method1() has started running, it should have it's own browser instance, and should close it afterwards.

Once test_method2() has started running, it should have it's own browser, and close it afterwards. This is a good design to have. I'd recommend not changing it.