MarketSquare / MarketSquare/robotframework-browser

Add `contrast=` argument to `Emulate Media`, `New Context` and `New Persistent Context`

Open
#5,105 0 comments 0 reactions 1 assignee View on GitHub

@Snooz82 is already working on this.

Since Aug 6, 2026.

enhancement
Dominant language
Python
Stars
655
Forks
147
Avg merge
5h 27m
Merged PRs (30d)
59

Description

## Use case

The library already lets users emulate the accessibility-related media features `prefers-color-scheme` (`colorScheme=`), `prefers-reduced-motion` (`reducedMotion=`) and `forced-colors` (`forcedColors=`), both per page via `Emulate Media` and context-wide via `New Context`. The one remaining member of that family, `prefers-contrast`, cannot be emulated at all today — so styles behind a `@media (prefers-contrast: more)` query (high-contrast themes, thicker focus outlines) cannot be tested. Playwright has supported the `contrast` option since 1.51, and the repo's pinned Playwright 1.62 includes it; there is no workaround short of manipulating OS settings.

## Proposed keyword / arguments

New named-only argument `contrast=` accepting `no-preference` or `more` (and `null` on `Emulate Media` to reset), default `None` (= not emulated):

- `Emulate Media colorScheme=None forcedColors=... media=None reducedMotion=None *, contrast=None`
- `New Context ... contrast=None ...`
- `New Persistent Context ... contrast=None ...`

```robotframework
*** Test Cases ***
High Contrast Theme Is Applied
New Context contrast=more
New Page ${URL}
Get Style body background-color == rgb(0, 0, 0)

Toggle Contrast Mid Test
New Page ${URL}
Emulate Media contrast=more
Get Style a text-decoration-line == underline
Emulate Media contrast=no-preference
```

## Playwright API

- [page.emulateMedia](https://playwright.dev/docs/api/class-page#page-emulate-media) — `contrast` option (Playwright 1.51)
- [browser.newContext](https://playwright.dev/docs/api/class-browser#browser-new-context) — `contrast` option

## Implementation notes

- `protobuf/playwright.proto`: add `contrast` field to the EmulateMedia request and to the context-options payload (context options travel as serialized JSON, so this may need no proto change for `New Context`).
- `node/playwright-wrapper`: pass `contrast` through to `page.emulateMedia` and `browser.newContext`/`launchPersistentContext`.
- `Browser/keywords`: new `Contrast` enum in `Browser/utils/data_types.py` (mirroring the existing `ReducedMotion`/`ForcedColors` enums); new named-only argument in `pdf.py` (`emulate_media`) and `playwright_state.py` (`new_context`, `new_persistent_context`).
- Docs + atest: page asserting a `prefers-contrast: more` media query via `Evaluate JavaScript`/`Get Style`.

## Backwards compatibility

Additive named-only argument with default `None`, meaning the media feature is simply not emulated — identical to today's behavior. No existing argument changes position or semantics.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.