使用Selenium/Python/Nos时,浏览器实例化两次

2024-04-24 02:57:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Selenium和python绑定创建一个示例测试,并用nose运行它。我知道我做错了什么,因为测试会打开两个浏览器(当安装程序运行时,Firefox窗口会立即打开并关闭,然后当测试运行时)驱动程序。获取,另一个窗口打开)。我有以下项目:

/test_project
    /config
        config.ini
    /pages
        __init__.py
        test_page.py
    /test_scripts
        script.py
    __init__.py
    base.py
    config_parser.py

配置.ini:

^{pr2}$

基准.py

from selenium import webdriver
from config_parser import Config


class TestCase(object):

    def setup(self):
        self.config = Config()

        if self.config.read_config('Selenium', 'browser').lower() == 'firefox':
            self.driver = webdriver.Firefox()
        elif self.config.read_config('Selenium', 'browser').lower() == 'chrome':
            self.driver = webdriver.Chrome(self.config.read_config('Selenium', 'chromedriver_path'))

    def teardown(self):
        self.driver.quit()

测试_页面.py

from config_parser import Config

class TestPage(object):

    def __init__(self, driver):
        self.driver = driver
        self.config = Config()

    def open(self):
        self.driver.get(self.config.read_config('Selenium', 'base_url'))
        import time
        time.sleep(3)

脚本.py

from pages import test
from base import TestCase


class RandomTest(TestCase):

    def test_foo(self):
        x = test.TestPage(self.driver)
        x.open()
        assert 1 == 1

有人能帮我理解为什么会打开两个浏览器窗口,以及我能做些什么来纠正这个问题吗?在

提前谢谢你。在


Tags: frompytestimportselfconfigparserread
1条回答
网友
1楼 · 发布于 2024-04-24 02:57:29

这是因为您的基本TestCase类也被nose test runner识别为一个测试。在

^{}装饰器标记它:

from selenium import webdriver
from config_parser import Config
from nose.tools import nottest

@nottest
class TestCase(object):
    ...

相关问题 更多 >