使用不同的浏览器进行参数测试

里亚克桑德·克里莫维奇(Aliaksandr Klimovich)

我试图实现与测试参数化py.testwebdriver使用Python 3:

import pytest
from selenium import webdriver
from urllib.parse import urljoin


BASE_URL = 'http://ya.ru'


class WebDriverWrapper(type):

    def __init__(self, base_url, *args, **kwargs):
        self._base_url = base_url
        super().__init__(*args, **kwargs)

    def get(self, url):
        url = urljoin(self._base_url, url)
        return super().get(url)


@pytest.yield_fixture(scope='session', params=['PhantomJS', 'Firefox'])
def driver(request, base_url=BASE_URL):

    _driver = None           

    if request.param == 'PhantomJS':
        class Driver(webdriver.PhantomJS):
            __metaclass__ = WebDriverWrapper
        _driver = Driver(base_url)

    elif request.param == 'Firefox':
        class Driver(webdriver.Firefox):
            __metaclass__ = WebDriverWrapper
        _driver = Driver(base_url)

    _driver.get('/')

    yield _driver
    _driver.quit()

所以..WebDriverWrapper例如webdriver.Firefox必须将其包装起来,这样base_url就不必再在代码中重复了;然后我有了driver固定的东西params; 每个测试都必须通过相应的param浏览器;我用元类实现了它

在py.test下运行代码后,出现多个错误:

==================================== ERRORS ====================================
_______________ ERROR at setup of test_case[PhantomJS-hsup-hsup] _______________

request = <SubRequest 'driver' for <Function 'test_case[PhantomJS-hsup-hsup]'>>
base_url = 'http://ya.ru'

    @pytest.yield_fixture(scope='session', params=['PhantomJS', 'Firefox'])
    def driver(request, base_url=BASE_URL):

        _driver = None


        if request.param == 'PhantomJS':
            class Driver(webdriver.PhantomJS):
                __metaclass__ = WebDriverWrapper
>           _driver = Driver(base_url)

File "/mnt/common/Projects/python/autotest-snippets/page_object/fixtures.py", line 31

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/phantomjs/webdriver.py", line 50
in __init__
    self.service.start()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <selenium.webdriver.phantomjs.service.Service object at 0x7fc7a2721b70>

    def start(self):
        """
            Starts PhantomJS with GhostDriver.

            :Exceptions:
             - WebDriverException : Raised either when it can't start the service
               or when it can't connect to the service
            """
        try:
            self.process = subprocess.Popen(self.service_args, stdin=subprocess.PIPE,
                                            close_fds=platform.system() != 'Windows',
                                            stdout=self._log, stderr=self._log)

        except Exception as e:
>           raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
E           selenium.common.exceptions.WebDriverException: Message: Unable to start phantomjs with ghostdriver.
E           Screenshot: available via screen

File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/phantomjs/service.py", line 75
WebDriverException
________________ ERROR at setup of test_case[Firefox-hsup-hsup] ________________

request = <SubRequest 'driver' for <Function 'test_case[Firefox-hsup-hsup]'>>
base_url = 'http://ya.ru'

    @pytest.yield_fixture(scope='session', params=['PhantomJS', 'Firefox'])
    def driver(request, base_url=BASE_URL):

        _driver = None


        if request.param == 'PhantomJS':
            class Driver(webdriver.PhantomJS):
                __metaclass__ = WebDriverWrapper
            _driver = Driver(base_url)
        elif request.param == 'Firefox':
            class Driver(webdriver.Firefox):
                __metaclass__ = WebDriverWrapper
>           _driver = Driver(base_url)

File "/mnt/common/Projects/python/autotest-snippets/page_object/fixtures.py", line 35

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <fixtures.driver.<locals>.Driver object at 0x7fc7a26c5278>
firefox_profile = 'http://ya.ru', firefox_binary = None, timeout = 30
capabilities = None, proxy = None

    def __init__(self, firefox_profile=None, firefox_binary=None, timeout=30,
                 capabilities=None, proxy=None):

        self.binary = firefox_binary
        self.profile = firefox_profile

        if self.profile is None:
            self.profile = FirefoxProfile()

        self.profile.native_events_enabled = (
>           self.NATIVE_EVENTS_ALLOWED and self.profile.native_events_enabled)
E       AttributeError: 'str' object has no attribute 'native_events_enabled'

File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/firefox/webdriver.py", line 46
AttributeError
=========================== 2 error in 0.13 seconds ============================

如何解决?

里亚克桑德·克里莫维奇(Aliaksandr Klimovich)

用以下方法解决了它:

def Driver(klass, base_url):

    class WebDriverWrapper(klass):

        def __init__(self, base_url, *args, **kwargs):
            self._base_url = base_url
            super().__init__(*args, **kwargs)

        def get(self, url):
            url = urljoin(self._base_url, url)
            return super().get(url)

    return WebDriverWrapper(base_url) 

@pytest.yield_fixture(scope='session', params=[
    ('PhantomJS', webdriver.PhantomJS), 
    ('Firefox',   webdriver.Firefox)
])
def driver(request, base_url=BASE_URL):
    _driver = Driver(request.param[1], base_url)    
    _driver.get('/')
    yield _driver
    _driver.quit()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Jasmine使用不同的浏览器测试Angular应用

使用不同浏览器时悬停效果的问题

如何使用Dart进行自动浏览器测试?

如何使用不同的网络适配器打开不同的浏览器窗口?

Web浏览器是否对不同的选项卡使用不同的传出端口?

使用Rspec进行测试会出错,但是使用浏览器进行测试可以正常工作

模拟浏览器和服务器之间的不同时区以进行本地测试

在浏览器中使用Nunjucks,我如何只请求模板一次以使用不同的值进行迭代?

在浏览器控制 C# 中使用不同的 IP

是否可以根据浏览器类型使用不同的CSS背景图像?

如何在无头浏览器中使用不同版本的渲染引擎渲染网页?

使用不同的浏览器,通过getUserMedia获得一致的音频质量

使用不同浏览器大小的菜单时出现意外结果

使用不同的浏览器解析JavaScript中的浮点数

HTTP请求使用不同的浏览器标头时会发生什么?

不同大小的浏览器使用不同的CSS样式

如何正确指定textarea宽度?使用cols属性,我使用不同的浏览器获得不同的宽度

跨浏览器模拟器进行测试

如何在不同的浏览器中使用LiveServerTestCase运行硒测试?

是否可以在不同的Selenium测试中使用相同的浏览器会话?

使用javascript与浏览器进行交互

无法使用不同的选项卡在同一浏览器上同时使用前端和后端登录

我可以使用TestCafe进行非浏览器测试吗?

在Docker中使用Capybara和无头硒浏览器进行Rails系统测试

Puppeteer - Cucumber - 使用一个浏览器进行测试

TestCafe:跨不同浏览器的并发测试问题

在不同的浏览器上运行behat测试

通过 TestNG 在不同浏览器上运行 selenium 测试

testcafe基于浏览器运行不同的测试