Giter VIP home page Giter VIP logo

pytest-playwright-enhanced's Introduction

version codecov docs

pytest-playwright-enhanced

Caution

pytest-playwright-enhanced is in the alpha stage.

pytest-playwright-enhanced is a batteries included pytest plugin for the playwright python bindings that offers extended functionality with a focus on removing boilerplate code for projects that wish to test modern web applications and APIs. pytest-playwright-enhanced plans to offer the following:

  • Automatic, browser management and installation if required (TBD)
  • Improved, human readable APIs that build on top of playwrights API.
  • Improved artifacting and debugability.
  • Asynchronous support.
  • Custom hooks to allow you to plugin and modify behaviour at runtime.
  • Much More...

pytest-playwright-enhanced is currently implementing core plugin functionality, the main enhancements will follow shortly.

pytest-playwright-enhanced right now treats browsers, contexts and pages at a function level. This allows easily per test overrides at the small cost of slightly less performance creating browsers, for now it is worth it as it offers much more flexibility and user defined configurations.


Quick Start

Quickly get running by doing the following:

  • pip install pytest-playwright-enhanced
  • pytest <posargs> --acquire-binaries=with-deps

Overriding Browser and Context args

There are two methods for overwriting both browser and context arguments at runtime. These are:

import pytest
import typing

@pytest.mark.browser_kwargs(env={}, timeout=15)
@pytest.mark.context_kwargs()
def test_my_app(pw_page):
    # This page will have a preconfigured browser and context from the 
    # marker arguments.
    ...


def my_function(config: pytest.Config) -> dict[typing.Any, typing.Any]:
    return {
        # custom options.
        # inspect CLI if you need via config.
    }

# if you need to calculate the args at runtime, later use:
@pytest.mark.browser_kwargs(callback=my_function)
@pytest.mark.context_kwargs(callback=my_function)
def test_my_app(pw_page):
    # internal machinery will invoke `my_function_that_returns_kwargs` 
    # later to get overrides.  These are merged sensible with CLI and 
    # other PWE defaults.
    ...

Alternatively, if you need more dynamicism, overwrite the fixture and take full control:

import pytest
import typing

@pytest.fixture(scope="function")
def pw_browser_contexts(config: pytest.Config) -> dict[typing.Any, typing.Any]:
    return {
        # Any overrides here,
        # ...
    }

Caution

If you chose the fixture route, you are responsible for all details, PWE defaults will not be included. Using the markers, a deferred function can be used to calculate args at runtime!


Fixtures


  • pw_playwright - At present this returns the sync Playwright instance.
  • pw_multi_browser - Automatically run a test on chromium, firefox and webkit.
  • pw_headed - Returns if the browser is running headlessly or not.
  • pw_slow_mo - Returns the currently throttle in milliseconds, defaults to 0.
  • pw_is_chromium - Returns if the current browser is chromium.
  • pw_is_webkit - Returns if the current browser is webkit.
  • pw_is_firefox - Returns if the current browser is firefox.
  • pw_root_url - Returns the base url auto loaded by Page objects.
  • pw_browser - Returns the current Browser instance.
  • pw_browser_engine - A string representing the type of browser for this test.
  • pw_context - A playwright sync Context object, useful for multi page scenarios.
  • pw_context_kwargs - An overridable fixture to control arguments to playwright Context instances.
  • pw_page - Returns a new Page instance.
  • pw_is_debugging - Returns if playwright will be using PW_DEBUG mode.
  • pw_browser_kwargs - An overridable fixture to control arguments to playwright `Browser nstances.

Hooks

pytest_playwright_acquire_binaries: Hook in and customise binary acquisition at runtime. pytest_playwright_is_debugging: User defined behaviour for detecting if an IDE is debugging. pytest_playwright_browser_env: Control the environment passed on to playwright browser instances. pytest_playwright_configure_proxy: Return a ProxySettings object in your own hooks to control proxy settings.


Markers

  • @pytest.mark.only_on_browsers - Only run on a subset of browsers when using the pw_multi_browser fixture.
  • @pytest.mark.context_kwargs - Per test level overrides to the context object.
  • @pytest.mark.browser_kwargs - Per test level overrides to the browser object.

pytest-playwright-enhanced's People

Contributors

symonk avatar dependabot[bot] avatar

Stargazers

Owen Lamont avatar Omkar Khatavkar avatar  avatar

Watchers

 avatar  avatar

pytest-playwright-enhanced's Issues

support `--screenshots`

Adding support for screenshots is not feature complete until:

  • Yes can provide yes
  • Defaults to no
  • User can provide full which means yes & provides full_screen=True to the context
  • All cases above have adequate unit test(s)
  • All cases above are xdist worker safe

Conditionals:

  • Can we do anything fancy with the screenshot name to map it back to a test?

Allowed deferring of `context` & `browser` kwargs

it is not always 'known' what the user might need, it may be required to calculate it dynamically, allow some sort of defer/callback that is invoked by the plugin machinery that can be provided as a keyword arg to:

@pytest.mark.context_kwargs(callback=lambda: {"foo": 1})
@pytest.mark.browser_kwargs(callback=lambda: {"bar": 2})

video on fail fails with xdist

reproducable:

def test_something(pw_page, pw_multi_browser):
    pw_page.goto("https://www.google.co.uk")
    assert False
pytest . --headed --browser webkit --browser chromium --browser firefox -n 3 --video-on-fail yes

ensure `--artifacts` directory is a folder

ensuring a trailing / and is actually a folder etc.
Add some error handling to places we are using this, many things can go wrong
file lengths on windows, general errors
does running containerised make this more challenging?

[High Prio] Implement a reusable binary solution for tests using `tox` and w/o

Right now we have some major problems in terms of tox/testing. Everything works fine from a normal perspective after a simple playwright install because browsers reside in ~/.cache/ms-playwright/* - however, in tox this is a pytes tmpdir, which would auto acquire binaries pretty much on every run.

Implement some sort of solution for development that allows tox to leverage / reuse existing binaries. This is also slightly tricker because pytester (probably) doesn't always pass the ENV through etc.

tox sets a $homedir and fails with binary acquisition

reliably running tests that actually launch contexts/pages is... cumbersome right now, pytest itself will set a $HOME in its temp directory, manual IDE execution is easy, tox is slightly trickier - figure out a solution

tracing `title` & `name`

when dumping the trace, a name & title are supported, derive them from the node.name possibly?

Build out mkdocs site

Docs are vital for UX, build out the mkdocs + serve them on github - the machinery is already in place for this, content is missing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.