Python新手在這裏。 我試圖在我的測試用例中重複使用相同的瀏覽器。 但是,我無法弄清楚如何通過全局變量來完成這項工作。Python中的Selenium webdriver:跨測試用例重複使用相同的Web瀏覽器
目前, 我有一個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)
而且我在安裝/ 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.1:回溯(最近通話最後一個): 文件 「C:\測試\ SETUP \ testcaseA.py」,10號線,在安裝程序中 self.driver = DRIVER#獲取全局變量驅動 NameError:全局名稱 'DRIVER'沒有定義
有什麼建議?
謝謝!
謝謝你,但我得到這個錯誤: ft1.1:回溯(最後最近一次調用): \t文件 「C:\測試\ testcaseA.py」,第11行,在安裝程序中 \t \t自我.driver.implicitly_wait(30) \t AttributeError的: 'NoneType' 對象沒有屬性 'implicitly_wait' – m0dE
了它。你在___init___.py中的例子最後必須有「return DRIVER」....就像我說的,我是一個python newb;) – m0dE
在答案中加入return語句,乾杯 –