• 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().

  • 相关阅读:
    【工具】Ubuntu开机黑屏、NVIDIA显卡驱动问题
    了解Qt QScreen的geometry ,size
    细数实现全景图VR的几种方式(panorama/cubemap/eac)
    删除类及其对象的属性:delattr()函数
    浅谈Jmeter性能测试流程
    基于SpringBoot的在线试题库系统设计与实现
    洞态在某互联⽹⾦融科技企业的最佳落地实践
    如何新建一个一台交换机下连两个PC的拓扑
    腾讯出品小程序自动化测试框架【Minium】系列(七)测试框架的设计和开发
    后端关卡18 了解JDBC
  • 原文地址:https://blog.csdn.net/Zhongtongyi/article/details/128067773