PlayWright 系列1:安装

简介: PlayWright 系列1:安装

大家好,我是阿萨。从今天开始给大家介绍下最新的微软的Web UI 自动化框架的PlayWright. 今天我们先从安装开始。


一. 安装


Playwright是一个端到端测试的框架。Playwright支持所有现代渲染引擎,包括Chromium、WebKit和Firefox。可以在Windows、Linux和macOS上测试,在本地或CI上测试,无头或有头的原生移动模拟器上运行。


Playwright推荐使用官方的Playwright Pytest插件来编写端到端测试。它提供上下文隔离,在多个浏览器配置上运行,开箱即用。

另外,你也可以使用该库,用你喜欢的测试运行器手动编写测试基础设施。

Pytest插件使用Playwright的同步版本,也有一个可通过库访问的异步版本。通过安装Playwright和运行示例测试开始,看看它的运行情况。安装Pytest插件。


pip install pytest-playwright

安装要求的浏览器:

playwright install


二. 创建测试例子


创建一个test_Sarah.py文件在当前文件夹或者子文件夹。复制如下代码。



import refrom playwright.sync_api 
import Page, expect

def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(page: Page): 
page.goto("https://playwright.dev/") 

# Expect a title "to contain" a substring. 
expect(page).to_have_title(re.compile("Playwright")) 

# create a locator
get_started = page.get_by_role("link", name="Get started") 

 # Expect an attribute "to be strictly equal" to the value. 
expect(get_started).to_have_attribute("href", "/docs/intro") 

 # Click the get started link. 
 get_started.click() 
 #  Expects the URL to contain intro. 
expect(page).to_have_url(re.compile(".*intro"))


三. 运行脚本


默认情况下,测试将在chromium上运行。这可以通过CLI选项进行配置。测试是在无头模式下运行的,这意味着在运行测试时,没有浏览器的用户界面会打开。测试的结果和测试日志将显示在终端。


pytest


你学会了吗?如果觉得阿萨的内容对你有帮助,欢迎围观点赞。


相关文章
|
1月前
|
Web App开发 API Python
Playwright系列(8):认识playwright 相关库
Playwright系列(8):认识playwright 相关库
Playwright系列(8):认识playwright 相关库
|
1月前
|
测试技术 Python
Playwright系列(4):录制测试脚本
Playwright系列(4):录制测试脚本
116 0
|
1月前
|
API 开发工具 开发者
Playwright系列(12):调试选择器
Playwright系列(12):调试选择器
|
1月前
|
Python
Python Playwright 打包报错 Please run the following command to download new browsers
Python Playwright 打包报错 Please run the following command to download new browsers
132 0
|
1月前
|
前端开发 测试技术
使用 Playwright 进行元素定位
本文介绍了Playwright在自动化测试和网页爬取中如何定位页面元素。通过CSS选择器、XPath和文本内容等方式,Playwright的`locator`对象允许灵活定位。示例代码展示了使用`query_selector()`、XPath表达式以及`with_text()`方法找到元素并获取其文本。此外,文章还提到了其他内置定位方法,如根据角色、标签文本、占位符等属性进行定位,并提供了相关使用示例。总之,Playwright提供了丰富的元素定位方法,便于高效地进行网页操作。
36 4
|
1月前
|
Web App开发 Linux C++
Playwright系列(7):用VSCode 开始写Playwright 脚本
Playwright系列(7):用VSCode 开始写Playwright 脚本
328 0
Playwright系列(7):用VSCode 开始写Playwright 脚本
|
1月前
|
JavaScript
Playwright系列(4):Trace Viewer
Playwright系列(4):Trace Viewer
Playwright系列(4):Trace Viewer
|
1月前
|
Web App开发 存储 测试技术
Playwright系列(11):Playwright命令行
Playwright系列(11):Playwright命令行
161 0
|
1月前
|
自然语言处理 测试技术 Python
【实测】playwright 学习笔记 - 02
【实测】playwright 学习笔记 - 02
【实测】playwright 学习笔记 - 02
|
10月前
|
JavaScript 前端开发 测试技术
【实测】playwright 学习笔记 - 0001
【实测】playwright 学习笔记 - 0001