首页 > 极客资料 博客日记

Playwright 源码 BrowserType

2024-09-06 10:30:04极客资料围观27

文章Playwright 源码 BrowserType分享给大家,欢迎收藏极客之家,专注分享技术知识

playwright-java 的 Browser、BrowserContext、Page 挺好理解的,唯独这厮,就有一丢丢 ……

package com.microsoft.playwright;

/**
 * BrowserType provides methods to launch a specific browser instance or connect to an existing one. 
 *
 * BrowserType 提供启动一个特定浏览器或连接到一个已存在浏览器的方法。
 */
public interface BrowserType {

  // 启动浏览器 返回浏览器实例
  Browser launch(LaunchOptions options);
  
  // 通过 Chrome DevTools Protocol 连接已存在的浏览器 
  Browser connectOverCDP(String endpointURL, ConnectOverCDPOptions options);
  
  // 返回浏览器名 例如 chromium webkit firefox
  String name();
  
  // 返回可执行的浏览器路径
  String executablePath();
 
  // ...
  
}

调试以下代码,可看出:

BrowserType 不是简单直译地表示浏览器类型,而是表示带有浏览器类型和浏览器可执行路径,用来启动或连接已启动浏览器的类。

package demo;
import com.microsoft.playwright.*;

import java.util.Arrays;
import java.util.List;

public class Demo {
  public static void main(String[] args) {
    try(Playwright playwright = Playwright.create()) {
      // 三个 BrowserType
      List<BrowserType> browserTypes = Arrays.asList(
              playwright.chromium(),
              playwright.webkit(),
              playwright.firefox()
      );
      BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions().setHeadless(false);
      // 遍历
      for (BrowserType browserType : browserTypes) {
        // 启动浏览器
        try (Browser browser = browserType.launch(launchOptions)) {
          BrowserContext context = browser.newContext();
          Page page = context.newPage();
          page.navigate("https://localhost/");
        }
      }
    }
  }
}

好了,那上图的 initializer 从何而来?又要到哪里去?

一条重要的线索,渐渐浮出水面 ……

翻过这座山,越过那片海;给下面的方法都加断点,相信,上下而求索的你,终将找到真相 ……

  • com.microsoft.playwright.impl.PlaywrightImpl#createImpl
  • com.microsoft.playwright.impl.ReaderThread#run
  • com.microsoft.playwright.impl.ChannelOwner#runUntil
  • com.microsoft.playwright.impl.Connection#processOneMessage
  • com.microsoft.playwright.impl.Connection#createRemoteObject
  • com.microsoft.playwright.impl.PlaywrightImpl#PlaywrightImpl

Huajianketang would climb every mountain, and swim every ocean, just to find you.


版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云