当前位置: 首页>后端>正文

Playwright学习

瀛︿範浜?Cypress 宸ュ叿鍚庯紝绐佺劧鍙戠幇鐩墠寰堟祦琛岀殑 Playwright锛孭laywright 鎻愪緵浜嗕竴濂楀姛鑳藉己澶т笖鐏垫椿鐨勫伐鍏凤紝浣垮緱鍦ㄤ笉鍚屾祻瑙堝櫒涓婃墽琛岃嚜鍔ㄥ寲娴嬭瘯鍙樺緱鏇村姞瀹规槗鍜屽彲闈狅紝杩欎娇寰?Playwright 鎴愪负寰堝寮€鍙戣€呴€夋嫨鐨勯閫夊伐鍏蜂箣涓€銆?/p>

杩欎箞鍘夊鐨勫伐鍏峰繀椤荤湅鐪?! 浠ユ鏉ヨ褰曡嚜宸卞涔?Playwright 杩囩▼涓殑涓€浜涘唴瀹广€?/p>

鐜鍑嗗

瀛︿範涓€涓柊鐨勫伐鍏锋鏋讹紝蹇呴』浼樺厛闃呰瀹樻柟鏂囨。銆傞槄璇宦燩laywright 瀹樻柟鏂囨。锛岃€冭檻浣跨敤 Python + Playwright 杩涜瀛︿範锛屽垱寤轰竴涓柊鐨?Python铏氭嫙鐜锛堟垜浣跨敤 IDEA 杩涜寮€鍙戯紝杩樺彲閫夋嫨浣跨敤 VS Code / Pycharm 锛夛細

Playwright学习,第1张
Python + Playwright

瀹夎 Playwright锛?/b> 浣跨敤 npm 杩涜瀹夎銆傛墦寮€缁堢骞惰繍琛屼互涓嬪懡浠わ細

npm init playwright@latest

鍛戒护閫斾腑闇€瑕佹垜浠浠ヤ笅鍐呭杩涜閫夋嫨锛?/p>

鍦?TypeScript 鎴?JavaScript 涔嬮棿杩涜閫夋嫨锛堥粯璁や负 TypeScript锛孴ypeScript 鎻愪緵浜嗘洿寮哄ぇ鐨勫紑鍙戝伐鍏峰拰鏇翠赴瀵岀殑鍔熻兘锛屽挨鍏堕€傜敤浜庡ぇ鍨嬮」鐩拰闇€瑕佷弗鏍肩被鍨嬫鏌ョ殑鍦烘櫙锛孞avaScript 鍒欐洿閫傚悎灏忓瀷椤圭洰鍜屽揩閫熷師鍨嬪紑鍙戯級锛?/p>

鎮ㄧ殑娴嬭瘯鏂囦欢澶圭殑鍚嶇О锛堝鏋滄偍鐨勯」鐩腑宸叉湁娴嬭瘯鏂囦欢澶癸紝鍒欓粯璁や负娴嬭瘯鎴?e2e锛夛紱

娣诲姞 GitHub Actions 宸ヤ綔娴佺▼浠ヨ交鏉惧湪 CI 涓婅繍琛屾祴璇曪紱

瀹夎 Playwright 娴忚鍣紙榛樿涓?true锛夈€?/p>

Playwright 灏嗕笅杞芥墍闇€鐨勬祻瑙堝櫒骞跺垱寤轰竴浜涢厤缃枃浠讹紝鍙互鍦╬laywright.config涓坊鍔燩laywright 閰嶇疆锛屽寘鎷慨鏀规兂瑕佽繍琛?Playwright 鐨勬祻瑙堝櫒銆傚鏋滃湪鐜版湁椤圭洰涓繍琛屾祴璇曪紝閭d箞渚濊禆椤瑰皢鐩存帴娣诲姞鍒皃ackage.json銆倀ests鏂囦欢澶瑰寘鍚竴涓熀鏈ず渚嬫祴璇曪紝鍙府鍔╂垜浠紑濮嬫祴璇曘€傛湁鍏虫洿璇︾粏鐨勭ず渚嬶紝鍙煡鐪媡ests-examples鍖呭惈涓烘祴璇曞緟鍔炰簨椤瑰簲鐢ㄧ▼搴忚€岀紪鍐欑殑娴嬭瘯鐨勬枃浠跺す銆?/p>

