欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用pytest-playwright的原因分析

 更新時(shí)間:2023年03月02日 14:25:39   作者:田辛 | 田豆芽  
pytest-playwright 是一個(gè) Python 包,它允許您使用 Microsoft 的 Playwright 庫(kù)在 Python 項(xiàng)目中進(jìn)行端到端測(cè)試,這篇文章主要介紹了Python為什么使用pytest-playwright,需要的朋友可以參考下

pytest-playwright 是一個(gè) Python 包,它允許您使用 Microsoft 的 Playwright 庫(kù)在 Python 項(xiàng)目中進(jìn)行端到端測(cè)試。

在這篇博客中,田辛老師將向您介紹 pytest-playwright,演示如何安裝它,并舉例說(shuō)明如何在您的 Python 項(xiàng)目中使用它。

1 用playwright能不能不用這個(gè)包?

首先田辛老師強(qiáng)調(diào),如果你不想使用 pytest-playwright,你仍然可以在你的 Python 項(xiàng)目中使用 Playwright。只不過(guò)需要一些額外的配置。 我們會(huì)在下次博客中介紹如何PyUnit+playwright。 下面的代碼是一個(gè)單純的playwright的例子

from playwright.sync_api import Playwright, sync_playwright_with_browsers

with sync_playwright_with_browsers() as playwright:
    browser = playwright.chromium.launch(headless=False)
    page = browser.new_page()
    page.goto('https://www.baidu.com')
    browser.close()

此代碼使用 sync_playwright_with_browsers() 函數(shù)啟動(dòng) Playwright 實(shí)例,啟動(dòng) Chromium 瀏覽器,導(dǎo)航至 Google 主頁(yè),然后關(guān)閉瀏覽器。只不過(guò)Python不會(huì)識(shí)別它是一段自動(dòng)化測(cè)試代碼, 只是當(dāng)成一段普通的Python程序去運(yùn)行。

2 安裝

安裝方法其實(shí)田辛老師在前兩天的文檔里面提過(guò),通過(guò)pip進(jìn)行安裝:
pip install pytest-playwright

3 代碼和文檔

田辛老師還是希望大家去看原始文檔的,所以給出如下鏈接:

pytest-playwright 的官方 Github 存儲(chǔ)庫(kù):
https://github.com/pytest-playwright/pytest-playwright 。
在這里您可以找到源代碼、文檔、問(wèn)題跟蹤器和與包相關(guān)的其他資源。

pytest-playwright 的官方文檔托管在 Read the Docs:
https://pytest-playwright.readthedocs.io/en/latest/
該文檔包括安裝說(shuō)明、使用示例、配置選項(xiàng)等。

4 示例代碼

以下是如何使用 pytest-playwright 測(cè)試一個(gè)簡(jiǎn)單網(wǎng)站的示例:

import pytest  
from playwright.sync_api import Playwright, sync_playwright  
@pytest.fixture(scope='module')  
def playwright() -> Playwright:  
    with sync_playwright() as playwright:  
        yield playwright  
@pytest.fixture(scope='module')  
def browser(playwright: Playwright):  
    browser = playwright.chromium.launch(headless=False)  
    yield browser  
    browser.close()  
@pytest.fixture(scope='module')  
def page(browser):  
    page = browser.new_page()  
    yield page  
    page.close()  
def test_baidu_homepage(page):  
    page.goto('https://www.baidu.com')  
    assert page.title() == '百度一下,你就知道'

以上的代碼使用, 創(chuàng)建一個(gè) Playwright 實(shí)例,啟動(dòng)一個(gè) Chromium 瀏覽器,并創(chuàng)建一個(gè)新頁(yè)面。然后使用 test_baidu_homepage 方法使用 page fixture 導(dǎo)航到網(wǎng)站主頁(yè)并檢查頁(yè)面標(biāo)題。

要使用 pytest-playwright 運(yùn)行此測(cè)試,請(qǐng)將代碼保存到名為 test_baidu.py 的文件中,然后從命令行運(yùn)行以下命令:

pytest test_google.py

另外這個(gè)代碼中,田辛老師故意用到了yield的機(jī)制, 如果對(duì)yield不熟悉的同學(xué)可以嘗試閱讀之前田老師寫(xiě)的這篇文章:【Python】一篇文章讀懂yield基本用法

5 結(jié)論

pytest-playwright 是一個(gè)強(qiáng)大且易于使用的工具,用于在 Python 中自動(dòng)化瀏覽器測(cè)試。憑借其直觀(guān)的語(yǔ)法、豐富的功能集和大量的文檔,它是任何希望改進(jìn)其測(cè)試工作流程的人的絕佳選擇。田辛老師要提醒的是, playwright的使用不一定非使用pytest-playwright, 明天我們會(huì)來(lái)看看pyunit怎么使用playwright。 雖然麻煩一點(diǎn),但是田辛老師想說(shuō),作為測(cè)試人員提升的一個(gè)重要邏輯就是:不要對(duì)任何技術(shù)產(chǎn)生路徑依賴(lài)。

到此這篇關(guān)于Python使用pytest-playwright的原因分析的文章就介紹到這了,更多相關(guān)python使用pytest-playwright內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論