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

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.

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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest-pyppeteer-0.3.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1022-azure

File hashes

Hashes for pytest-pyppeteer-0.3.1.tar.gz
Algorithm Hash digest
SHA256 3ce5e2f7a8bfe0a9eae6d9a3bd00d39ab568b354e554f1278fac8fe168a80565
MD5 763ecfb354db674f57a3bad897121120
BLAKE2b-256 7e00965a69ac892349978a56ebb4ede82be2540a0219feaaf4054bd87a723a37

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_pyppeteer-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.2 Linux/5.13.0-1022-azure

File hashes

Hashes for pytest_pyppeteer-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 afe3d4a0770096f12b5ce5cb24d76890be4aa94b651ca2b20ae13368e335c4de
MD5 c8f3c7a10d2d5f5486da773ba8524daa
BLAKE2b-256 abb102ed9e68bb28fade10187cbe3bc68967243ab03ddd189bde7e4f76a01475

See more details on using hashes here.

Supported by

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