最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python中playwright結(jié)合pytest執(zhí)行用例的實(shí)現(xiàn)

 更新時(shí)間:2021年12月12日 10:09:07   作者:一名小測(cè)試  
本文主要介紹了python中playwright結(jié)合pytest執(zhí)行用例的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

playwright結(jié)合Pytest為您的 Web 應(yīng)用程序編寫(xiě)端到端的測(cè)試。

安裝pytest插件

C:\Users\lifeng01>pip install pytest-playwright
Collecting pytest-playwright
  Using cached pytest_playwright-0.2.2-py3-none-any.whl (9.8 kB)
Requirement already satisfied: pytest in d:\python\python37\lib\site-packages (from pytest-playwright) (6.2.5)
Requirement already satisfied: playwright>=1.13 in d:\python\python37\lib\site-packages (from pytest-playwright) (1.17.0)
Requirement already satisfied: python-slugify in d:\python\python37\lib\site-packages (from pytest-playwright) (5.0.2)
Requirement already satisfied: pytest-base-url in d:\python\python37\lib\site-packages (from pytest-playwright) (1.4.2)
Requirement already satisfied: typing-extensions in d:\python\python37\lib\site-packages (from playwright>=1.13->pytest-playwright) (3.7.4.3)
Requirement already satisfied: pyee>=8.0.1 in d:\python\python37\lib\site-packages (from playwright>=1.13->pytest-playwright) (8.2.2)
Requirement already satisfied: websockets>=8.1 in d:\python\python37\lib\site-packages (from playwright>=1.13->pytest-playwright) (9.1)
Requirement already satisfied: greenlet>=1.0.0 in d:\python\python37\lib\site-packages (from playwright>=1.13->pytest-playwright) (1.0.0)
Requirement already satisfied: atomicwrites>=1.0 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (1.3.0)
Requirement already satisfied: attrs>=19.2.0 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (19.3.0)
Requirement already satisfied: py>=1.8.2 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (1.9.0)
Requirement already satisfied: toml in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (0.10.0)
Requirement already satisfied: colorama in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (0.4.3)
Requirement already satisfied: pluggy<2.0,>=0.12 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (0.13.1)
Requirement already satisfied: packaging in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (20.9)
Requirement already satisfied: importlib-metadata>=0.12 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (1.7.0)
Requirement already satisfied: iniconfig in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (1.0.1)
Requirement already satisfied: requests>=2.9 in d:\python\python37\lib\site-packages (from pytest-base-url->pytest-playwright) (2.25.0)
Requirement already satisfied: text-unidecode>=1.3 in d:\python\python37\lib\site-packages (from python-slugify->pytest-playwright) (1.3)
Requirement already satisfied: zipp>=0.5 in d:\python\python37\lib\site-packages (from importlib-metadata>=0.12->pytest->pytest-playwright) (3.1.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\python\python37\lib\site-packages (from requests>=2.9->pytest-base-url->pytest-playwright) (1.26.2)
Requirement already satisfied: certifi>=2017.4.17 in d:\python\python37\lib\site-packages (from requests>=2.9->pytest-base-url->pytest-playwright) (2020.4.5.1)
Requirement already satisfied: idna<3,>=2.5 in d:\python\python37\lib\site-packages (from requests>=2.9->pytest-base-url->pytest-playwright) (2.9)
Requirement already satisfied: chardet<4,>=3.0.2 in d:\python\python37\lib\site-packages (from requests>=2.9->pytest-base-url->pytest-playwright) (3.0.4)
Requirement already satisfied: pyparsing>=2.0.2 in d:\python\python37\lib\site-packages (from packaging->pytest->pytest-playwright) (2.4.7)
Installing collected packages: pytest-playwright
Successfully installed pytest-playwright-0.2.2

編寫(xiě)測(cè)試用例

import pytest
from playwright.sync_api import Page
 
 
def test_baidu_com(page: Page):
 
    page.goto("https://www.baidu.com")
    # xpath定位輸入框,輸入文本內(nèi)容
    page.fill('//*[@id="kw"]', "秦時(shí)明月")
    # css點(diǎn)位點(diǎn)擊“百度一下”按鈕
    page.click('#su')
    # 打印title名稱
    print(page.title)
 
 
if __name__ == '__main__':
    pytest.main(["-v", "test_baidu_example.py"])

名詞解釋:

test_baidu_com(page: Page):自動(dòng)完成配置類型 ( page是pytest_playwright插件提供的一個(gè)夾函數(shù),page指定到類型為Page,Page是playwright庫(kù)提供的一個(gè)類,page指定類型為Page就可以調(diào)用Page類中的一些列方法 )

命令行輸入命令執(zhí)行:

PS F:\project_gitee\Test\playwrightProject\cases> pytest --browser chromium --video on
================================================================================== test session starts ==================================================================================
platform win32 -- Python 3.7.7, pytest-6.2.5, py-1.9.0, pluggy-0.13.1
rootdir: F:\project_gitee\Test\playwrightProject\cases
plugins: allure-pytest-2.9.43, anyio-3.3.4, base-url-1.4.2, clarity-1.0.1, cov-2.8.1, emoji-0.2.0, forked-1.3.0, html-3.1.1, lazy-fixture-0.6.3, metadata-1.11.0, pikachu-0.1.0, playwrigh
t-0.2.2, rerunfailures-9.1.1, xdist-2.2.1
collected 1 item                                                            
 
test_baidu_example.py .                                                                                                                                                           [100%]
 
=================================================================================== 1 passed in 1.97s ===================================================================================

命令行常用基礎(chǔ)命令:

# 運(yùn)行測(cè)試
pytest 
 
# 開(kāi)啟頁(yè)面運(yùn)行
pytest --headed
 
# 指定瀏覽器運(yùn)行
pytest --browser firefox
 
# 開(kāi)啟頁(yè)面運(yùn)行并指定瀏覽器運(yùn)行
pytest --headed --browser firefox
 
# 指定兩個(gè)瀏覽器運(yùn)行,優(yōu)先運(yùn)行火狐再運(yùn)行谷歌
pytest --browser firefox --browser chromium
 
# 使用瀏覽器頻道,支持谷歌瀏覽器(我的電腦其他瀏覽器是報(bào)錯(cuò)的)
pytest --browser-channel chromium
 
# 用慢動(dòng)作運(yùn)行測(cè)試
pytest --slowmo 100
 
# 指定谷歌瀏覽器,開(kāi)啟記錄每次測(cè)試的軌跡(生成一個(gè).zip包,存放在test-results文件夾中)
pytest --browser chromium --tracing on(on:開(kāi);off:關(guān))
 
# 指定谷歌瀏覽器,開(kāi)啟每次測(cè)試錄制視頻(生成一個(gè).webm包,存放在test-results文件夾中)
pytest --browser chromium --video on(on:開(kāi);off:關(guān))
 
# 指定谷歌瀏覽器,開(kāi)啟每次測(cè)試后是否自動(dòng)截屏(生成一個(gè).png圖片,存放在test-results文件夾中)
pytest --browser chromium --screenshot on

通過(guò)瀏覽器跳過(guò)測(cè)試:

import pytest
from playwright.sync_api import Page
 
 
@pytest.mark.skip_browser("firefox")
def test_baidu_com(page: Page):
 
    page.goto("https://www.baidu.com")
    # xpath定位輸入框,輸入文本內(nèi)容
    page.fill('//*[@id="kw"]', "秦時(shí)明月")
    # css點(diǎn)位點(diǎn)擊“百度一下”按鈕
    page.click('#su')
    # 打印title名稱
    print(page.title)
 
 
if __name__ == '__main__':
    pytest.main(["-v", "test_baidu_example.py"])

在特定瀏覽器上運(yùn)行:

import pytest
from playwright.sync_api import Page
 
 
@pytest.mark.only_browser("chromium")
def test_baidu_com(page: Page):
 
    page.goto("https://www.baidu.com")
    # xpath定位輸入框,輸入文本內(nèi)容
    page.fill('//*[@id="kw"]', "秦時(shí)明月")
    # css點(diǎn)位點(diǎn)擊“百度一下”按鈕
    page.click('#su')
    # 打印title名稱
    print(page.title)
 
 
if __name__ == '__main__':
    pytest.main(["-v", "test_baidu_example.py"])

忽略 HTTPS 錯(cuò)誤和設(shè)置自定義視口大小

第一種是寫(xiě)在conftest.py中,然后直接運(yùn)行測(cè)試用例:

# conftest.py
 
import pytest
 
 
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
    return {
        **browser_context_args,
        "ignore_https_errors": True,
    }
 
 
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
    return {
        **browser_context_args,
        "viewport": {
            "width": 1920,
            "height": 1080,
        }
    }

測(cè)試用例寫(xiě)法如下(最好這樣寫(xiě),不然運(yùn)行就報(bào)錯(cuò),作者已采坑):

import pytest
from playwright.sync_api import Page
 
 
def test_example(page: Page):
    page.goto("http://www.baidu.com")
    assert page.title() == "百度一下,你就知道"
 
    page.close()
 
 
if __name__ == '__main__':
    pytest.main(["-v", "test_example.py"])

命令行運(yùn)行測(cè)試用例:

PS F:\project_gitee\Test\playwrightProject\cases> pytest --headed --browser chromium
================================================================================== test session starts ==================================================================================
platform win32 -- Python 3.7.7, pytest-6.2.5, py-1.9.0, pluggy-0.13.1
rootdir: F:\project_gitee\Test\playwrightProject\cases
plugins: allure-pytest-2.9.43, anyio-3.3.4, asyncio-0.16.0, base-url-1.4.2, clarity-1.0.1, cov-2.8.1, emoji-0.2.0, forked-1.3.0, html-3.1.1, lazy-fixture-0.6.3, metadata-1.11.0, pikachu-
0.1.0, playwright-0.2.2, rerunfailures-9.1.1, xdist-2.2.1
collected 1 item                                                                                                                                                                         
 
test_example.py .                                                                                                                                                                  [100%]
 
=================================================================================== 1 passed in 2.39s ===================================================================================

第二種是直接在用例中增加指定參數(shù)即可:

import pytest
from playwright.sync_api import sync_playwright
 
 
def test_example():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)
        page = browser.new_context(
            ignore_https_errors=True,
            viewport={
                "width": 1920,
                "height": 1040,
            }
        )
        page = page.new_page()
        page.goto("http://www.baidu.com")
        assert page.title() == "百度一下,你就知道"
        browser.close()
 
 
