编写测试代码
创建一个名为 test_itestcat.py
的文件,并写入以下代码:
1 2 3 4 5 6 7 8 9 |
def test_itestcat(page): # 打开 itestcat.com page.goto("https://itestcat.com") # 验证页面标题 assert "itestcat" in page.title() # 在页面中查找特定的元素 (可以根据需求调整) assert page.locator("text=首页").is_visible() |
运行测试
在终端中运行以下命令来执行测试:
pytest test_itestcat.py
详细解释:
page.goto()
用于让浏览器导航到指定的 URL,这里是itestcat.com
。page.title()
用于获取当前页面的标题,assert
用于验证页面标题是否包含特定的文本。page.locator()
用于查找页面中的元素,你可以根据需要调整此部分代码。
运行上述代码后,Playwright 会在后台自动运行浏览器,访问 itestcat.com
并进行测试。
原创文章,作者:iTestCat,保留所有权利,禁止转载,如若转载,请联系作者!