【发布时间】:2012-05-16 21:46:01
【问题描述】:
这里是 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:回溯(最近一次通话最后一次): 文件“C:\test\setup\testcaseA.py”,第 10 行,在 setUp self.driver = DRIVER #获取全局驱动变量 NameError: 未定义全局名称“DRIVER”
有什么建议吗?
谢谢!
【问题讨论】:
标签: python unit-testing selenium webdriver