python中的Selenium webdriver:跨测试用例重复使用相同的网络浏览器 [英] Selenium webdriver in python: Re-using same web browser across testcases

查看:57
本文介绍了python中的Selenium webdriver:跨测试用例重复使用相同的网络浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是 Python 新手.我正在尝试在整个测试用例中重复使用相同的浏览器.但是,我不知道如何传递全局变量来完成这项工作.

Python newb here. I'm trying to re-use same browser throughout my testcases. However, I cannot figure out how to pass global variable to make this work.

目前,我有一个看起来像这样的 main.py#!C:/Python27/python.exe

Currently, I have a main.py that looks like this #!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 文件,如下所示:

And I have testcaseA.py file in setup/ folder that looks like this:

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 基本相同

testcaseB.py is basically identical to testcaseA.py

当我运行 main.py 时,出现错误:ft1.1:回溯(最近一次调用最后一次):文件C:\test\setup\testcaseA.py",第 10 行,在 setUp 中self.driver = DRIVER #获取全局驱动变量NameError: 全局名称DRIVER"未定义

When I run main.py, I get an error: ft1.1: Traceback (most recent call last): File "C:\test\setup\testcaseA.py", line 10, in setUp self.driver = DRIVER #get global driver variable NameError: global name 'DRIVER' is not defined

有什么建议吗?

谢谢!

推荐答案

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

You could try creating another module (I usually use pkg.__init__ for such things) and in there put a function that returns the selenium driver. Return the cached one if already exists, of course. Eg. in 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()

这篇关于python中的Selenium webdriver:跨测试用例重复使用相同的网络浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