Web自动化测试之playwright:pages、popup、dialog和frame处理

本文介绍如何使用playwright来处理新页面、frame以及对话框。

新页面窗口

获取浏览器上下文产生的新页面,也就是通过 target="_blank" 链接打开的新窗口。

1
2
3
4
with context.expect_page() as new_page_info:
page.click('a[target="_blank"]')
new_page = new_page_info.value
new_page.wait_for_load_state()

点击【立即注册】会打开一个新的页面:

下面代码实现打开百度,点击登录,进入注册界面

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
from time import sleep

from playwright.sync_api import sync_playwright

class TestDemo():
def setup(self):
playwright = sync_playwright().start()
self.browser = playwright.chromium.launch(headless=False)
self.context = self.browser.new_context()
self.page = self.context.new_page()

def teardown(self):
self.browser.close()

def test_click(self):
self.page.goto("http://www.baidu.com")

self.page.click('id=s-top-loginbtn')

with self.context.expect_page() as new_page_info:
self.page.click('"立即注册"') # Opens a new tab
register_page = new_page_info.value

register_page.wait_for_load_state()
print(register_page.title())
# 注册用户名密码
register_page.fill("id=TANGRAM__PSP_4__userName", "username")
register_page.fill("id=TANGRAM__PSP_4__phone", "12345678")
register_page.close()
sleep(2)

# 登录用户名密码
self.page.fill("id=TANGRAM__PSP_11__userName", "username")
self.page.fill("id=TANGRAM__PSP_11__password", "pwd")
sleep(2)

playwright不像selenium那样在有头模式下可以切换窗口,它默认是无头模式的,直接对对应的页面对象进行操作就可以了。

selenium窗口切换参考 Selenium switch_to方法

弹框处理

页面中的弹框(popup)是很常见的,处理方法和打开新页面类似:

1
2
3
4
5
6
with page.expect_popup() as popup_info:
page.click("#open")
popup = popup_info.value

popup.wait_for_load_state()
print(popup.title())

对话框处理

常见对话框包括alert(), confirm(), prompt()等,默认情况下,Playwright会取消对话框,可以通过在触发对话框之前注册一个对话框句柄:

1
2
page.on("dialog", lambda dialog: dialog.accept())
page.click("button")

dialog测试地址:https://sahitest.com/demo/confirmTest.htm

1
2
3
4
5
6
7
8
def test_dialog(self):
self.page.goto("https://sahitest.com/demo/confirmTest.htm")
self.page.click('[name="b1"]') # 默认取消对话框

# 接受对话框
self.page.on("dialog", lambda dialog: dialog.accept())
self.page.click('[name="b1"]')
sleep(5)

frame切换

获取frame方法如下:

1
2
3
4
5
6
7
# 通过frame的name属性
frame = page.frame('frame-login')
# 通过frame的URL
frame = page.frame(url=r'.*domain.*')
# 通过选择器selector
frame_element_handle = page.query_selector('.frame-class')
frame = frame_element_handle.content_frame()

参考:

  1. https://playwright.dev/python/docs/frames
  2. https://playwright.dev/python/docs/dialogs
--THE END--

本文标题:Web自动化测试之playwright:pages、popup、dialog和frame处理

文章作者:hiyo

文章链接:https://hiyongz.github.io/posts/web-testing-with-playwright-for-dialogs-page-and-popup/

许可协议:本博客文章除特别声明外,均采用CC BY-NC-ND 4.0 许可协议。转载请保留原文链接及作者。

关注微信公众号,及时接收最新技术文章!