[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple #index-url=http://mirrors.aliyun.com/pypi/simple/ #[install] #trusted-host=mirrors.aliyun.com |
python3 -m playwright codegen -o 'login_b2c.py' --target python -b chromium http://localhost:8080/b2c/index.html |
from playwright import sync_playwright def run(playwright): browser = playwright.chromium.launch(headless=False) context = browser.newContext() # Open new page page = context.newPage() # Go to http://localhost:8080/b2c/index.html page.goto("http://localhost:8080/b2c/index.html") # Click text=/.*登录.*/ page.click("text=/.*登录.*/") # assert page.url == "http://localhost:8080/b2c/login.html" # Click input[name="username"] page.click("input[name=\"username\"]") # Fill input[name="username"] page.fill("input[name=\"username\"]", "tester") # Press Enter page.press("input[name=\"username\"]", "Enter") # Click input[name="password"] page.click("input[name=\"password\"]") # Fill input[name="password"] page.fill("input[name=\"password\"]", "123456") # Click input[name="validcode"] page.click("input[name=\"validcode\"]") # Fill input[name="validcode"] page.fill("input[name=\"validcode\"]", "1111") # Click input[type="button"] # with page.expect_navigation(url="http://localhost:8080/b2c/index.html"): with page.expect_navigation(): page.click("input[type=\"button\"]") page.close() # --------------------- context.close() browser.close() with sync_playwright() as playwright: run(playwright) |
# coding=utf-8 from playwright.sync_api import Page import pytest # 此处构造测试用例所需的数据,第一位是用户名,第二位是密码,第三位为预期结果 data=[['tester','123456','您好:tester'],['tester1','123456','您好:tester1'],['tester2','1234567','账号密码错误']] # 使用pytest.mark.parametrize引入用户数据 @pytest.mark.parametrize('userdata',data) # 测试函数的第一个参数为引用playwright提供的Page,这是一个fixture,需要安装 # pytest-playwright def test_login(pageage,userdata): page.goto("http://localhost:8080/b2c/index.html") # Click text=/.*登录.*/ page.click("text=/.*登录.*/") # assert page.url == "http://localhost:8080/b2c/login.html" # Click input[name="username"] page.click("input[name=\"username\"]") # Fill input[name="username"] # page.fill("input[name=\"username\"]", "tester") page.fill("input[name=\"username\"]", userdata[0]) # Press Enter page.press("input[name=\"username\"]", "Enter") # Click input[name="password"] page.click("input[name=\"password\"]") # Fill input[name="password"] page.fill("input[name=\"password\"]", userdata[1]) # Click input[name="validcode"] page.click("input[name=\"validcode\"]") # Fill input[name="validcode"] page.fill("input[name=\"validcode\"]", "1111") # Click input[type="button"] # 因为需要转换页面,这里需要主动等待1秒钟,这个wait_for_timeout的单位是毫秒 page.wait_for_timeout(1000) page.click("input[type=\"button\"]") # 页面刷新较慢,这里我设了3秒等待 page.wait_for_timeout(3000) # 断言,这里使用了in的方式,为了简便。playwright提供了很多的assert断言方式 # 可以阅读官方文档:https://playwright.dev/python/docs/assertions assert userdata[2] in page.content() # Close page page.close() if __name__ == '__main__': # 使用pytest.main来运行测试,--headful是有界面运行,删掉,就是无头模式 # 这里可以看出playwright的有头指的是要启动浏览器,无头模式就是不看到浏览器运行 pytest.main(['-v','login_b2c.py','--headful']) |
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/huminghai/dev/py_pro/demo/login_b2c.py ========================test session starts ==================== platform darwin -- Python 3.7.1, pytest-6.1.0, py-1.10.0, pluggy-0.13.1 -- /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 cachedir: .pytest_cache rootdir: /Users/huminghai/dev/py_pro/demo plugins: base-url-1.4.2, sugar-0.9.4, flaky-3.7.0, xdist-2.1.0, cov-2.10.1, asyncio-0.14.0, playwright-0.0.11, timeout-1.4.2, forked-1.3.0 collecting ... collected 3 items login_b2c.py::test_login[chromium-userdata0] PASSED [ 33%] login_b2c.py::test_login[chromium-userdata1] PASSED [ 66%] login_b2c.py::test_login[chromium-userdata2] PASSED [100%] ======================= 3 passed in 41.42s ==================== Process finished with exit code 0 |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |