PlayWright基础(1)
基础用法
安装
1
2pip3 install playwright
playwright install实例化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24from playwright.sync_api import sync_playwright, expect, Page
def test_pw_test(page):
pw = sync_playwright().start()
bw = pw.chromium.launch(headless=False) # 默认True 不打开浏览器
content = bw.new_context()
page = content.new_page()
page.goto("http://www.baidu.com/")
# 断点,可打开录制界面
page.pause()
page.get_by_placeholder("Username").click()
# page.get_by_placeholder("Username").fill("admin")
# page.get_by_placeholder("Username").wait_for() # 等待单个元素
page.get_by_placeholder("Username").type("admin", delay=300) # 打字机
page.get_by_placeholder("Password").click()
page.get_by_placeholder("Password").fill("DAIP8888")
page.get_by_role("button", name="Log in").click()
page.wait_for_timeout(3000)
print(page.title())
# 断言
expect(page.locator('.avatar-default').first).to_be_visible()
# 保存cookie
content.storage_state(path='../data/auth.json') # 保存的json可在new_context(storage_state='../data/auth.json')鉴权方法
page也正常登录
直接使用
storage_state
- 直接使用
- 保存到文件
- 保存到内存
cookie
直接使用
保存到文件
保存到内存
处理方式实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33def loginSaveCookie(page: Page, userName: str, pwd: str, host: str):
"""
页面登录
:param page: pw对象
:param userName: 用户名
:param pwd: 密码
:param host: 地址
:return:naji
"""
# 判断cookie文件是否存在并且在有效期范围内
if os.path.exists(f"{userName}.txt") and int(time() - os.path.getctime(f"{userName}.txt")) < 86400:
page.context.clear_cookies()
# 读取
with open(f"{userName}.txt") as f:
cookies = f.read()
cookies = eval(cookies)
# 添加进浏览器内
page.context.add_cookies(cookies)
page.goto(host)
else:
# 切换用户时使用
page.context.clear_cookies()
page.goto(host)
page.get_by_placeholder("Username").click()
page.get_by_placeholder("Username").type(userName, delay=300) # 打字机
page.get_by_placeholder("Password").click()
page.get_by_placeholder("Password").fill(pwd)
page.get_by_role("button", name="Log in").click()
expect(page.get_by_text("智能运维系统")).to_be_visible()
cookies = page.context.cookies()
# 存储cookie
with open(f"{userName}.txt", 'w') as f:
f.write(str(cookies))
接口登录
其他
base_url使用
pytest.ini配置
1
2
3
4# content of pytest.ini
[pytest]
# Run firefox with UI
addopts = --headed --base-url=http://www.baidu.com/代码测试
1
2
3
4
5
6
7
8
9
10
11
12# page.url可获取当前地址的url
def redirectUrl(page: Page, path: str) -> None:
"""
跳转URL
:param page:
:param path: 页面路径
:return:
"""
if path[0] != '/':
path = f"/{path}"
page.goto(f"{page.url.split('/')[0]}{path}")
用例编写
CURD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51import pytest
from playwright.sync_api import sync_playwright, expect, Page
# 新增名称,作为修改时的搜索
dataName = ""
def test_createProject(page: Page):
"""
新增数据
:param page:
:return:
"""
global dataName
dataName = "xxx"
page.locator("新增").fill(dataName)
def test_updateProject(page: Page):
"""
修改数据
:param page:
:return:
"""
global dataName
page.locator("搜索框").fill(dataName)
dataName = "修改后的xx"
# 修改操作
page.locator(f"{dataName}").hover() # 悬浮操作
# 断言操作
expect(page.locator(f"{dataName}")).to_be_visible()
"""
此处使用后置操作,用来清除测试数据
"""
def test_deleteProject(page: Page, tearDownDeleteData):
"""
删除数据
:param page:
:return:
"""
page.locator("搜索").fill(dataName)
page.keyboard.press('Enter')
page.locator("删除").click()
expect(page.get_by_text("没数据啦")).to_be_visible()
if __name__ == '__main__':
pytest.main(['-vs', './test_pw_0.py'])tearDown,某条用例的后置操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21@pytest.fixture()
def tearDownDeleteData(page: Page):
"""
用例操作完 后置操作
:param page:
:return:
"""
yield
# 清除数据测试操作
page.locator("搜索").fill("xxxx")
# 键盘操作
page.keyboard.press("Enter")
while True:
try:
page.get_by_text("没啦数").wait_for(timeout=3000)
break
except:
page.locator("删除").first.click() # 多个取第一个