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”
有什么建议么?
Thanks!