if __name__ == '__main__':
    pytest.main(["-v", "test_example.py"])

名詞解釋:
p.chromium.launch(headless=False):headless=False是開(kāi)啟有頁(yè)面運(yùn)行(默認(rèn)是True,也就是無(wú)頭瀏覽器)

第三種是模擬手機(jī)瀏覽器

# conftest.py
 
import pytest
 
 
@pytest.fixture(scope="session")
def browser_context_args(browser_context_args, playwright):
    iphone_11 = playwright.devices['iPhone 11 Pro']
    return {
        **browser_context_args,
        **iphone_11,
    }

測(cè)試用例寫(xiě)法如下(最好這樣寫(xiě),不然運(yùn)行就報(bào)錯(cuò),作者已采坑):

import pytest
from playwright.sync_api import Page
 
 
def test_example(page: Page):
    page.goto("http://www.baidu.com")
    assert page.title() == "百度一下,你就知道"
 
    page.close()
 
 
if __name__ == '__main__':
    pytest.main(["-v", "test_example.py"])

命令行運(yùn)行測(cè)試用例:

PS F:\project_gitee\Test\playwrightProject\cases> pytest --headed --browser chromium
================================================================================== test session starts ==================================================================================
platform win32 -- Python 3.7.7, pytest-6.2.5, py-1.9.0, pluggy-0.13.1
rootdir: F:\project_gitee\Test\playwrightProject\cases
plugins: allure-pytest-2.9.43, anyio-3.3.4, asyncio-0.16.0, base-url-1.4.2, clarity-1.0.1, cov-2.8.1, emoji-0.2.0, forked-1.3.0, html-3.1.1, lazy-fixture-0.6.3, metadata-1.11.0, pikachu-
0.1.0, playwright-0.2.2, rerunfailures-9.1.1, xdist-2.2.1
collected 1 item                                                                                                                                                                         
 
