【问题标题】:Selenium webdriver in python: Re-using same web browser across testcasespython中的Selenium webdriver:跨测试用例重用相同的Web浏览器
【发布时间】: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


    【解决方案1】:

    您可以尝试创建另一个模块(我通常使用 pkg.__init__ 来处理此类事情)并在其中放置一个返回 selenium 驱动程序的函数。当然,如果已经存在,则返回缓存的。例如。在 mypkg/__init__.py

    from selenium import webdriver
    
    DRIVER = None
    
    def getOrCreateWebdriver():
        global DRIVER
        DRIVER = DRIVER or webdriver.Firefox()
        return DRIVER
    

    并从您的测试中调用:

    import mypkg
    ...
    class testcaseA(unittest.TestCase):
    
        def setUp(self):
            self.driver = mypkg.getOrCreateWebdriver()
    

    【讨论】:

    • 谢谢,但我收到此错误:ft1.1:Traceback(最近一次调用最后一次):文件“C:\test\testcaseA.py”,第 11 行,在 setUp self.driver .implicitly_wait(30) AttributeError: 'NoneType' 对象没有属性 'implicitly_wait'
    • 想通了。您在 init.py 中的示例最后必须有“return DRIVER”....就像我说的,我是 python newb ;)
    【解决方案2】:

    您有一个例外,因为 testcaseA.py 不知道 DRIVER 是什么。你必须以某种方式导入它。您不能直接从 main.py 导入它,因为这会导致循环依赖。最好的解决方案是在单独的模块中创建 DRIVER。

    如果您开始使用 Python 进行测试,请查看 nosetests 库。你不会后悔的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2015-01-17
      • 2020-02-26
      • 2013-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多