Fixtures

mocker

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

browser

The browser extra provides a new browser fixture:

pytest_logikal.browser.browser() Browser

Yield a Browser instance.

class pytest_logikal.browser.Browser

Browser automation.

Has the same attributes and methods as a selenium.webdriver.Chrome instance.

check(name: Optional[str] = None) None

Create a screenshot and check it against an expected version.

Parameters

name – The name of the expected screenshot.

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.

class pytest_logikal.browser.BrowserSettings(setting: Optional[str] = 'desktop', name: Optional[str] = None, width: Optional[int] = None, height: Optional[Union[int, str]] = None, frame_height: Optional[int] = None, mobile: Optional[bool] = None)

Browser settings data.

Parameters
  • setting – The named setting to use.

  • name – The name to use for screenshots. Defaults to the setting name.

  • width – The width of the browser window.

  • height – The height of the browser window (use ‘unlimited’ for unlimited and ‘frame’ to use the frame height). Defaults to ‘unlimited’.

  • frame_height – The browser window frame height to use when taking ‘unlimited’ screenshots.

  • mobile – Whether it is a mobile browser. Defaults to False.

You may control browser settings for a specific test with the set_browser() decorator:

@pytest_logikal.browser.set_browser(*args: Dict[str, Any], **kwargs: Any) Callable[[Any], Any]

Control the creation of the Browser object in the browser fixture.

Parameters
  • *args – A list of arguments to forward to the underlying BrowserSettings instances.

  • **kwargs – The arguments to forward to the underlying BrowserSettings instance.

You can specify browser settings with this decorator as follows:

from pytest_django.live_server_helper import LiveServer
from pytest_logikal.browser import Browser, set_browser


@set_browser(setting='tablet')
def test_homepage(browser: Browser, live_server: LiveServer) -> None:
    browser.get(live_server.url)
    browser.check()

The currently available settings are the following:

Setting

Width

Height

Frame

Mobile

desktop

1800

unlimited

900

False

laptop-l

1440

unlimited

900

False

laptop

1024

unlimited

768

False

tablet

768

unlimited

1024

True

mobile-l

425

unlimited

680

True

mobile-m

375

unlimited

600

True

mobile-s

320

unlimited

512

True

You can also run your test in multiple browser environments:

@set_browser(
    dict(setting='desktop'),
    dict(setting='mobile'),
)
def test_homepage(browser: Browser, live_server: LiveServer) -> None:
    browser.get(live_server.url)
    browser.check()

The named settings can be overridden or extended via the tool.browser.settings table in pyproject.toml:

[tool.browser.settings]
desktop = {width = 1024, height = 768}
mobile-xl = {width = 480, frame_height = 768, mobile = true}

You can then use your custom named setting as follows:

@set_browser(setting='mobile-xl')
def test_homepage(browser: Browser, live_server: LiveServer) -> None:
    browser.get(live_server.url)
    browser.check()

While we recommend using named settings, you can also use completely arbitrary settings for a test as follows:

@set_browser(name='laptop-xl', width=1920, height=1080)
def test_homepage(browser: Browser, live_server: LiveServer) -> None:
    browser.get(live_server.url)
    browser.check()

live_url

The django extra provides a new live_url fixture:

pytest_logikal.django.live_url(name: str) str

Return the path to a URL.

Parameters

name – The URL pattern name.

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.