在Python Playwright中实现自动化测试主要涉及以下几个步骤:
pip install playwright
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
context = browser.new_context()
context.goto()
方法导航到目标网页:url = 'https://example.com'
context.goto(url)
element = context.get_element('#element-id')
element.click()
assert context.title() == 'Expected Title'
browser.close()
方法关闭浏览器实例:browser.close()
from playwright.sync_api import sync_playwright
def test_example():
with sync_playwright() as p:
browser = p.chromium.launch()
context = browser.new_context()
url = 'https://example.com'
context.goto(url)
element = context.get_element('#element-id')
element.click()
assert context.title() == 'Expected Title'
browser.close()
if __name__ == '__main__':
test_example()
这个示例脚本将启动Chrome浏览器,导航到指定网页,点击一个元素,然后检查页面标题是否符合预期,最后关闭浏览器。你可以根据自己的需求编写更复杂的自动化测试脚本。