PlayWright是由业界大佬微软(Microsoft)开源的端到端 Web 测试和自动化库,可谓是大厂背书,功能满格,虽然作为无头浏览器,该框架的主要作用是测试 Web 应用,但事实上,无头浏览器更多的是用于 Web 抓取目的,也就是爬虫。

PlayWright的安装和使用
pip3 install playwright
安装驱动

playwright可以安装支持的浏览器。运行不带参数的命令将安装默认浏览器

playwright install

可以通过提供参数来安装特定的浏览器

playwright install webkit

查看所有支持的浏览器

playwright install --help

默认会下载chromium内核,firefox以及webkit驱动

demo1,到该页面截图
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://cnblogs.com/luckyletop")
page.screenshot(path="example.png")
browser.close()
playwroght支持api的俩种变体:同步和异步
import asyncio
from playwright.async_api import async_playwright async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.goto("http://cnblogs.com/luckyletop")
print(await page.title())
await browser.close() asyncio.run(main())

定位器

定位器是playwright自动等待和重试能力的核心部分。定位器代表了一种随时在页面上查找元素的方法。

page.get_by_role() 通过显式和隐式可访问性属性进行定位
page.get_by_text() 按文本内容定位
page.get_by_label()通过关联标签的文本定位表单控件
page.get_by_placeholder() 通过占位符定位输入
page.get_by_alt_text() 通过替代文本来定位元素 (一般指图像 具有alt描述图像的属性v)
page.get_by_title()通过元素的标题属性来定位元素
page.get_by_test_id()根据元素的data_testid属性定位元素
page.locator('css=button').click() #通过css
page.locator('xpath=//button').click() #通过xpath

断言

Assertion Description
expect(locator).to_be_checked() Checkbox is checked 复选框被选中
expect(locator).to_be_disabled() Element is disabled 元素被禁用
expect(locator).to_be_editable() Element is editable 元素可编辑
expect(locator).to_be_empty() Container is empty 容器是空的
expect(locator).to_be_enabled() Element is enabled 元素已启用
expect(locator).to_be_focused() Element is focused 元素已聚焦
expect(locator).to_be_hidden() Element is not visible 元素不可见
expect(locator).to_be_visible() Element is visible 元素可见
expect(locator).to_contain_text() Element contains text 元素包含文本
expect(locator).to_have_attribute() Element has a DOM attribute 元素具有DOM属性
expect(locator).to_have_class() Element has a class property 元素具有类属性
expect(locator).to_have_count() List has exact number of children
expect(locator).to_have_css() Element has CSS property 列表具有css
expect(locator).to_have_id() Element has an ID 元素有一个id
expect(locator).to_have_js_property() Element has a JavaScript property
expect(locator).to_have_text() Element matches text 元素与文本匹配
expect(locator).to_have_value() Input has a value
expect(locator).to_have_values() Select has options selected
expect(page).to_have_title() Page has a title 页面有标题
expect(page).to_have_url() Page has a URL 页面有一个url
expect(response).to_be_ok() Response has an OK status 响应状态为ok

pytest-playwright

官方的pytest-playwright插件可以编写端到端的是。塔提供上下文隔离,开箱即用地在多个浏览器配置上运行。它继承了pytest框架,以及支持playwright的一些基础使用

pip install pytest-playwright

安装这个插件后,无需自定义fixture,直接使用内置的前置page即可

import re
from playwright.sync_api import Page, expect def test_has_title(page: Page):
page.goto("https://cnblogs.com/luckyletop") # Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("乐乐")) #说明:直接在用例函数里声明了一个page,格式:def test_has_title(page: Page),即可跳转访问页面
全局配置base_url
pip install pytest-base-url
pytest.ini

[pytest]
base_url = https://cnblogs.com
import re
from playwright.sync_api import Page, expect def test_has_title(page: Page,base_url):
page.goto(base_url+'/luckyletop') #说明:配置后,可直接获取到base_url的,前提是先安装插件,然后在ptest.ini文件里配置