test_example.py F                                                                                                                                                                  [100%]
 
======================================================================================= FAILURES ========================================================================================
________________________________________________________________________________ test_example[chromium] _________________________________________________________________________________
 
page = <Page url='https://m.baidu.com/?from=844b&vit=fps'>
 
    def test_example(page: Page):
        page.goto("http://www.baidu.com")
>       assert page.title() == "百度一下,你就知道"
E       AssertionError: assert '百度一下' == '百度一下,你就知道'
E         - 百度一下,你就知道
E         + 百度一下
 
test_example.py:15: AssertionError
================================================================================ short test summary info ================================================================================
FAILED test_example.py::test_example[chromium] - AssertionError: assert '百度一下' == '百度一下,你就知道'
=================================================================================== 1 failed in 3.14s ===================================================================================

這里運(yùn)行后,打開(kāi)的web頁(yè)面,是一個(gè)手機(jī)形狀的頁(yè)面,仿的是手機(jī)瀏覽器(代碼中指定的是iPhone 11 Pro)

持久上下文

它的意思就是打開(kāi)一個(gè)web頁(yè)面,會(huì)在這個(gè)web頁(yè)面開(kāi)多個(gè)新標(biāo)簽頁(yè)面去執(zhí)行測(cè)試用例。

例如:我有兩個(gè)測(cè)試用例需要執(zhí)行,如果使用持久上下文,就會(huì)打開(kāi)一個(gè)web頁(yè)面,然后web頁(yè)面的第一個(gè)標(biāo)簽頁(yè)運(yùn)行第一個(gè)測(cè)試用例,第二個(gè)標(biāo)簽頁(yè)運(yùn)行第二個(gè)測(cè)試用例,直到把所有測(cè)試用例運(yùn)行完了,關(guān)閉整個(gè)web頁(yè)面,結(jié)束。

