Fixtures

mocker

The default installation provides the mocker fixture via pytest-mock.

browser

The browser extra provides a new browser() fixture:

pytest_logikal.browser.plugin.browser() Browser

Yield a scenario-specific Browser sub-class instance.

class pytest_logikal.browser.Browser

Bases: ABC, WebDriver

Base class for browser-specific web drivers.

check(name: str | None = None, wait_milliseconds: int | None = 100) None

Create a screenshot and check it against an expected version.

Parameters:
  • name – The name of the expected screenshot.

  • wait_milliseconds – The milliseconds to wait before calculating the screenshot height for unlimited height checks.

replace_text(element: Any, text: str) None

Replace the text of an element.

Parameters:
  • element – The element to use.

  • text – The new text value.

login(user: Any, force: bool = True) None

Note

The django extra must be installed for this method to work.

Log in a given user.

Parameters:
  • user – The user to log in.

  • force – Whether to log the user in without going through the authentication steps.

Browser settings must be specified via the set_browser() decorator:

@pytest_logikal.browser.set_browser(scenarios: Scenario | list[Scenario]) Callable[[Any], Any]

Apply the given scenarios to the browser() fixture.

Parameters:

scenarios – The scenarios to use.

You can specify browser scenarios with this decorator as follows:

from pytest_django.live_server_helper import LiveServer

from pytest_logikal.browser import Browser, scenarios, set_browser


@set_browser(scenarios.desktop)
def test_homepage(browser: Browser, live_server: LiveServer) -> None:
    browser.get(live_server.url)
    browser.check()

You can also run your test in multiple scenarios:

from pytest_django.live_server_helper import LiveServer

from pytest_logikal.browser import Browser, scenarios, set_browser


@set_browser([
    scenarios.desktop,
    scenarios.mobile_l,
])
def test_homepage(browser: Browser, live_server: LiveServer) -> None:
    browser.get(live_server.url)
    browser.check()

Custom scenarios can be specified via the Scenario class:

class pytest_logikal.browser.Scenario(settings: Settings | Iterable[Settings], browsers: Iterable[str] | None = None)

Browser scenario specification.

Parameters:
  • settings – Settings to use.

  • browsers – Browsers to use. Defaults to using all registered browser versions.

class pytest_logikal.browser.Settings(name: str, width: int, height: int, full_page_height: bool = True, mobile: bool = False)

Browser settings specification.

Parameters:
  • name – Name of the settings.

  • width – Browser window width.

  • height – Browser window height.

  • full_page_height – Whether to use the full page height for screenshots.

  • mobile – Whether it is a mobile browser.

Built-in Scenarios

Currently the following built-in scenarios are available:

pytest_logikal.browser.scenarios.desktop_4k = Scenario(settings=Settings(name='desktop_4k', width=2560, height=1440, full_page_height=True, mobile=False), browsers=None)
pytest_logikal.browser.scenarios.desktop = Scenario(settings=Settings(name='desktop', width=1800, height=900, full_page_height=True, mobile=False), browsers=None)
pytest_logikal.browser.scenarios.laptop_l = Scenario(settings=Settings(name='laptop_l', width=1440, height=900, full_page_height=True, mobile=False), browsers=None)
pytest_logikal.browser.scenarios.laptop = Scenario(settings=Settings(name='laptop', width=1024, height=768, full_page_height=True, mobile=False), browsers=None)
pytest_logikal.browser.scenarios.tablet = Scenario(settings=Settings(name='tablet', width=768, height=1024, full_page_height=True, mobile=True), browsers=None)
pytest_logikal.browser.scenarios.mobile_l = Scenario(settings=Settings(name='mobile_l', width=425, height=680, full_page_height=True, mobile=True), browsers=None)
pytest_logikal.browser.scenarios.mobile_m = Scenario(settings=Settings(name='mobile_m', width=375, height=600, full_page_height=True, mobile=True), browsers=None)
pytest_logikal.browser.scenarios.mobile_s = Scenario(settings=Settings(name='mobile_s', width=320, height=512, full_page_height=True, mobile=True), browsers=None)

live_url

The django extra provides a new live_url fixture:

pytest_logikal.django.live_url(name: str | None, args: Sequence[Any] | None, kwargs: dict[str, Any] | None) str

Return the path to a URL.

Parameters:
  • name – The URL pattern name.

  • args – The URL arguments to use.

  • kwargs – The URL keyword arguments to use.

language

The django extra also provides the language fixture:

pytest_logikal.django.language(request: Any) Iterable[str]

Return the currently activated language.

You may control the language for a specific test with the set_language or all_languages decorators:

@pytest_logikal.django.set_language(*language_codes: str) Callable[[Any], Any]

Mark a test to run with each of the specified languages.

Parameters:

*language_codes – The language codes to use.

Note

You must also use the language fixture in your test when applying this decorator.

@pytest_logikal.django.all_languages Callable[[Any], Any]

Mark a test to run with every available language.

Note

You must also use the language fixture in your test when applying this decorator.

timezone

The django extra also provides the timezone fixture:

pytest_logikal.django.timezone(request: Any) Iterable[str]

Return the current time zone ID.

You may control the time zone for a specific test with the set_timezone decorator:

@pytest_logikal.django.set_timezone(*zone_ids: str) Callable[[Any], Any]

Mark a test to run with each of the specified time zones.

Parameters:

*zone_ids – The time zone IDs to use.

Note

You must also use the timezone fixture in your test when applying this decorator.