playwright - 编剧:如果http状态为500则停止
问题描述
我希望剧作家早点失败,并在一个 http 请求不是200
或时停止302
。
def test_it(live_server):
os.environ['DEBUG']='pw:api'
with sync_playwright() as playwright:
run_test_id(live_server, playwright)
def run_test_id(live_server, playwright):
browser = playwright.webkit.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto(live_server.url)
page.click("text=Create Test Data")
不幸的是page.click()
返回无,所以我不知道如何检查点击的 http 状态。
如何在 Playwright 中实现“异常 http 状态失败”?
解决方案
一个选项是使用page.on()
:
def handle_response(response):
print(f"{response.status}")
if response.status == 500:
exit(1)
page.on("response", handle_request)
但是,这不仅会检查单击后的状态代码,还会检查请求的任何响应,例如,如果您page.goto()
返回 500,它也会在此处停止。
(我更习惯用 JS 编写剧本,所以我的例子可能有点偏离。)
推荐阅读
- python - 如何在一行中发送所有这些消息(discord.py)?
- nmap - 起点 Hackthebox 错误“您的端口规格不合法”
- r - 在 R 中变换和可视化相似度矩阵
- html - 如何相对于其中的文本量增加框(边框)的高度?
- websocket - 当我打开一个 javascript alert() 大约 30 秒时,“Websocket 已经处于关闭或关闭状态”
- php - Laravel 本地化与路由资源参数混淆
- spring-batch - 如何在 SpringBatch 中使用 SimpleBinaryBufferedReaderFactory 以字节为单位读取数据
- r - 使用嵌套的 foreach 循环创建数据框列表
- java - Java Async HttpClient 请求似乎阻塞了主线程?
- javascript - jquery ajaxComplete 在 axios 中的等价物