當(dāng)然,如果你沒(méi)有使用持久上下文,那就是打開(kāi)web頁(yè)面執(zhí)行第一個(gè)測(cè)試用例,執(zhí)行完第一個(gè)測(cè)試用例關(guān)閉web頁(yè)面,然后再打開(kāi)web頁(yè)面執(zhí)行第二個(gè)測(cè)試用例,執(zhí)行完第二個(gè)測(cè)試用例關(guān)閉web頁(yè)面,結(jié)束。

import pytest
from playwright.sync_api import BrowserType
from typing import Dict
 
@pytest.fixture(scope="session")
def context(
    browser_type: BrowserType,
    browser_type_launch_args: Dict,
    browser_context_args: Dict
):
    context = browser_type.launch_persistent_context("./data", **{
        **browser_type_launch_args,
        **browser_context_args,
        "locale": "de-DE",
    })
 
    yield context
 
    context.close()

名詞解釋:

  • ./data用戶數(shù)據(jù)目錄的路徑,它存儲(chǔ)瀏覽器會(huì)話數(shù)據(jù),如cookie和本地存儲(chǔ)。
  • browser_type_launch_args如果沒(méi)有設(shè)置值,返回的就是一個(gè)空字典;它里面的參數(shù)就是和p.chromium.launch()中的傳參一致的,此處了解下,后面具體介紹。
  • browser_context_args如果沒(méi)有設(shè)置值,返回的就是一個(gè)空字典;它里面的參數(shù)就和context = browser.new_context()中的傳參一致的,此處了解下,后面具體介紹。
  • locale指定用戶區(qū)域設(shè)置,例如' en-GB '、' de-DE '等。

命令行運(yùn)行測(cè)試用例:

PS F:\project_gitee\Test\playwrightProject\cases> pytest --headed --browser chromium
================================================================================== test session starts ==================================================================================
platform win32 -- Python 3.7.7, pytest-6.2.5, py-1.9.0, pluggy-0.13.1
rootdir: F:\project_gitee\Test\playwrightProject\cases
plugins: allure-pytest-2.9.43, anyio-3.3.4, asyncio-0.16.0, base-url-1.4.2, clarity-1.0.1, cov-2.8.1, emoji-0.2.0, forked-1.3.0, html-3.1.1, lazy-fixture-0.6.3, metadata-1.11.0, pikachu-
0.1.0, playwright-0.2.2, rerunfailures-9.1.1, xdist-2.2.1
collected 2 items                                                                                                                                                                        
 
test_baidu_example.py .                                                                                                                                                            [ 50%]
test_example.py .                                                                                                                                                                  [100%]
 
=================================================================================== 2 passed in 3.12s ===================================================================================

必須知曉問(wèn)題:

上述文章中的browser_type_launch_args和browser_context_args都是pytest_playwright插件提供的測(cè)試夾會(huì)話:
展示pytest_playwright插件部分源碼:

@pytest.fixture(scope="session")
def browser_type_launch_args(pytestconfig: Any) -> Dict:
    launch_options = {}
    headed_option = pytestconfig.getoption("--headed")
    if headed_option:
        launch_options["headless"] = False
    browser_channel_option = pytestconfig.getoption("--browser-channel")
    if browser_channel_option:
        launch_options["channel"] = browser_channel_option
    slowmo_option = pytestconfig.getoption("--slowmo")
    if slowmo_option:
        launch_options["slow_mo"] = slowmo_option
    return launch_options
 
 
@pytest.fixture(scope="session")
def browser_context_args(
    pytestconfig: Any,
    playwright: Playwright,
    device: Optional[str],
) -> Dict:
    context_args = {}
    if device:
        context_args.update(playwright.devices[device])
    base_url = pytestconfig.getoption("--base-url")
    if base_url:
        context_args["base_url"] = base_url
 
    video_option = pytestconfig.getoption("--video")
    capture_video = video_option in ["on", "retain-on-failure"]
    if capture_video:
        context_args["record_video_dir"] = artifacts_folder.name
 
    return context_args

