Python中Playwright模塊進(jìn)行自動(dòng)化測試的實(shí)現(xiàn)
playwright是由微軟開發(fā)的Web UI自動(dòng)化測試工具,支持Node.js、Python、C# 和 Java語言,本文將介紹Python版本的Playwright使用方法。
微軟開源了一個(gè)非常強(qiáng)大的自動(dòng)化項(xiàng)目叫playwright-python,項(xiàng)目地址:https://github.com/microsoft/playwright-python
它支持主流的瀏覽器,包含:Chrome、Firefox、Safari、Microsoft Edge 等,同時(shí)支持以無頭模式、有頭模式運(yùn)行,并提供了同步、異步的 API,可以結(jié)合 Pytest 測試框架使用,并且支持瀏覽器端的自動(dòng)化腳本錄制。
Playwright提供的自動(dòng)化技術(shù)是綠色的、功能強(qiáng)大、可靠且快速,支持Linux、Mac以及Windows操作系統(tǒng)。
官網(wǎng):https://playwright.dev/
從官網(wǎng)的解釋,官方給Playwright定位是一款真正意義上的Web端到端測試工具。
安裝步驟
第 1 步,安裝 playwright-python 依賴庫 (需要注意的是,playwright庫需要依賴Python3.7+以上)
pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade Playwright

第 2 步,安裝主流的瀏覽器驅(qū)動(dòng)
playwright install

如果想查看Playwright支持的功能, 可以直接在命令行輸入:playwright -h

使用Playwright可以無需寫代碼,只需手動(dòng)操作瀏覽器,它會(huì)錄制我們的操作,然后自動(dòng)生成代碼腳本。
錄制腳本
1. 查看錄制命令說明 playwright codegen -h
Usage: playwright codegen [options] [url]
open page and generate code for user actions
Options:
-o, --output <file name> saves the generated script to a file
--target <language> language to generate, one of javascript, playwright-test, python, python-async,
python-pytest, csharp, csharp-mstest, csharp-nunit, java (default: "python")
--save-trace <filename> record a trace for the session and save it to a file
--test-id-attribute <attributeName> use the specified attribute to generate data test ID selectors
-b, --browser <browserType> browser to use, one of cr, chromium, ff, firefox, wk, webkit (default:
"chromium")
--block-service-workers block service workers
--channel <channel> Chromium distribution channel, "chrome", "chrome-beta", "msedge-dev", etc
--color-scheme <scheme> emulate preferred color scheme, "light" or "dark"
--device <deviceName> emulate device, for example "iPhone 11"
--geolocation <coordinates> specify geolocation coordinates, for example "37.819722,-122.478611"
--ignore-https-errors ignore https errors
--load-storage <filename> load context storage state from the file, previously saved with --save-storage
--lang <language> specify language / locale, for example "en-GB"
--proxy-server <proxy> specify proxy server, for example "http://myproxy:3128" or
"socks5://myproxy:8080"
--proxy-bypass <bypass> comma-separated domains to bypass proxy, for example
".com,chromium.org,.domain.com"
--save-har <filename> save HAR file with all network activity at the end
--save-har-glob <glob pattern> filter entries in the HAR by matching url against this glob pattern
--save-storage <filename> save context storage state at the end, for later use with --load-storage
--timezone <time zone> time zone to emulate, for example "Europe/Rome"
--timeout <timeout> timeout for Playwright actions in milliseconds, no timeout by default
--user-agent <ua string> specify user agent string
--viewport-size <size> specify browser viewport size in pixels, for example "1280, 720"
-h, --help display help for command
Examples:
$ codegen
$ codegen --target=python
$ codegen -b webkit https://example.com比如,我要在baidu.com搜索,用chromium驅(qū)動(dòng),將結(jié)果保存為test.py的python文件。文檔默認(rèn)保存到 C:\Users\user\下面
playwright codegen --target python -o 'test.py' -b chromium https://www.baidu.com

到此這篇關(guān)于Python中Playwright模塊進(jìn)行自動(dòng)化測試的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python Playwright自動(dòng)化測試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python爬蟲爬取王者榮耀英雄信息并保存到圖數(shù)據(jù)庫的操作方法
本文介紹了如何使用Python爬蟲技術(shù)從王者榮耀官方獲取英雄信息,并將數(shù)據(jù)保存到圖數(shù)據(jù)庫中,文章詳細(xì)說明了爬取英雄名稱、類型及皮膚名稱的過程,并展示了創(chuàng)建英雄類型節(jié)點(diǎn)和英雄信息節(jié)點(diǎn)的方法2024-09-09
Ubuntu中安裝指定Python版本方法詳解(理論上各版本通用)
現(xiàn)在基于linux的發(fā)行版本有很多,有centos,ubuntu等,一般基于linux的衍生系統(tǒng)至少都安裝了Python2版本,但是現(xiàn)在Python已經(jīng)是3.x版本大行其道了,這篇文章主要給大家介紹了關(guān)于Ubuntu中安裝指定Python版本方法的相關(guān)資料,理論上各版本通用,需要的朋友可以參考下2023-06-06
Python報(bào)錯(cuò)SyntaxError:unexpected?EOF?while?parsing的解決辦法
在運(yùn)行或編寫一個(gè)程序時(shí)常會(huì)遇到錯(cuò)誤異常,這時(shí)python會(huì)給你一個(gè)錯(cuò)誤提示類名,告訴出現(xiàn)了什么樣的問題,下面這篇文章主要給大家介紹了關(guān)于Python報(bào)錯(cuò)SyntaxError:unexpected?EOF?while?parsing的解決辦法,需要的朋友可以參考下2022-07-07
Python中__new__()方法適應(yīng)及注意事項(xiàng)詳解
這篇文章主要介紹了Python中__new__()方法適應(yīng)及注意事項(xiàng)的相關(guān)資料,new()方法是Python中的一個(gè)特殊構(gòu)造方法,用于在創(chuàng)建對(duì)象之前調(diào)用,并負(fù)責(zé)返回類的新實(shí)例,它與init()方法不同,需要的朋友可以參考下2025-03-03
Python運(yùn)行出現(xiàn)DeprecationWarning的問題及解決
這篇文章主要介紹了Python運(yùn)行出現(xiàn)DeprecationWarning的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
利用python實(shí)現(xiàn)xml與數(shù)據(jù)庫讀取轉(zhuǎn)換的方法
這篇文章主要給大家介紹了關(guān)于利用python實(shí)現(xiàn)xml與數(shù)據(jù)庫讀取轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
Python中嵌套序列扁平化的多種實(shí)現(xiàn)方法詳解
在數(shù)據(jù)處理和算法設(shè)計(jì)中,嵌套序列扁平化是解決復(fù)雜問題的關(guān)鍵技術(shù),Python提供了強(qiáng)大的工具來處理嵌套序列,下面小編就來和大家詳細(xì)介紹一下實(shí)現(xiàn)方法吧2025-09-09