绠€鍗曟祴璇?/h3>

鐩存帴蹇€熸祴璇晅ests鏂囦欢澶逛腑鐨勫熀鏈ず渚嬶細

npx playwright test

姝ゆ祴璇曞湪鏃犳祻瑙堝櫒妯″紡涓嬭繍琛岋紝杩欐剰鍛崇潃杩愯娴嬭瘯鏃朵笉浼氭墦寮€娴忚鍣ㄣ€傛祴璇曠粨鏋滃拰娴嬭瘯鏃ュ織灏嗘樉绀哄湪缁堢涓€?/p>

娴嬭瘯瀹屾垚鍚庯紝灏嗙敓鎴愪竴涓狧TML 鎶ュ憡鍣紝鍏朵腑鏄剧ず瀹屾暣鐨勬祴璇曟姤鍛婏紝鍏佽鎸夋祻瑙堝櫒銆侀€氳繃鐨勬祴璇曘€佸け璐ョ殑娴嬭瘯銆佽烦杩囩殑娴嬭瘯鍜屼笉绋冲畾娴嬭瘯鏉ヨ繃婊ゆ姤鍛娿€傚彲浠ュ崟鍑绘瘡涓祴璇曞苟鎺㈢储娴嬭瘯鐨勯敊璇互鍙婃祴璇曠殑姣忎釜姝ラ銆傞粯璁ゆ儏鍐典笅锛屽鏋滄煇浜涙祴璇曞け璐ワ紝灏嗚嚜鍔ㄦ墦寮€ HTML 鎶ュ憡銆?/p>

Playwright学习,第2张
HTML鎶ュ憡

UI妯″紡

鍗充负headful 妯″紡锛?/b> 鍦ㄦ妯″紡涓嬶紝鍙互鐪嬪埌娴忚鍣ㄧ晫闈紝灏卞儚姝e父浣跨敤娴忚鍣ㄤ竴鏍枫€傝繖瀵逛簬璋冭瘯鍜岃瀵熸祴璇曟墽琛岃繃绋嬮潪甯告湁甯姪銆?/p>

npx playwright test --ui

Playwright学习,第3张
Playwright UI妯″紡

Playwright + Python

Playwright 鐨勫師鐢熺増鏈槸鐢?TypeScript 缂栧啓鐨勶紝骞舵彁渚涗簡 JavaScript 鍜?TypeScript 鐨?API锛屽洜姝ゅ彲浠ヤ娇鐢ㄨ繖涓ょ璇█鏉ョ紪鍐欐祴璇曡剼鏈€侾laywright 杩樻彁渚涗簡 Python 鐗堟湰鐨?API锛屽彲浠ヤ娇鐢?Python 缂栧啓鍜屾墽琛岃嚜鍔ㄥ寲娴嬭瘯鑴氭湰銆?/p>

浣跨敤Python鐗堟湰锛岄渶pip瀵煎叆鎵€闇€瑕佺殑playwright锛?/p>

pip install playwright

涔嬪悗灏卞彲浠mport鐩稿簲鐨勫playwright妯″潡涓殑鍑芥暟锛屼娇鐢?Python 璇█缂栧啓鐩稿簲鐨勭鍒扮娴嬭瘯銆?/p>

鍚屾涓庡紓姝?/h4>

鍦ㄤ娇鐢?Playwright + Python 杩涜鑷姩鍖栨祴璇曟椂锛屽彲浠ラ€夋嫨鍚屾鎴栧紓姝ョ紪绋嬮鏍硷紝鍏蜂綋鍙栧喅浜庝釜浜哄亸濂藉拰椤圭洰鐨勯渶姹傘€?/p>

鍚屾缂栫▼锛?/b>

