Skip to main content

A plugin to run pyppeteer in pytest.

Project description

pytest-pyppeteer

A plugin to run pyppeteer in pytest.

PyPI - Python Version GitHub issues PyPI Downloads Code style: black Documentation Status 中文博客

Installation

You can install pytest-pyppeteer via pip:

$ pip install pytest-pyppeteer

or install the latest one on Github:

pip install git+https://github.com/luizyao/pytest-pyppeteer.git

Quickstart

For example, The Shawshank Redemption deserves a 9.0 or higher rating on douban.com.

from dataclasses import dataclass

from pytest_pyppeteer.models import Browser


@dataclass(init=False)
class Elements:
    """Collect locators of page objects, no matter XPath or CSS Selector."""

    # query input
    query = "#inp-query"

    # search button
    apply = ".inp-btn > input:nth-child(1)"

    # the first result
    first_result = (
        "#root > div > div > div > div > div:nth-child(1) > div.item-root a.cover-link"
    )

    # rating
    rating = (
        "#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > strong"
    )


async def test_lifetimes(pyppeteer: Browser):
    page = await pyppeteer.new_page()
    await page.goto('https://movie.douban.com/')

    await page.type(Elements.query, "The Shawshank Redemption")
    await page.click(Elements.apply)

    await page.waitfor(Elements.first_result)
    await page.click(Elements.first_result)

    await page.waitfor(Elements.rating)
    rating = await page.get_value(Elements.rating)

    assert float(rating) >= 9.0

quickstart

Usage

Command line options

--executable-path

You can specify the path to a Chromium or Chrome executable. otherwise pytest-pyppeteer will use the default installation location of Chrome in current platform, but now only support win64, win32 and mac platform.

For other platforms, pyppeteer will downloads the recent version of Chromium when called first time. If you don’t prefer this behavior, you can specify an exact path by override this fixture:

@pytest.fixture(scope="session")
def executable_path(executable_path):
    if executable_path is None:
        return "path/to/Chrome/or/Chromium"
    return executable_path

Note:

The default installation location of Chrome in different platform:

  • win64: C:/Program Files/Google/Chrome/Application/chrome.exe
  • win32: C:/Program Files (x86)/Google/Chrome/Application/chrome.exe
  • mac: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

--headless

Run browser in headless mode.

--args

Additional args to pass to the browser instance.

For example, specify a proxy:

$ pytest --args proxy-server "localhost:5555,direct://" --args proxy-bypass-list "192.0.0.1/8;10.0.0.1/8"

Or by override the args fixture:

@pytest.fixture(scope="session")
def args(args) -> List[str]:
    return args + [
        "--proxy-server=localhost:5555,direct://",
        "--proxy-bypass-list=192.0.0.1/8;10.0.0.1/8",
    ]

--window-size

The default browser size is 800*600, you can use this option to change this behavior:

$ pytest --window-size 1200 800

--window-size 0 0 means to starts the browser maximized.

--slow

Slow down the pyppeteer operate in milliseconds. Defaults to 0.0.

No matter selector or xpath

pyppeteer fixture provide a pytest_pyppeteer.models.Browser instance, its usage is almost the same as pyppeteer.browser.Browser, except that it provides a new instance method: new_page(), which is similar to newPage(), but it returns a pytest_pyppeteer.models.Page instead of pyppeteer.page.Page.

pytest_pyppeteer.models.Page’s usage is also the same as pyppeteer.page.Page, but it provides some new instance methods, and override some methods. For example, you can query an element by selector or xpath in just same method query_locator instead of original querySelector and xpath.

You can also get an original Page by pyppeteer.newPage().

options marker

You can override some command line options in the specified test.

For example, auto-open a DevTools panel:

import asyncio

import pytest


@pytest.mark.options(devtools=True)
async def test_marker(pyppeteer):
    await pyppeteer.new_page()
    await asyncio.sleep(2)

options marker

Advanced Usage

Control multiple browsers asynchronously

You can easily to control multiple browsers at the same time.

For example, query the The Shawshank Redemption’s movie and book rating on douban.com at the same time, then compare them.

import asyncio
from dataclasses import dataclass
from typing import TYPE_CHECKING, Callable

import pytest

if TYPE_CHECKING:
    from .models import Browser, Page


@dataclass
class Elements:
    query = "#inp-query"
    apply = ".inp-btn > input:nth-child(1)"


@dataclass
class BookElements(Elements):
    url = "https://book.douban.com/"

    result = '(//*[@class="item-root"])[1]/a'
    rating = "#interest_sectl > div > div.rating_self.clearfix > strong"


@dataclass
class MovieElements(Elements):
    url = "https://movie.douban.com/"

    result = (
        "#root > div > div > div > div > div:nth-child(1) > div.item-root a.cover-link"
    )
    rating = (
        "#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > strong"
    )


async def query_rating(pyppeteer: "Browser", name: str, elements: "Elements"):
    page: Page = await pyppeteer.new_page()

    await page.goto(elements.url)

    await page.type(elements.query, name)
    await page.click(elements.apply)

    await page.waitfor(elements.result)
    await page.click(elements.result)

    await page.waitfor(elements.rating)
    rating = await page.get_value(elements.rating)
    return rating


async def test_multiple_browsers(pyppeteer_factory: "Callable"):
    pyppeteer1 = await pyppeteer_factory()
    pyppeteer2 = await pyppeteer_factory()

    movie_rating, book_rating = await asyncio.gather(
        query_rating(pyppeteer1, "The Shawshank Redemption", MovieElements),
        query_rating(pyppeteer2, "The Shawshank Redemption", BookElements),
    )

    assert movie_rating == book_rating

multiple_browsers

License

Distributed under the terms of the MIT license, pytest-pyppeteer is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-pyppeteer-0.2.5.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_pyppeteer-0.2.5-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest-pyppeteer-0.2.5.tar.gz.

File metadata

  • Download URL: pytest-pyppeteer-0.2.5.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for pytest-pyppeteer-0.2.5.tar.gz
Algorithm Hash digest
SHA256 53c2a34325deb0557c5691c6dd1253aa3147641e1878fcce9fda6cc37efd7c06
MD5 b84069b50b4a9f9a834755c1f87dad96
BLAKE2b-256 a117552e173982c1b8b5d9210223da24cefd4a1a87747e24cb2dcd78d7af8d40

See more details on using hashes here.

File details

Details for the file pytest_pyppeteer-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: pytest_pyppeteer-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for pytest_pyppeteer-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 49eabd1ca591022a406b3ea9a199ce7a02334a4a875fdcf4c8b1c55983e84203
MD5 2c0da981d5f78e771d7e32e0d477aa82
BLAKE2b-256 ce48fc1e67db84d56e7cf6ac0230fc2744eb006f6a6bb9a1fa27119ef84ed0ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page