Skip to main content

Plugin for running pyppeteer in pytest.

Project description

pytest-pyppeteer

Test with pyppeteer in pytest.

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

Installation

Requirements

pytest-pyppeteer work with Python >=3.6.

Install pytest-pyppeteer

pip install pytest-pyppeteer

or install latest one:

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

Quickstart

For example, compare the scores of a book and its movie on Douban.

--nptp, --new-pyppeteer-test-project

Create a new pyppeteer test project in the specified path.

pytest --nptp=douban

The directory structure:

├── desc
├── pyproject.toml
└── test_douban.py

1 directory, 2 files

Configuration

desc

Create two files douban_movie.desc and douban_book.desc in desc directory.

[HomePage] is required.

# douban_movie.desc

[HomePage]
#CSS
search_input = '#inp-query'
search_apply = '.inp-btn > input:nth-child(1)'

[SearchResultsPage]
# {} indicates that this part can be replaced by the custom parameter
# CSS
result = '#root > div > div > div > div > div:nth-child({}) > div.item-root a.cover-link'

[DetailPage]
rating = '#interest_sectl > div.rating_wrap.clearbox > div.rating_self.clearfix > strong'
# douban_book.desc

[HomePage]
#CSS
search_input = '#inp-query'
search_apply = '.inp-btn > input:nth-child(1)'

[SearchResultsPage]
# {} indicates that this part can be replaced by the custom parameter
# Xpath
result = '(//*[@class="item-root"])[{}]/a'

[DetailPage]
rating = '#interest_sectl > div > div.rating_self.clearfix > strong'

pyproject.toml

Add target:

[tool.pytest.pyppeteer.targets]
[tool.pytest.pyppeteer.targets.target1]
name = "douban_movie"
base_url = "https://movie.douban.com/"

[tool.pytest.pyppeteer.targets.target2]
name = "douban_book"
base_url = "https://book.douban.com/"

Path executablePath to a Chromium or Chrome executable.

[tool.pytest.pyppeteer.options]
executablePath = "/Applications/Chrome.app/Contents/MacOS/Google Chrome"

Write tests

# test_douban.py

import asyncio
from functools import partial

import pytest
from pytest_pyppeteer.models import Pyppeteer


async def query_rating(target: Pyppeteer, movie_or_book_name: str):
    await target.open(goto_base_url=True)
    await target.input("search_input", text=movie_or_book_name)
    await target.click("search_apply")

    # Into search results page
    target.switch_page("SearchResultsPage")
    # Click the first result
    await target.click("result", custom_parameter=(1,))

    # Into detail page
    target.switch_page("DetailPage")
    rating: str = await target.get_value("rating")
    await target.close()
    return rating


@pytest.mark.parametrize("target", [("target1", "target2")], indirect=True)
async def test_shawshank_rating(target):
    target1, target2 = target
    shawshank_rating = partial(query_rating, movie_or_book_name="肖申克的救赎")

    movie_rating, book_rating = await asyncio.gather(
        shawshank_rating(target1), shawshank_rating(target2)
    )

    assert movie_rating == book_rating

Execute tests screenshot

License

MIT License

Changelog

0.1.1 (2020-08-22)

Added

  • ✨ add target.hover function [c0fe87e]

Fixed

More details refer to CHANGELOG.

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.1.1.tar.gz (12.8 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.1.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest-pyppeteer-0.1.1.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.4

File hashes

Hashes for pytest-pyppeteer-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aff1470f3cb45324096c4fe4d32b2ed1b999dedd61d929234da24f32cb23a7a4
MD5 f8c28a50112bf04b1f73b0bb9a01f651
BLAKE2b-256 41bb317a83808e107e0cf87c0d9fb8e43af011d9bc9c2ce3e3eaad6398d3724d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_pyppeteer-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.4

File hashes

Hashes for pytest_pyppeteer-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 832579f6160afd4e984f7b14488e31e57cb12292287f0577fe961ee12ede4426
MD5 c01bb94f13eb8ef1069fafaee74beb50
BLAKE2b-256 f29f6308788a72309a517410df1b9e0fb64e1e8355ffd3494f9a22f7d62f2864

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