鍦ㄥ悓姝ョ紪绋嬩腑锛屼唬鐮佹寜鐓ч『搴忔墽琛岋紝姣忎釜鎿嶄綔閮戒細绛夊緟鍓嶄竴涓搷浣滃畬鎴愬悗鍐嶆墽琛屼笅涓€涓搷浣溿€備娇鐢ㄥ悓姝ョ紪绋嬮鏍肩紪鍐欑殑浠g爜閫氬父鏇存槗浜庣悊瑙e拰璋冭瘯锛屽洜涓哄畠浠槸绾挎€ф墽琛岀殑銆傚湪 Python 涓紝鍙互浣跨敤鍚屾鐨勬柟寮忔潵缂栧啓 Playwright 娴嬭瘯锛屼緥濡備娇鐢╰ime.sleep()鏉ョ瓑寰呴〉闈㈠姞杞藉畬鎴愭垨鎵ц鍏朵粬鎿嶄綔銆?/p>

import time

from playwright.sync_api import sync_playwright #鍚屾

with sync_playwright() as p:

聽聽聽聽browser = p.chromium.launch()

聽聽聽聽page = browser.new_page()

聽聽聽聽page.goto("https://www.baidu.com")

聽聽聽聽time.sleep(2) # 绛夊緟 2 绉?/p>

聽聽聽聽browser.close()

寮傛缂栫▼锛?/b>

鍦ㄥ紓姝ョ紪绋嬩腑锛屼唬鐮佸彲浠ュ苟鍙戞墽琛岋紝涓嶅繀绛夊緟鍓嶄竴涓搷浣滃畬鎴愬氨鍙互鎵ц涓嬩竴涓搷浣溿€傝繖鏍峰彲浠ユ彁楂樼▼搴忕殑鎬ц兘鍜屽搷搴旈€熷害銆備娇鐢ㄥ紓姝ョ紪绋嬮鏍肩紪鍐欑殑浠g爜閫氬父鏇撮€傚悎澶勭悊澶ч噺鐨勫苟鍙戜换鍔°€傚湪 Python 涓紝鍙互浣跨敤asyncio搴撳拰寮傛鐗堟湰鐨?Playwright API 鏉ョ紪鍐欏紓姝ヤ唬鐮併€?/p>

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("https://www.baidu.com") # 鍦ㄥ紓姝ョ紪绋嬩腑锛屽彲浠ヤ娇鐢?await 鏉ョ瓑寰呭紓姝ユ搷浣滃畬鎴?/p>

聽聽聽聽聽聽聽聽await asyncio.sleep(2) # 绛夊緟 2 绉?/p>

聽聽聽聽聽聽聽聽await browser.close()

asyncio.run(main())

await

鍦?Python 鐨勫紓姝ョ紪绋嬩腑锛屼娇鐢?await 鍏抽敭瀛楁槸涓轰簡璁╁綋鍓嶅崗绋嬫殏鍋滄墽琛岋紝绛夊緟鍙︿竴涓崗绋嬪畬鎴愬悗鍐嶇户缁墽琛屻€傚鏋滀笉浣跨敤 await锛岃€屾槸鐩存帴璋冪敤寮傛鍑芥暟锛屼細瀵艰嚧褰撳墠鍗忕▼绔嬪嵆鎵ц涓嬩竴琛屼唬鐮侊紝鑰屼笉绛夊緟寮傛鎿嶄綔瀹屾垚锛岃繖鍙兘浼氬鑷撮敊璇垨涓嶇鍚堥鏈熺殑琛屼负銆?/p>

鍦ㄨ繖涓緥瀛愪腑锛屽鏋滀笉浣跨敤await锛岃€屾槸鐩存帴璋冪敤寮傛鍑芥暟锛屼細瀵艰嚧褰撳墠鍗忕▼绔嬪嵆鎵ц涓嬩竴琛屼唬鐮侊紝鑰屼笉绛夊緟寮傛鎿嶄綔瀹屾垚銆傚叿浣撴潵璇达紝濡傛灉涓嶄娇鐢╝wait鏉ョ瓑寰呭紓姝ユ搷浣減.chromium.launch()銆乥rowser.new_page()鍜宲age.goto()瀹屾垚锛岃€屾槸鐩存帴璋冪敤瀹冧滑锛屽彲鑳戒細瀵艰嚧浠ヤ笅闂锛?/p>

