Python新手在这里。我试图在我的测试用例中重复使用相同的浏览器。但是,我无法弄清楚如何传递全局变量来完成这项工作。
目前,我有一个 main.py,看起来像这样 #!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)
我在 setup/ 文件夹中有 testcaseA.py 文件,如下所示:
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 与 testcaseA.py 基本相同
当我运行 main.py 时,我收到一个错误:ft1.1: Traceback (last recent call last): File "C:\test\setup\testcaseA.py", line 10, in setUp self.driver = DRIVER #get全局驱动程序变量 NameError:未定义全局名称“驱动程序”
有什么建议么?
谢谢!