• Playwright官方文档要点记录(java)


    一、page

    总览:

            The Page class emits various events (described below) which can be handled using any of Node's native EventEmitter methods, such as ononce or removeListener.

    1、page加载时:

    page.onLoad(p -> System.out.println("Page loaded!"));

    2、取消默认事件加载(removeListener):

    案例:

    1. Consumer logRequest = interceptedRequest -> {
    2. System.out.println("A request was made: " + interceptedRequest.url());
    3. };
    4. page.onRequest(logRequest);
    5. // Sometime later...
    6. page.offRequest(logRequest);

    3、page关闭时:

    Page.onClose(handler)

    4、Page.onConsoleMessage(handler)

            Emitted when JavaScript within the page calls one of console API methods, e.g. console.log or console.dir. Also emitted if the page throws an error or a warning.

            The arguments passed into console.log appear as arguments on the event handler.

    要点:获取到浏览器的js输出如log、dir,或者是抛出错误/警告,后执行page.onConsoleMessage里面的代码

    案例:evaluate模拟打印后,触发onconsolemessage内部代码

    1. page.onConsoleMessage(msg -> {
    2. for (int i = 0; i < msg.args().size(); ++i)
    3. System.out.println(i + ": " + msg.args().get(i).jsonValue());
    4. });
    5. page.evaluate("() => console.log('hello', 5, {foo: 'bar'})");

    5、Page.onCrash(handler)

            页面分配内存等导致奔溃后触发,后续操作终止,最好是捕获异常

    1. try {
    2. // Crash might happen during a click.
    3. page.click("button");
    4. // Or while waiting for an event.
    5. page.waitForPopup(() -> {});
    6. } catch (PlaywrightException e) {
    7. // When the page crashes, exception message contains "crash".
    8. }

    6、Page.onDialog

            Emitted when a JavaScript dialog appears, such as alertpromptconfirm or beforeunload. Listener must either Dialog.accept([promptText]) or Dialog.dismiss() the dialog - otherwise the page will freeze waiting for the dialog, and actions like click will never finish.

    要点:执行到如下代码时会一直等待js的dialog事件发生后才进行后续操作

    1. page.onDialog(dialog -> {
    2. dialog.accept();
    3. });

    7、Page.onDownload

    触发下载操作后,执行代码块内语句

    8、Page.onFileChooser(handler)

            Emitted when a file chooser is supposed to appear, such as after clicking the . Playwright can respond to it via setting the input files using FileChooser.setFiles(files[, options]) that can be uploaded after that.

    要点:比如前端点击下载按钮后,触发选择路径下某个文件

    1. page.onFileChooser(fileChooser -> {
    2. fileChooser.setFiles(Paths.get("/tmp/myfile.pdf"));
    3. });

    9、goBack

            Returns the main resource response. In case of multiple redirects, the navigation will resolve with the response of the last redirect. If can not go back, returns null.

            Navigate to the previous page in history.

            返回上一次page加载的页面,存在多个则返回最后一次

    10、goForward

            和goBack类似,不同的是goBack返回的是上一次page,而goForward可以选择

    二、Locator

    1、boundingBox

    类似于通过页面元素的绑定框快捷操作元素,页面静态不发生滚动,则通过鼠标点击不影响

    This method returns the bounding box of the element, or null if the element is not visible. The bounding box is calculated relative to the main frame viewport - which is usually the same as the browser window.

    Scrolling affects the returned bounding box, similarly to Element.getBoundingClientRect. That means x and/or y may be negative.

    Elements from child frames return the bounding box relative to the main frame, unlike the Element.getBoundingClientRect.

    Assuming the page is static, it is safe to use bounding box coordinates to perform input. For example, the following snippet should click the center of the element.

    三、BrowserContext

    1、setDefaultTimeout

    相当于方法返回超时,超过设定时间后抛异常

    注意:

    Page.setDefaultNavigationTimeout()Page.setDefaultTimeout() and BrowserContext.setDefaultNavigationTimeout() take priority over BrowserContext.setDefaultTimeout().

  • 相关阅读:
    实操技巧:私域+公域=全渠道获客
    媒体报道 | 亿美软通以诚信之心 守护信息安全
    怎么用vscode创建工程
    C++:二叉搜索树的原理和模拟实现
    【软件测试】—— 自动化测试之unittest框架
    【负载均衡在线OJ项目日记】项目简介
    Chromium127调试指南 Windows篇 - 安装C++扩展与配置(五)
    Python 文件简单操作
    React Native调用摄像头画面及拍照和保存图片到相册全流程
    通过自动化单元测试的形式守护系统架构
  • 原文地址:https://blog.csdn.net/Zhongtongyi/article/details/128067773