Skip to main content

A plugin to run pyppeteer in pytest

Project description


A plugin to run pyppeteer in pytest.

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


You can install pytest-pyppeteer via pip:

$ pip install pytest-pyppeteer

or install the latest one on Github:

pip install git+


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

from dataclasses import dataclass

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("")

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

    await page.waitfor(Elements.first_result)

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

    assert float(rating) >= 9.0




browser fixture

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

But the 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 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 at the same time, then compare them.

import asyncio
from dataclasses import dataclass

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

class BookElements(Elements):
    url = ""

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

class MovieElements(Elements):
    url = ""

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


Command line options


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:

def executable_path(executable_path):
    return executable_path or "path/to/Chrome/or/Chromium"


Run browser in headless mode.


Additional args to pass to the browser instance.

For example, specify a proxy:

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

Or by override the args fixture:

def args(args) -> List[str]:
    return args + [


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 down the pyppeteer operate in milliseconds. Defaults to 0.0.



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

For example, auto-open a DevTools panel:

import asyncio

import pytest

async def test_marker(browser):
    await browser.new_page()
    await asyncio.sleep(2)

options marker


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


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.3.1.tar.gz (8.8 kB view hashes)

Uploaded source

Built Distribution

pytest_pyppeteer-0.3.1-py3-none-any.whl (8.7 kB view hashes)

Uploaded py3

Supported by

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