娴忚鍣ㄥ璞″彲鑳藉皻鏈畬鍏ㄥ垵濮嬪寲锛?/b>鍦ㄨ皟鐢╬.chromium.launch()鍚庯紝娴忚鍣ㄥ璞″彲鑳藉皻鏈畬鍏ㄥ垵濮嬪寲锛屽鏋滀笉绛夊緟瀹冨垵濮嬪寲瀹屾垚锛岀洿鎺ヨ皟鐢╞rowser.new_page()锛屽彲鑳戒細瀵艰嚧娴忚鍣ㄥ璞″皻鏈噯澶囧ソ锛屼粠鑰屽紩鍙戦敊璇€?/p>

椤甸潰鍙兘灏氭湭鍔犺浇瀹屾垚锛?/b>鍦ㄨ皟鐢╬age.goto()鍚庯紝椤甸潰鍙兘灏氭湭瀹屽叏鍔犺浇瀹屾垚锛屽鏋滀笉绛夊緟椤甸潰鍔犺浇瀹屾垚锛岀洿鎺ユ墽琛屽悗缁搷浣滐紝鍙兘浼氬鑷撮〉闈㈠皻鏈噯澶囧ソ锛屼粠鑰屽鑷存棤娉曡幏鍙栧埌棰勬湡鐨勯〉闈㈠唴瀹广€?/p>

娴忚鍣ㄥ璞″彲鑳藉皻鏈叧闂細鍦ㄨ皟鐢╞rowser.close()鍚庯紝濡傛灉涓嶇瓑寰呮祻瑙堝櫒瀵硅薄鍏抽棴瀹屾垚锛岀洿鎺ユ墽琛屽悗缁搷浣滐紝鍙兘浼氬鑷存祻瑙堝櫒瀵硅薄灏氭湭瀹屽叏鍏抽棴锛屼粠鑰岄€犳垚璧勬簮娉勬紡鎴栧叾浠栭棶棰樸€?/p>

缁间笂鎵€杩帮紝涓嶄娇鐢╝wait鏉ョ瓑寰呭紓姝ユ搷浣滃畬鎴愶紝鍙兘浼氬鑷寸▼搴忔棤娉曟寜棰勬湡鎵ц锛屽嚭鐜伴敊璇垨涓嶇鍚堥鏈熺殑琛屼负銆傚洜姝わ紝鍦ㄥ紓姝ョ紪绋嬩腑锛屼娇鐢╝wait鏄‘淇濆紓姝ユ搷浣滄寜椤哄簭鎵ц鍜屽畬鎴愮殑鍏抽敭銆?/p>

鎬荤殑鏉ヨ锛屽悓姝ョ紪绋嬫洿鐩磋鏄撴噦锛岃€屽紓姝ョ紪绋嬫洿閫傚悎澶勭悊澶ц妯$殑骞跺彂浠诲姟銆傚湪閫夋嫨缂栫▼椋庢牸鏃讹紝闇€瑕佽€冭檻椤圭洰鐨勯渶姹傘€佸鏉傚害鍜屾€ц兘瑕佹眰銆?/p>

Playwright codegen

Playwright 鐨?codegen 鏄竴涓潪甯稿疄鐢ㄧ殑宸ュ叿锛屽畠鍙互甯姪鎴戜滑鑷姩鐢熸垚鎵ц娴忚鍣ㄦ搷浣滅殑浠g爜銆傝宸ュ叿鍙互鍦ㄦ祻瑙堝櫒涓褰曠敤鎴风殑浜や簰鎿嶄綔锛屽苟灏嗗叾杞崲涓?Playwright 鑴氭湰锛岃繖鏍峰彲浠ュぇ澶х畝鍖栫紪鍐欒嚜鍔ㄥ寲娴嬭瘯鑴氭湰鐨勮繃绋嬨€?/p>

npx playwright codegen <url>

鍛戒护鐢熸垚鐨勪唬鐮佷负 TypeScript 鏄洜涓?Playwright 鏈韩鏄娇鐢?TypeScript 缂栧啓鐨勶紝鍥犳榛樿鎯呭喌涓嬶紝codegen 宸ュ叿鐢熸垚鐨勪唬鐮佷篃鏄?TypeScript銆?/p>