PlayWright安装及使用的更多相关文章

  1. 【web自动化测试】playwright安装失败怎么办

    在安装中,如果没有输入 playwright install, 则不会安装浏览器,运行 playwright codegen 时会报浏览器找不到的错误: "chromium" br ...

  2. 【web自动化测试】Playwright快速入门,5分钟上手

    我喜欢Playwright! 这是微软开源的一款非常强大的自动化工具,再过几年,他很有可能取代Selenium在浏览器自动化的通知地位.使用过一段时间,我没有找到很好的中文资料可以参考,导致很多问题无 ...

  3. playwright结合pytest使用案例

    playwright简介 不愧是宇宙最强,它也是目前为止对ui自动化领域里最好的一个库,在selenium之上,还有对应的异步机制,其他见百度不便在此详叙. 本篇经典案例是对我司的veer产品做ui自 ...

  4. 微软开源 Python 自动化神器 Playwright

    背景 逛博客时候突然看到 Playwright web自动化,感觉很有意思,就翻看了很多博客,简单记录一下. 简介 Playwright是一个强大的Python库,仅用一个API即可自动执行Chrom ...

  5. 新一代自动化测试神器Playwright

    转载请注明出处️ 作者:测试蔡坨坨 原文链接:caituotuo.top/4bedb73c.html 你好,我是测试蔡坨坨. 说到WebUI自动化测试,首当其冲的当属Selenium,在很长的一段时间 ...

  6. 实例讲解Playwright(一)

    实例讲解Playwright(一) 网址 说明 https://playwright.dev/ 官网首页 https://playwright.dev/python/docs/intro Python ...

  7. 微软自动化测试工具palywright

    前言 我们介绍许多e2e的自动化测试工具 一类是基于 Selenium 的测试框架: robot framework gauge SeleniumBase seldom(我自己维护的) 另一类是基于J ...

  8. playwright自动化项目搭建

    这是关于playwright系列介绍的最后一篇.搭建基于 playwright 的自动化项目. GitHub地址: https://github.com/defnngj/playwright-pro ...

  9. 探索微软开源Python自动化神器Playwright

    相信玩过爬虫的朋友都知道selenium,一个自动化测试的神器工具.写个Python自动化脚本解放双手基本上是常规的操作了,爬虫爬不了的,就用自动化测试凑一凑. 虽然selenium有完备的文档,但也 ...

  10. 微软出品自动化神器Playwright,不用写一行代码(Playwright+Java)系列(一) 之 环境搭建及脚本录制

    一.前言 半年前,偶然在视频号刷到某机构正在直播讲解Playwright框架的使用,就看了一会,感觉还不错,便被种草,就想着自己有时间也可以自己学一下,这一想着就半年多过去了. 读到这,你可能就去百度 ...

随机推荐

  1. 【爬虫】一次爬取某瓣top电影前250的学习记录

    先贴上爬取的脚本: import requests import re for i in range(1,11):     num=(i-1)*25     url=f"https://mo ...

  2. FHQ_Treap学习笔记

    前置芝士(了解即可啦~):C++.BST 二叉搜索树.堆.二叉堆 Treap 的概念 Treap 树堆,即树(Tree)+堆(Heap),是一棵弱平衡的二叉搜索树(BST),能同时满足二叉搜索树与堆的 ...

  3. 《实现领域驱动设计》笔记——DDD入门

    设计不只是感观,设计就是产品的工作方式. 我们的目标应该是创造一个可观测的.可伸缩的.组织良好的软件模型. DDD同时提供了战略上的战术上的建模工具. 我能DDD吗? DDD首先并不是关于技术的,而是 ...

  4. 企业微信获取code

    String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AuthUtil.APPID   + ...

  5. rust程序设计(3)结构体相关概念和疑问

    结构体 // 如何定义结构体 struct User { active: bool, username: String, email: String, sign_in_count: u64, } // ...

  6. 用Python编写 2048 命令行小游戏

    本篇博文围绕使用Python开发热门游戏2048 GAME(命令行版本) 代码未做任何优化(原生且随意).全程以面向过程.MVC的设计思想为主.开发环境是Ubuntu系统下的Pycharm 2048是 ...

  7. STA分析-复制

    1 静态时序分析(Static Timing Analysis)静态时序分析(Static Timing Analysis):静态执行对于数字设计时序的分析,不依赖于施加在输入端口上的激励,验证设计是 ...

  8. 【2020】装了VirtualBox后VMware Workstation无法使用SSH连接Centos的解决方法

    装了个VirtualBox,然后发现无法使用Xshell远程Vmware中的centos了,一开始感觉是虚拟网卡冲突了,发现把VirtualBox的虚拟网卡禁用就可以使用,但是好麻烦啊??每次我特么要 ...

  9. NC65主键含义

    最简单的办法,调用用友的类 import nc.jdbc.framework.generator.SequenceGenerator; IdGenerator idGenerator = new Se ...

  10. zabbix+oracle环境部署

    oracle11安装完成后, 实例名: zbx01 oracle创建zbx01用户:col file_name for a60;set linesize 160;select file_name,ta ...