Skip to main content

pytest-pyppeteer

A plugin to run pyppeteer in pytest.

PyPI - Python Version GitHub issues PyPI Downloads Code style: black

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


@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(browser):
    page = await browser.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

Fixtures

browser fixture

Provide an pyppeteer.browser.Browser instance with a new method new_page(), like pyppeteer.browser.Browser.newPage(), new_page() could create a pyppeteer.page.Page instance.

But the pyppeteer.page.Page instance created by new_page() has some new methods:

Method Type
query_locator New
waitfor New
click Override
type Override
get_value New

For example, you can query an element by css or xpath in the same method query_locator instead of original querySelector and xpath.

More details check with page.py in the source code.

browser_factory fixture

Provide to create an pyppeteer.browser.Browser instance.

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


@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(browser, name: str, elements: "Elements"):
    page = await browser.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(browser_factory):
    browser1 = await browser_factory()
    browser2 = await browser_factory()

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

    assert movie_rating == book_rating

multiple_browsers

Command line options

--executable-path

You can specify the Chromium or Chrome executable path. Otherwise I will use the default install path of Chrome in current 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):
    return executable_path or "path/to/Chrome/or/Chromium"

--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.

Markers

options

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(browser):
    await browser.new_page()
    await asyncio.sleep(2)

options marker

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.

Metadata

Release files for pytest-pyppeteer 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-pyppeteer 0.3.1
File Size Uploaded
pytest-pyppeteer-0.3.1.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-pyppeteer 0.3.1
File Interpreter ABI Platform
pytest_pyppeteer-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / pytest-pyppeteer-0.3.1.tar.gz

Download URL pytest-pyppeteer-0.3.1.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3ce5e2f7a8bfe0a9eae6d9a3bd00d39ab568b354e554f1278fac8fe168a80565
BLAKE2b-256 checksum
How to use checksums
7e00965a69ac892349978a56ebb4ede82be2540a0219feaaf4054bd87a723a37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1022-azure

Release files / pytest_pyppeteer-0.3.1-py3-none-any.whl

Download URL pytest_pyppeteer-0.3.1-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
afe3d4a0770096f12b5ce5cb24d76890be4aa94b651ca2b20ae13368e335c4de
BLAKE2b-256 checksum
How to use checksums
abb102ed9e68bb28fade10187cbe3bc68967243ab03ddd189bde7e4f76a01475
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1022-azure

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.5

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page