?
?
?
Playwright简介:
Playwright优势:
Python环境:python解释器+IDE工具,不知道怎么安装的可参考: python3.4 + pycharm 环境安装 + pycharm 使用
Playwright环境:
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("http://testingpai.com/")
page.get_by_text("登录").click()
page.get_by_placeholder("用户名/邮箱/手机号").click()
page.get_by_placeholder("用户名/邮箱/手机号").fill("kemi")
page.get_by_placeholder("用户名/邮箱/手机号").press("Tab")
page.get_by_placeholder("密码").fill("123456")
page.pause() # 断点调试功能
page.get_by_role("button", name="登录").click()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
get_by_id
: 通过元素的 id 属性来查找元素,例如:element = page.get_by_id("my-id")get_by_name
: 通过元素的 name 属性来查找元素,例如:element = page.get_by_name("my-name")get_by_text
: 通过元素的文本内容来查找元素,例如:element = page.get_by_text("Submit")get_by_title
: 通过元素的 title 属性来查找元素,例如:element = page.get_by_title("my-title")get_by_placeholder
: 通过元素的 placeholder 属性来查找元素,例如:element = page.get_by_placeholder("my-placeholder")get_by_selector
: 通过 CSS 选择器来查找元素,例如:element = page.get_by_selector("#submit-button")get_by_xpath
: 通过 XPath 表达式来查找元素,例如:element = page.get_by_xpath("//div[@class='my-class']")get_by_label
: 根据label属性值查找元素,类似于HTML中的label标签和对应的for属性。get_by_role
: 通过角色方式定位元素,例如:element = page.get_by_role("button", name="Submit")page.locator("#element-id")
或page.locator('id=element-id')
定位。page.locator("css=selector")
定位。例如:element = page.locator("button#submit-button")page.locator("xpath=expression")
定位。例如:element = page.locator("//button[@id='submit-button']")page.locator("name=element-name")
定位。page.locator("text=link-text")
定位。page.locator("partial_text=partial-link-text")
定位。page.locator("[attribute=value]")
定位。page.locator("tag=tag-name")
定位。from playwright.sync_api import Page, expect
def test_example(page: Page) -> None:
page.goto("http://testingpai.com/")
page.get_by_text("登录").click()
page.get_by_placeholder("用户名/邮箱/手机号").click()
page.get_by_placeholder("用户名/邮箱/手机号").fill("kemi")
page.get_by_placeholder("用户名/邮箱/手机号").press("Tab")
page.get_by_placeholder("密码").fill("123456")
page.get_by_role("button", name="登录").click()
import pytest
from playwright.sync_api import Page, expect
# 测试用例数据
case_data = [{'caseid':1,'username':'kemi123', 'password':'kemi123'},
{'caseid':2,'username':'zhangsan', 'password':'123456'},
{'caseid':3,'username':'lisi', 'password':'123456'}]
# 数据驱动
@pytest.mark.parametrize('case', case_data)
def test_example(page: Page,case) -> None:
username = case['username']
password = case['password']
page.goto("http://testingpai.com/")
page.get_by_text("登录").click()
page.get_by_placeholder("用户名/邮箱/手机号").click()
page.get_by_placeholder("用户名/邮箱/手机号").fill(username)
page.get_by_placeholder("用户名/邮箱/手机号").press("Tab")
page.get_by_placeholder("密码").fill(password)
page.get_by_role("button", name="登录").click()
page.screenshot(path=f"images/login/登录{case['caseid']}.png") # 截图保存到指定位置
?