playwright对谷歌插件进行自动化测试

环境

python 3.8.9
pytest 7.4.0

场景

浏览器自动导入谷歌插件,并且对插件功能进行测试

相关代码

  • conftest.py文件中fixture得编写
import yaml
import pytest
import os
from playwright.sync_api import BrowserType
from typing import Dict, Generator
import requests
import logging


# Add command line arguments --env
def pytest_addoption(parser):
    parser.addoption("--enviroment", action="store", default="test", choices=["test"],
                     help="choose environment, default is test")                     
@pytest.fixture(scope='session', autouse=True)
def env(request):
    '''
    read config file
    '''
    config_path = os.path.join(request.config.rootdir, "config", request.config.getoption('--enviroment'), "env.yaml")
    with open(config_path) as f:
        env_config = yaml.load(f.read(), Loader=yaml.SafeLoader)
    return env_config
    
    
@pytest.fixture()
def context_for_chrome_extension(
        browser_type: BrowserType,
        browser_context_args: Dict,
        env
):
    # path_to_extension = "/Users/m2m_test/m2m/m2m-chrome-extension/public"
    path_to_extension = env["path_to_extension"]
    # chrome extension must run in Chrome / Chromium launched with a persistent context.
    browsercontext = browser_type.launch_persistent_context(
        "",
        **{
            **browser_context_args,
            # **browser_type_launch_args,
            "headless": False,
            "args": [f"--disable-extensions-except={path_to_extension}",
                     # "--headless=new",   # run headless
                     f"--load-extension={path_to_extension}"]
        }
    )
    logging.info("create context success")
    yield browsercontext
    browsercontext.close()

@pytest.fixture()
def extension_id(context_for_chrome_extension) -> Generator[str, None, None]:
    '''
    get extension id
    '''
    # for manifest v2:
    # background = context.background_pages[0]
    # if not background:
    #     background = context.wait_for_event("backgroundpage")

    # for manifest v3:
    # if can not get servie wokers, wait 1 seconds
    # page = context_for_chrome_extension.new_page() 
    # page.wait_for_timeout(1000)
    background = context_for_chrome_extension.service_workers[0]
    # page.close()
    if not background:
        background = context_for_chrome_extension.wait_for_event("serviceworker")

    extension_id = background.url.split("/")[2]
    logging.info(f"extension id: {extension_id}")
    yield extension_id
  • test_case.py测试文件中代码的编写
from playwright.sync_api import expect, Page

def test_popup_page(page: Page, extension_id: str) -> None:
    page.goto(f"chrome-extension://{extension_id}/popup.html")
    page.goto(f"chrome-extension://{extension_id}/index.html")
    expect(page.locator("body")).to_have_text("****")
  • 如果对页面元素进行封装,则page_object.py中
class PageObject:
    def __init__(self, context, env):
        self.page = context.new_page()
        ·····
     # 处理方法
    def function_one(self):
    ····

test_case.py测试文件中

from page_object import PageObject
import pytest

@pytest.mark.chrome_ex     # chrome_ex register in pytest.ini file
def test_popup_page(context_for_chrome_extension, env, extension_id: str) -> None:
    obj = PageObject(context_for_chrome_extension, env, extension_id)
    ·····

你可能感兴趣的:(playwright,python,经验分享,python,playwright,自动化测试)