在当今快节奏的开发环境中,测试是软件开发的重要组成部分。 Microsoft Playwright 是一种流行的测试自动化框架,允许开发人员为 Web 应用程序编写端到端测试。 Playwright 建立在 Puppeteer 之上,这是另一个流行的测试自动化框架。在这篇博文中,田老师将探讨什么是 Playwright,如何在 Windows 11 和 CentOS 7 上安装它,并提供一个示例来说明如何使用它在 PC 和 iPhone 上测试 Web 应用程序。

1 What is Playwright?

Microsoft Playwright 是一个跨浏览器的测试自动化框架,允许开发人员使用 Python、JavaScript 和其他编程语言为 Web 应用程序编写测试。 Playwright 建立在 Puppeteer 之上,它支持所有主要的网络浏览器,包括 Chrome、Firefox 和 Safari。 Playwright 提供了一个高级 API,可以简化为 Web 应用程序编写测试的过程。

2 如何在 Windows 11 和 CentOS 7 上安装 Playwright

在 Windows 11 和 CentOS 7 上安装 Playwright 是一个简单的过程。以下是在这些操作系统上安装 Playwright 的步骤,特别说明:因为Windows和CentOS安装过程基本一致, 在CentOS我们用Node.js安装。 实际上,田老师还是觉得Node.js的Playwright更好用些。 但是Python流行度高呀~!

2.1 在 Windows 11 上安装 Playwright

On Python

  1. 打开命令提示符或 PowerShell 窗口。
  2. 运行以下命令以使用 pip 安装 Playwright:
pip install playwright
  1. 安装Playwright内置浏览器
python -m playwright install
  1. 安装完成验证
playwright -V

2.2 在CentOS7上进行安装

On Node.js

  1. 通过运行以下命令安装必要的依赖项:
sudo yum install -y libstdc++-static libstdc++-devel libpng12
  1. 通过运行以下命令安装 Node.js:
sudo yum install -y nodejs
  1. 通过运行以下命令使用 npm 安装 Playwright:
npm install playwright

3 示例:测试PC和移动设备的Web应用

让我们看一个示例,了解如何使用 Playwright 在 PC 和 iPhone 上测试 Web 应用程序。在这个例子中,田老师将使用 Python 来编写测试。

在开始编写测试之前,我们需要安装必要的包。运行以下命令安装 pytest-playwrightpytest 包:

pip install pytest-playwright

3.1 在PC上测试

import pytest
from playwright.sync_api import Playwright, sync_playwright

@pytest.fixture(scope="module")
def playwright() -> Playwright:
    with sync_playwright() as playwright:
        yield playwright

def test_web_app_on_pc(playwright: Playwright):
    browser = playwright.chromium.launch()
    page = browser.new_page()
    page.goto("https://www.example.com")
    assert page.title() == "Example Domain"
    browser.close()

在此测试中,田老师将使用 Playwright 启动 Chromium 浏览器的新实例,导航到 Example Domain 网站,然后检查页面标题是否为“Example Domain”。如果标题匹配,则测试通过。如果标题不匹配,则测试失败。

3.2 在 iPhone 上测试 Web 应用程序

import pytest
from playwright.sync_api import Playwright, sync_playwright

@pytest.fixture(scope="module")
def playwright() -> Playwright:
    with sync_playwright() as playwright:
        yield playwright

def test_web_app_on_iphone(playwright: Playwright):
    browser = playwright.webkit.launch(headless=True)
    context = browser.new_context(
        **{"viewport": {"width": 375, "height": 667}, "user_agent": "iPhone"}
    )
    page = context.new_page()
    page.goto("https://www.example.com")
    assert page.title() == "Example Domain"
    browser.close()

在此测试中,田老师使用 Playwright 启动一个新的 WebKit 浏览器实例,创建一个视口大小为 375x667(这是 iPhone 屏幕的大小)的新上下文,将用户代理设置为“iPhone”,导航到Example Domain 网站,然后检查页面的标题是否为“Example Domain”。如果标题匹配,则测试通过。如果标题不匹配,则测试失败。

如果要执行上面两段代码,直接保存代码比如test_wep_app.py 然后执行 pytest test_web_app.py 即可。

4 结论

Microsoft Playwright 是一个功能强大的测试自动化框架,可简化为 Web 应用程序编写端到端测试的过程。借助 Playwright,您可以使用 Python、JavaScript 和其他编程语言编写测试,并且可以在所有主要 Web 浏览器上测试您的 Web 应用程序。在这个博文中,田老师介绍了 Playwright 的基础知识,并向您展示了如何在 Windows 11 和 CentOS 7 上安装它。田老师还演示了如何使用 Playwright 和 Python 在 PC 和 iPhone 上测试 Web 应用程序。