4

Python newb here. I'm trying to re-use same browser throughout my testcases. However, I cannot figure out how to pass global variable to make this work.

Currently, I have a main.py that looks like this #!C:/Python27/python.exe

import unittest import unittest, time, re, HTMLTestRunner, cgi import os, sys, inspect from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException global DRIVER DRIVER = webdriver.Firefox() # Make all subfolders available for importing cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0])) if cmd_folder not in sys.path: sys.path.insert(0, cmd_folder) # Import test cases from setup.testcaseA import * from setup.testcaseB import * # serialize the testcases (grouping testcases) suite = unittest.TestSuite() # setup new test suite suite.addTest(unittest.makeSuite(testcaseA)) suite.addTest(unittest.makeSuite(testcaseB)) runner = HTMLTestRunner.HTMLTestRunner() print "Content-Type: text/html\n" # header is required for displaying the website runner.run(suite) 

And I have testcaseA.py file in setup/ folder that looks like this:

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException import unittest, time, re, cgi class testcaseA(unittest.TestCase): def setUp(self): #get global driver variable <- DOESNT WORK! self.driver = DRIVER def testcaseA(self): driver = self.driver #Bunch of tests def tearDown(self): #self.driver.quit() <- Commented out, because I want to continue re-using the browser 

testcaseB.py is basically identical to testcaseA.py

When I run main.py, I get an error: ft1.1: Traceback (most recent call last): File "C:\test\setup\testcaseA.py", line 10, in setUp self.driver = DRIVER #get global driver variable NameError: global name 'DRIVER' is not defined

Any suggestions?

Thanks!

2 Answers 2

11

You could try creating another module (I usually use pkg.__init__ for such things) and in there put a function that returns the selenium driver. Return the cached one if already exists, of course. Eg. in mypkg/__init__.py

from selenium import webdriver DRIVER = None def getOrCreateWebdriver(): global DRIVER DRIVER = DRIVER or webdriver.Firefox() return DRIVER 

And call from your tests with:

import mypkg ... class testcaseA(unittest.TestCase): def setUp(self): self.driver = mypkg.getOrCreateWebdriver() 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but I am getting this error: ft1.1: Traceback (most recent call last): File "C:\test\testcaseA.py", line 11, in setUp self.driver.implicitly_wait(30) AttributeError: 'NoneType' object has no attribute 'implicitly_wait'
Figured it out. Your example in init.py had to have "return DRIVER" in the end.... Like I said, I'm a python newb ;)
3

You have an exception because testcaseA.py has no idea what DRIVER is. You have to import it somehow. You cannot import it directly from main.py because that will result in circular dependency. The best solution would be to create DRIVER in a separate module.

If you are starting your journey with testing in Python take a look at nosetests library. You will not regret it!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.