Selenium Pytest 如何把浏览器参数化?

lyyyyyyy · 2020年11月24日 · 最后由 lyyyyyyy 回复于 2020年12月02日 · 50 次阅读

如题,我定义了一个 firefox 和一个 chrome 的 fixture,现在想用参数化的形式来做到用 chrome 和 firefox 分别打开一次浏览器去访问百度,请教应该如何去实现呢?

import pytest
import os
from selenium import webdriver


@pytest.fixture(name='chrome')
def chrome_driver():
    driver = webdriver.Chrome()
    driver.maximize_window()
    yield driver
    driver.close()


@pytest.fixture(name='firefox')
def firefox_driver():
    driver = webdriver.Firefox()
    driver.maximize_window()
    yield driver
    driver.close()


def open_url(driver, url):
    driver.get(url)


@pytest.mark.demo1
def test_login(chrome):
    open_url(chrome, "https://www.baidu.com")


if __name__ == '__main__':
    pytest.main(['-m demo1', os.path.abspath(__file__)])
共收到 6 条回复 时间 点赞

pytest.mark.parametrize ,把浏览器放到参数列表

Jerry li 回复

如果不是 fixture 可以用这种方式,fixture 这样用会报错

仅楼主可见
def test_login(chrome, firefox):
    open_url(chrome, "https://www.baidu.com")
    open_url(firefox, "https://www.baidu.com")

这样?

可以看看我之前文章里面这个例子。
https://testerhome.com/topics/25550
8.在不同 python 解释器之间测试对象序列化。python1 把对象 pickle-dump 到文件。python2 从文件中 pickle-load 对象。

"""
module containing a parametrized tests testing cross-python
serialization via the pickle module.
"""
import shutil
import subprocess
import textwrap

import pytest

pythonlist = ["python3.5", "python3.6", "python3.7"]


@pytest.fixture(params=pythonlist)
def python1(request, tmpdir):
    picklefile = tmpdir.join("data.pickle")
    return Python(request.param, picklefile)


@pytest.fixture(params=pythonlist)
def python2(request, python1):
    return Python(request.param, python1.picklefile)


class Python:
    def __init__(self, version, picklefile):
        self.pythonpath = shutil.which(version)
        if not self.pythonpath:
            pytest.skip("{!r} not found".format(version))
        self.picklefile = picklefile

    def dumps(self, obj):
        dumpfile = self.picklefile.dirpath("dump.py")
        dumpfile.write(
            textwrap.dedent(
                r"""
                import pickle
                f = open({!r}, 'wb')
                s = pickle.dump({!r}, f, protocol=2)
                f.close()
                """.format(
                    str(self.picklefile), obj
                )
            )
        )
        subprocess.check_call((self.pythonpath, str(dumpfile)))

    def load_and_is_true(self, expression):
        loadfile = self.picklefile.dirpath("load.py")
        loadfile.write(
            textwrap.dedent(
                r"""
                import pickle
                f = open({!r}, 'rb')
                obj = pickle.load(f)
                f.close()
                res = eval({!r})
                if not res:
                raise SystemExit(1)
                """.format(
                    str(self.picklefile), expression
                )
            )
        )
        print(loadfile)
        subprocess.check_call((self.pythonpath, str(loadfile)))


@user3ize("obj", [42, {}, {1: 3}])
def test_basic_objects(python1, python2, obj):
    python1.dumps(obj)
    python2.load_and_is_true("obj == {}".format(obj))

6楼 已删除
dongfanger 回复

谢谢,有点看不太懂😂

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册