Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7975159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:34:27+00:00 2026-06-04T08:34:27+00:00

Python newb here. I’m trying to re-use same browser throughout my testcases. However, I

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:34:28+00:00Added an answer on June 4, 2026 at 8:34 am

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Python newb here. I m trying to count the number of letter as in
Newb programmer here, I'm most familiar with Python but also learning C and Java,
Python newb...beware! I am trying to recursively ftp some files. In the script below,
I'm a python newb and am having trouble groking nested list comprehensions. I'm trying
Python newb here looking for some assistance... For a variable number of dicts in
Python newbie here. I'm trying to package a console app following this doc .
I keep having problems with programs in Python (I'm a complete newb) where it
I'm a newb to Python so apologies in advance if my question looks trivial.
Python newbie here and wanted to know if there was a way to combine
I have only learnt python for for few months and totally a newb in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.