使用 Unittest 管理 Playwright 浏览器

问题描述 投票:0回答:1

我对编码有点陌生,被要求对公司网络登录进行测试,他们希望我实现模块unitestt和剧作家测试生成器工具。这是我到目前为止所拥有的。我必须将运行与 test_1 分开,因为单元测试在读取 chromium 行时失败,但现在它只打开浏览器,那么我该怎么做才能让它运行整个测试?

from playwright.sync_api import Playwright, sync_playwright
from locators import Locators_evou
import unittest

class Test(unittest.TestCase):
    def run(playwright: Playwright) -> None:
        browser = playwright.chromium.launch(channel ="chrome", headless=False,slow_mo=500)
        context = browser.new_context()

        page = context.new_page()
        page.goto("http://localhost:3000/")

    def test_1(page):   
        page.click(Locators_evou.user_Log)
        page.fill(Locators_evou.user_Log, "Liliana")
        page.click(Locators_evou.password_log)
        page.fill(Locators_evou.password_log, "1234")
        page.check(Locators_evou.session_Log)
        page.click(Locators_evou.login_log)

        assert page.is_visible("¡Bienvenido!")
        
    with sync_playwright() as playwright:
        run(playwright)

   
if __name__=="__main__":
    unittest.main()
python testing python-unittest playwright playwright-python
1个回答
0
投票

Python 3.10.12 中的这个示例怎么样,它跳过了

with
上下文管理器并且不需要 pytest,如官方文档示例当前所示:

import unittest
from playwright.sync_api import expect, sync_playwright  # 1.37.0


class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.p = sync_playwright().start()
        cls.browser = cls.p.chromium.launch()
        cls.context = cls.browser.new_context()
        cls.context.set_default_timeout(5_000)

    def setUp(self):
        self.page = Test.context.new_page()

    def tearDown(self):
        self.page.close()

    @classmethod
    def tearDownClass(cls):
        cls.context.close()
        cls.browser.close()
        cls.p.stop()

    def test_example(self):
        "validate header on example.com"
        self.page.goto("https://www.example.com")
        expect(self.page.locator("h1")).to_have_text("Example Domain")

    def test_quotes_login(self):
        "login to quotes.toscrape"
        page = self.page
        page.goto("http://quotes.toscrape.com")
        page.get_by_text("login").click()
        page.type('input[name="username"]', "foo")
        page.type('input[name="password"]', "bar")
        page.get_by_role("button", name="Login").click()
        page.get_by_text("Logout").wait_for()


if __name__ == "__main__":
    unittest.main(verbosity=2)
© www.soinside.com 2019 - 2024. All rights reserved.