软件测试|web自动化测试神器playwright教程(三十二)

发布时间 2023-08-09 18:47:52作者: 霍格沃兹测试开发学社

在这里插入图片描述

前言

我们之前介绍过,我们在本地安装playwright时,默认会下载chromium,firefox 和 webkit浏览器,当然playwright不只支持下载这3个浏览器,还支持下载chrome、chrome-beta、msedge、msedge-beta、msedge-dev浏览器。

下载浏览器

在之前的教程中,我们介绍过,运行playwright install命令会自动安装chromium,firefox和webkit浏览器,如果我们想要安装其他的浏览器,在安装之前,我们可以运行playwright install --help查看支持的浏览器,示例如下:

playwright install --help

Usage: playwright install [options] [browser...]

ensure browsers necessary for this version of Playwright are installed

Options:
  --with-deps  install system dependencies for browsers
  --dry-run    do not execute installation, only print information
  --force      force reinstall of stable browser channels
  -h, --help   display help for command


Examples:
  - $ install
    Install default browsers.

  - $ install chrome firefox
    Install custom browsers, supports chromium, chrome, chrome-beta, msedge, msedge-beta, msedge-dev, firefox, webkit.

从输出信息来看,支持的浏览器有:chromium, chrome, chrome-beta, msedge, msedge-beta, msedge-dev, firefox, webkit

注:安装指定的浏览器, 如果本机已经安装过了,就不会再安装了

命令如下:

playwright install chrome

playwright install msedge

安装Chrome和edge时,这两个浏览器将被安装在默认位置,具体位置视操作系统而定。

示例如下:

playwright install chrome

Failed to install browsers
Error:
╔═════════════════════════════════════════════════════════════════╗
║ ATTENTION: "chrome" is already installed on the system!         ║
║                                                                 ║
║ "chrome" installation is not hermetic; installing newer version ║
║ requires *removal* of a current installation first.             ║
║                                                                 ║
║ To *uninstall* current version and re-install latest "chrome":  ║
║                                                                 ║
║ - Close all running instances of "chrome", if any               ║
║ - Use "--force" to install browser:                             ║
║                                                                 ║
║     playwright install --force chrome                           ║
║                                                                 ║
║ <3 Playwright Team                                              ║
╚═════════════════════════════════════════════════════════════════╝
--------------------------------------------------------------------------------

playwright install msedge

Failed to install browsers
Error:
╔═════════════════════════════════════════════════════════════════╗
║ ATTENTION: "msedge" is already installed on the system!         ║
║                                                                 ║
║ "msedge" installation is not hermetic; installing newer version ║
║ requires *removal* of a current installation first.             ║
║                                                                 ║
║ To *uninstall* current version and re-install latest "msedge":  ║
║                                                                 ║
║ - Close all running instances of "msedge", if any               ║
║ - Use "--force" to install browser:                             ║
║                                                                 ║
║     playwright install --force msedge                           ║
║                                                                 ║
║ <3 Playwright Team                                              ║
╚═════════════════════════════════════════════════════════════════╝

如上所示,因为本地已经安装了Chrome和Edge浏览器,所以playwright安装会报错,提示浏览器已经安装。

根据报错提示,我们也可以通过运行playwright install --force browser来强制安装浏览器

playwright install --force chrome

Downloading Google Chrome
Installing Google Chrome

ProductVersion   FileVersion      FileName
--------------   -----------      --------
113.0.5672.127   113.0.5672.127   C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

打开指定浏览器

如果不加参数,我们运行chromium.launch() 不带 channel 参数默认打开的是 chromium 浏览器

from playwright.sync_api import sync_playwright


with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False)
    page = browser.new_page()

    page.goto("https://www.baidu.com/")

我们都知道,Chrome和edge都是使用的chromium内核,因此我们只需要再加一个chanel参数即可,如果我们要打开Chrome浏览器,则参数为channel="chrome",示例如下:

from playwright.sync_api import sync_playwright


with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False, channel="chrome")
    page = browser.new_page()

    page.goto("https://www.baidu.com/")
    page.pause()

运行脚本,浏览器界面如下:

在这里插入图片描述

同理,要打开edge浏览器,添加channel="msedge"参数即可,代码如下:

from playwright.sync_api import sync_playwright


with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False, channel="msedge")
    page = browser.new_page()

    page.goto("https://www.baidu.com/")
    page.pause()

运行脚本,打开的浏览器页面如下:
在这里插入图片描述

总结

本文主要介绍了playwright对各浏览器的下载,以及启动不同浏览器的方法,我们需要注意启动Chrome或者edge浏览器时,需要加上chanel参数。