首页 > 编程语言 >Playwright 源码 BrowserType

Playwright 源码 BrowserType

时间:2024-09-06 10:13:37浏览次数:9  
标签:playwright 浏览器 BrowserType 源码 Playwright com microsoft impl

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.

标签:playwright,浏览器,BrowserType,源码,Playwright,com,microsoft,impl
From: https://www.cnblogs.com/huajianketang/p/18399732

相关文章