Playwright 使用心得
310 字
2 分钟
Playwright 使用心得
- 尽量不要使用
sleep实现等待,改为使用page.waitForTimeout方式,原因是 sleep 有可能导致 playwright 相关的监听事件无法触发,例如page.onResponse有可能不正常触发 - LoadState 的区别
public enum LoadState { LOAD, // The load state indicates that the entire page, including all dependent resources such as images, stylesheets, scripts, and other resources, has finished loading. This state is useful when you want to ensure that all content is fully available before interacting with the page. However, this doesn’t track the ongoing network requests. When the load event is fired, there can still be ongoing network requests in the backend. DOMCONTENTLOADED, // The domcontentloaded state indicates that the DOMContentLoaded event has fired, meaning the HTML document has been completely loaded and parsed. However, that doesn’t mean that all stylesheets and resources are loaded. This can be considered as the initial page load. NETWORKIDLE // Network idle state indicates that there is no ongoing network request of at least 500 milliseconds. This state is beneficial for applications that make multiple asynchronous requests, as it ensures that all network activity has been completed before proceeding. This state is mostly useful in single-page applications.}- 通过 page.route 可以拦截修改返回结果
page.route("**/waybillRoute/skipVerify*", route -> { try { APIResponse originalResponse = route.fetch(); String body = originalResponse.text(); // {"traceId":"08b30764b981490da450fd39fc1529a9","timestamp":"2025-08-01 // 13:22:25.449","success":true,"code":0,"message":"请求成功","detailMessage":null,"result":"0","obj":null} JsonNode jsonNode = JsonUtils.parseJSONObject(body); if (!jsonNode.get("result").asBoolean()) { ObjectNode objectNode = (ObjectNode)jsonNode; objectNode.put("result", true); body = JsonUtils.toJsonStr(jsonNode); log.info("自动登录失败,返回结果: {}, 修改后结果: {}", originalResponse.text(), body); } route.fulfill(new Route.FulfillOptions().setBody(body).setHeaders(originalResponse.headers())); } catch (Exception e) { route.resume(); }});参考资料
文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
相关文章智能推荐
1
JMM
技术分享Java 内存模型详解。包含主内存/工作内存、happens-before、内存屏障、volatile 原理等。
2
JVM GC
技术分享JVM 垃圾回收机制详解。包含 GC 算法、垃圾回收器、内存区域、GC 日志分析等。
3
Spring Cloud Gateway 流量复制
Spring Cloud Gateway 流量复制方案。包含复制场景、实现方式、注意事项等。
4
Mybatis 基础字段自动维护
技术分享感觉 mybatis plus 中的自动填充机制不够灵活优雅, 所以有了这篇文章
5
Drone CI 小试
技术分享每次推送代码到自建的 gitea 之后, 都要手动去 linux 服务器上执行部署脚本, 虽然这个操作一共也就几步(拉取最新代码 → 打包构建 → 生成 docker 镜像 → docker compose 运行), 之前就听说过 Drone CI, 这里假期刚好可以折腾小试一下.
随机文章随机推荐