?到此這篇關(guān)于python中playwright結(jié)合pytest執(zhí)行用例的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)playwright pytest執(zhí)行用例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • 基于python操作ES實(shí)例詳解

    基于python操作ES實(shí)例詳解

    這篇文章主要介紹了基于python操作ES實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python中集合的內(nèi)置函數(shù)詳解

    Python中集合的內(nèi)置函數(shù)詳解

    這篇文章主要介紹了Python中集合的內(nèi)置函數(shù)詳解,集合在python里面是另外一種對(duì)象,它的特征是在大括號(hào)里面,并且各個(gè)元素是唯一的,今天我們來(lái)看一下其內(nèi)置了哪些函數(shù),需要的朋友可以參考下
    2023-08-08
  • 詳解Python3注釋知識(shí)點(diǎn)

    詳解Python3注釋知識(shí)點(diǎn)

    在本篇文章里小編給大家分享了關(guān)于Python3注釋的相關(guān)知識(shí)點(diǎn)以及用法,需要的朋友們學(xué)習(xí)下。
    2019-02-02
  • python中的extend功能及用法

    python中的extend功能及用法

    Python中的extend()方法用于在列表末尾一次性追加另一個(gè)列表中的多個(gè)值,這篇文章主要介紹了python中的extend功能及用法,需要的朋友可以參考下
    2023-07-07
  • pandas按照列的值排序(某一列或者多列)

    pandas按照列的值排序(某一列或者多列)

    這篇文章主要介紹了pandas按照列的值排序(某一列或者多列),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 徹底卸載Anaconda詳細(xì)教程(超詳細(xì)!)

    徹底卸載Anaconda詳細(xì)教程(超詳細(xì)!)

    這篇文章主要給大家介紹了關(guān)于徹底卸載Anaconda的相關(guān)資料,Anaconda(官方網(wǎng)站)就是可以便捷獲取包且對(duì)包能夠進(jìn)行管理,同時(shí)對(duì)環(huán)境可以統(tǒng)一管理的發(fā)行版本,需要的朋友可以參考下
    2023-11-11
  • python使用__slots__讓你的代碼更加節(jié)省內(nèi)存

    python使用__slots__讓你的代碼更加節(jié)省內(nèi)存

    如果要限制添加的屬性,例如,Student類只允許添加 name、gender和score 這3個(gè)屬性,就可以利用Python的一個(gè)特殊的slots來(lái)實(shí)現(xiàn)。這篇文章主要給大家介紹了關(guān)于python如何使用__slots__讓你的代碼更加節(jié)省內(nèi)存的相關(guān)資料,需要的朋友可以參考下
    2018-09-09
  • Python 使用tempfile包輕松無(wú)痕的運(yùn)行代碼

    Python 使用tempfile包輕松無(wú)痕的運(yùn)行代碼

    大家好,我們知道軟件運(yùn)行過(guò)程中一般會(huì)在指定位置生成臨時(shí)文件,這些資源不要輕易刪除,可能是過(guò)程文件,定時(shí)清理是必要的,今天給大家分享一款工具:tempfile,喜歡本文點(diǎn)贊支持,歡迎收藏學(xué)習(xí)
    2021-11-11
  • PyCharm出現(xiàn)Error:Python?packaging?tool?'setuptools'?not?found解決辦法

    PyCharm出現(xiàn)Error:Python?packaging?tool?'setuptools&apo

    這篇文章主要給大家介紹了關(guān)于PyCharm出現(xiàn)Error:Python?packaging?tool?'setuptools'?not?found的解決辦法,文中通過(guò)圖文及代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • 一篇文章帶你了解Python之Selenium自動(dòng)化爬蟲(chóng)

    一篇文章帶你了解Python之Selenium自動(dòng)化爬蟲(chóng)

    這篇文章主要為大家詳細(xì)介紹了Python之Selenium自動(dòng)化爬蟲(chóng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01

最新評(píng)論

扶余县| 德阳市| 中西区| 山西省| 肃宁县| 南投县| 泉州市| 望城县| 长子县| 青田县| 南平市| 临颍县| 上杭县| 肥东县| 永善县| 治多县| 米易县| 灌阳县| 伊川县| 辉南县| 西吉县| 安庆市| 哈巴河县| 靖宇县| 临潭县| 孝昌县| 宁国市| 罗城| 陆丰市| 当涂县| 义马市| 阆中市| 行唐县| 建宁县| 习水县| 石楼县| 莒南县| 墨竹工卡县| 民丰县| 连云港市| 甘肃省|