濡傛灉鎯宠鐢熸垚 Python 璇█鐨勪唬鐮侊紝鍙互鍦ㄦ墽琛?codegen 鍛戒护鏃讹紝閫氳繃鎸囧畾--target鍙傛暟鏉ユ寚瀹氱敓鎴愮殑浠g爜璇█銆備緥濡傦細

npx playwright codegen --target=python <url>

杩欐牱灏卞彲浠ョ敓鎴?Python 璇█鐨勪唬鐮佷簡銆傚悓鏃讹紝Playwright 鐨?codegen 宸ュ叿浼氭牴鎹」鐩幆澧冨拰鎵ц涓婁笅鏂囨潵閫夋嫨閫傚綋鐨勮剼鏈瑷€銆傚叿浣撴潵璇达紝濡傛灉椤圭洰涓凡缁忓瓨鍦?TypeScript 鎴?Python 鏂囦欢锛岄偅涔?codegen 宸ュ叿鍙兘浼氶€夋嫨涓庨」鐩腑宸叉湁鏂囦欢鐩稿悓鐨勮剼鏈瑷€銆傝繖鏍峰彲浠ヤ繚鎸侀」鐩殑涓€鑷存€э紝骞跺噺灏戦泦鎴愬拰缁存姢鐨勫鏉傛€с€傚湪鎵ц codegen 宸ュ叿鏃讹紝瀹冧細妫€鏌ュ綋鍓嶇殑鎵ц涓婁笅鏂囥€備緥濡傦紝濡傛灉鍦?TypeScript 鐜涓繍琛?codegen 宸ュ叿锛屽畠鍙兘浼氳嚜鍔ㄩ€夋嫨鐢熸垚 TypeScript 浠g爜銆傚悓鏍峰湴锛屽鏋滃湪 Python 鐜涓繍琛?codegen 宸ュ叿锛屽畠鍙兘浼氳嚜鍔ㄩ€夋嫨鐢熸垚 Python 浠g爜銆?/p>

鍥犱负鎴戠殑椤圭洰鐜涓?Python锛屽綋鎴戠洿鎺ヤ娇鐢?/p>

playwright codegen <url>

鍙互鐩存帴鐢熸垚 Python 璇█鐨勪唬鐮併€?/p>

鍚屾椂锛宑odegen杩樻敮鎸佸緢澶氫笉鍚岀殑鍙傛暟锛屽锛?/p>

--device: 鎸囧畾瑕佹ā鎷熺殑璁惧绫诲瀷锛堜緥濡傦紝iPhone X銆乮Pad銆丟alaxy S5锛夈€?/p>

--geolocation: 鎸囧畾鍦扮悊浣嶇疆鍧愭爣锛屾牸寮忎负绾害鍜岀粡搴︼紝渚嬪37.7749,-122.4194銆?/p>

--permissions: 鎸囧畾瑕佹巿浜堢殑鏉冮檺锛屼緥濡俫eolocation銆乶otifications銆乵idiSysex銆?/p>

--lang: 鎸囧畾瑕佷娇鐢ㄧ殑璇█浠g爜锛屼緥濡俥n-US銆亃h-CN銆?/p>

--proxy: 鎸囧畾浠g悊鏈嶅姟鍣ㄧ殑鍦板潃鍜岀鍙c€?/p>

鍙互鏌ラ槄瀹樻柟鏂囨。浣跨敤杩欎簺鍙傛暟銆?/p>

Playwright聽Insepctor

鍦?Playwright聽Insepctor 涓紝鎴戜滑鍙互鐩存帴鐢熸垚鍖呮嫭 Pytest 鎴栬€卆sync寮傛绛変笉鍚岀被鍨嬬殑浠g爜锛堭煈嶏級锛屼箣鍚庤繕浼氬涔?Pytest + Playwright鐨勫唴瀹广€?/p>

Playwright学习,第4张
閫氳繃妫€鏌ュ櫒鐢熸垚涓嶅悓绫诲瀷鐨勪唬鐮?/div>


https://www.xamrdz.com/backend/3t41936056.html

相关文章: