铜梁旅游网站建设管理,钢丝网片,要网站开发费用短信,网站建设放什么会计科目在当今快节奏的开发环境中#xff0c;测试是软件开发的重要组成部分。 Microsoft Playwright 是一种流行的测试自动化框架#xff0c;允许开发人员为 Web 应用程序编写端到端测试。 Playwright 建立在 Puppeteer 之上#xff0c;这是另一个流行的测试自动化框架。在这篇博文…在当今快节奏的开发环境中测试是软件开发的重要组成部分。 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
打开命令提示符或 PowerShell 窗口。运行以下命令以使用 pip 安装 Playwright
pip install playwright安装Playwright内置浏览器
python -m playwright install安装完成验证
playwright -V2.2 在CentOS7上进行安装
On Node.js
通过运行以下命令安装必要的依赖项
sudo yum install -y libstdc-static libstdc-devel libpng12通过运行以下命令安装 Node.js
sudo yum install -y nodejs通过运行以下命令使用 npm 安装 Playwright
npm install playwright3 示例测试PC和移动设备的Web应用
让我们看一个示例了解如何使用 Playwright 在 PC 和 iPhone 上测试 Web 应用程序。在这个例子中田老师将使用 Python 来编写测试。
在开始编写测试之前我们需要安装必要的包。运行以下命令安装 pytest-playwright 和 pytest 包
pip install pytest-playwright3.1 在PC上测试
import pytest
from playwright.sync_api import Playwright, sync_playwrightpytest.fixture(scopemodule)
def playwright() - Playwright:with sync_playwright() as playwright:yield playwrightdef 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 Domainbrowser.close()
在此测试中田老师将使用 Playwright 启动 Chromium 浏览器的新实例导航到 Example Domain 网站然后检查页面标题是否为“Example Domain”。如果标题匹配则测试通过。如果标题不匹配则测试失败。
3.2 在 iPhone 上测试 Web 应用程序
import pytest
from playwright.sync_api import Playwright, sync_playwrightpytest.fixture(scopemodule)
def playwright() - Playwright:with sync_playwright() as playwright:yield playwrightdef test_web_app_on_iphone(playwright: Playwright):browser playwright.webkit.launch(headlessTrue)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 Domainbrowser.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 应用程序。