Playwright JS SDK

The Evinced Playwright JS SDK integrates with new or existing Playwright tests to automatically detect accessibility issues. By adding a few lines of code to your Playwright project, you can begin analyzing all the web pages and DOM changes to provide a dynamic view of how your site can become more accessible. As a result of the test, a rich and comprehensive report is generated to easily track issues to resolution.

Interested in seeing this in action? Contact us to get started!

Prerequisites

  • Playwright version 1.25 or higher

Get Started

Installation

1npm install @evinced/js-playwright-sdk

Your First Test

SDK Initialization

To work with Playwright JS SDK you need to have authentication token. Please refer to licensing section for details. There is no any other specific initialization of SDK.

Add Evinced accessibility checks (Single Run Mode)

This is a simple example of how to add an Evinced accessibility scan to a test. Please note the inline comments that give detail on each test step.

1const { test, expect } = require("@playwright/test");
2import { EvincedSDK } from "@evinced/js-playwright-sdk";
3
4test.describe("Demo Page", () => {
5 test("Example Test", async ({ page }) => {
6 const evincedService = new EvincedSDK(page);
7 await page.goto("https://demo.evinced.com/");
8 const issues = await evincedService.evAnalyze();
9 expect(issues.length).toEqual(0);
10 });
11});

Add Evinced accessibility checks (Continuous Mode)

This is an example of how to add a continuous Evinced accessibility scan to a test. Using the evStart() and evStop() methods, the Evinced engine will continually scan in the background capturing all DOM changes and page navigations as the test is executed. This will capture all accessibility issues as clicking on drop-downs or similar interactions reveals more of the page. The advantage of continuous mode is that no interaction with the actual test code is needed.

1const { test, expect } = require('@playwright/test');
2import { EvincedSDK } from '@evinced/js-playwright-sdk';
3
4test.describe("Demo Page", () => {
5 test('Example Test', async ({ page }) => {
6 const evincedService = new EvincedSDK(page);
7 await evincedService.evStart();
8
9 await page.goto('https://demo.evinced.com/');
10
11 const BASE_FORM_SELECTOR = '#gatsby-focus-wrapper > main > div.wrapper-banner > div.filter-container';
12 const SELECT_HOME_DROPDOWN = `${BASE_FORM_SELECTOR} > div:nth-child(1) > div > div.dropdown.line`;
13 const SELECT_WHERE_DROPDOWN = `${BASE_FORM_SELECTOR} > div:nth-child(2) > div > div.dropdown.line`;
14 const TINY_HOME_OPTION = `${BASE_FORM_SELECTOR} > div:nth-child(1) > div > ul > li:nth-child(2)`;
15 const EAST_COST_OPTION = `${BASE_FORM_SELECTOR} > div:nth-child(2) > div > ul > li:nth-child(3)`;
16
17 await page.locator(SELECT_HOME_DROPDOWN).click();
18 await page.locator(TINY_HOME_OPTION).click();
19 await page.locator(SELECT_WHERE_DROPDOWN).click();
20 await page.locator(EAST_COST_OPTION).click();
21
22 const issues = await evincedService.evStop();
23 expect(issues.length).toEqual(0);
24 });
25});

API

global configuration

Initializes the Evinced object within the project.

Example

The specific SDK initialization is not needed. You need just import EvincedSDK in your test files.

1import { EvincedSDK } from '@evinced/js-playwright-sdk';

Please refer to configuration to see examples of using init with options.

evAnalyze(options)

Scans the current page and returns a list of accessibility issues.

Note: This method is not supported if evStart() is already running.

Please refer to configuration to see examples of using init with options.

Example

1const evincedService = new EvincedSDK(page);
2await page.goto("https://demo.evinced.com/");
3const issues = await evincedService.evAnalyze();

Return value Promise<Issue[]>

A Report object is returned containing accessibility issues. This is the recommended method for static page analysis. For more information regarding reports as well as the Report object itself, please refer to our detailed Web Reports page.

evStart(options)

Continually watches for DOM mutations and page navigation and records all accessibility issues until the evStop() method is called. This method is recommended for dynamic page flows.

Example

1const evincedService = new EvincedSDK(page);
2await evincedService.evStart();
3await page.goto('https://demo.evinced.com/');
4const issues = await evincedService.evStop();

Return value Promise<void>

evStop(options)

Stops the process of issue gathering started by evStart() command.

Example

1const evincedService = new EvincedSDK(page);
2await evincedService.evStart();
3await page.goto('https://demo.evinced.com/');
4const issues = await evincedService.evStop();

Return value Promise<Issue[]>

Returns an object containing recorded accessibility issues from the point in time at which the evStart() method was instantiated. For more information regarding reports as well as the returned object itself, please refer to detailed Web Reports page.

evSaveFile(issues, format, destination)

Saves list of the issues in a file, with the specified format and location. For example, format could be ‘json', 'html, 'sarif' or 'csv'. Please find detailed information on Web Reports page.

Example

1const evincedService = new EvincedSDK(page);
2await page.goto("https://demo.evinced.com/");
3const issues = await evincedService.evAnalyze();
4evincedService.evSaveFile(issues, 'html', 'test-results/evinced-report.html');
5evincedService.evSaveFile(issues, 'csv', 'test-results/evinced-report.csv');
6evincedService.evSaveFile(issues, 'json', 'test-results/evinced-report.json');
7evincedService.evSaveFile(issues, 'sarif', 'test-results/evinced-report.sarif.json');

Return value Promise<void>

Aggregated Report

The aggregated report feature allows you to have a general aggregated report for the whole run (not only for one test or suite). This report will contain all the issues found by the tests where evStart() and evStop() commands were called. It is still possible to use the evSaveFile() command in any place of your code along with this Aggregated Report feature.

Example

The feature **is not yet implemented** in this SDK. If you'd like to increase the priority of it, please contact support@evinced.com.

Licensing

To work with Playwright JS SDK you need to have Authentication token. There are two methods to provide the token: offline mode and online mode. The difference is if you use online mode the request to obtain the token will be sent to Evinced Licencing Server. You need to provide Service ID and Secret API key in such a case. They are available via the Evinced Product Hub.

Offline mode assumes that you already have token and can provide it directly. If an offline token is required please reach out to your account team or support@evinced.com.

We encourage to use environment variables to store credentials and read them in code

1# Offline mode
2export AUTH_SERVICE_ID=<serviceId>
3export AUTH_TOKEN=<token>
4
5# Online mode
6export AUTH_SERVICE_ID=<serviceId>
7export AUTH_SECRET=<secret>

Example

Create the file global-setup.js with code:

1// Online credentials
2import { setCredentials } from '@evinced/js-playwright-sdk';
3async function globalSetup() {
4 await setCredentials({
5 serviceId: process.env.AUTH_SERVICE_ID,
6 secret: process.env.AUTH_SECRET,
7 });
8}
9
10export default globalSetup;
11
12// OR
13
14// Offline credentials
15import { setOfflineCredentials } from '@evinced/js-playwright-sdk';
16
17async function globalSetup() {
18 setOfflineCredentials({
19 serviceId: process.env.AUTH_SERVICE_ID,
20 token: process.env.AUTH_TOKEN,
21 });
22}
23
24export default globalSetup;

to enable global-setup.js you need to set in the playwright.config.js, just add the following line in config:

1globalSetup: require.resolve('./global-setup')

Configuration

The same configuration object can be used in global configuration, evStart() and evAnalyze() methods but with a bit different consequences. By providing some options in global configuration method you define a global configuration for all calls of evStart() or evAnalyze() methods. Please note that global configuration is not intended to be altered from tests due to it will affect all the rest tests as well as tests running in parallel threads. To alter configuration in specific tests you can provide config on the method's level, for instance with evStart() it defines local configuration for this particular session until evStop() is called. If provided in both levels, the command's level config will override the global one.

Engines configuration

Evinced uses two separate engines when scanning for accessibility issues, one is the aXe engine and the other is the Evinced engine. By default, Evinced disables the aXe Needs Review and Best Practices issues based on customer request and due to the fact they are mostly false positives. Please note this setting when comparing issue counts directly. See an example of how to enable Needs Review and Best practices issues in our toggles section.

Configuration object type

To define global configuration for Playwright JS SDK please use file `evConfig.json in a root directory of your project.

1export type EvInitOptions = {
2 rootSelector?: string;
3 enableScreenshots?: boolean;
4 axeConfig?: axe.RunOptions;
5 skipValidations?: SkipValidation[];
6 logging?: {
7 LOGGING_LEVEL: string;
8 ADD_LOGGING_CONTEXT?: boolean;
9 };
10 toggles?: { [key: string]: boolean };
11};

Root Selector

Instructs engine to return issues which belong to or which are nested to element with the specified selector. By default the issues for all elements from document root will be returned.

null by default.

Example

1const evincedService = new EvincedSDK(page);
2await evincedService.evStart({
3 rootSelector: '.some-selector'
4});
5await page.goto('https://demo.evinced.com/');
6const issues = await evincedService.evStop();

AXE Configuration

Evinced leverages Axe open-source accessibility toolkit as part of its own accessibility detection engine. The Evinced engine is able to pinpoint far more issues than Axe alone.

For the full Axe config options, see Axe Core API.

Example

1const evincedService = new EvincedSDK(page);
2await page.goto('https://demo.evinced.com/');
3const issues = await evincedService.evAnalyze({
4 "axeConfig": {
5 "rules": {
6 "link-name": { "enabled": false }
7 }
8 },
9});

Errors Strict Mode

When set to true it switches SDK in mode when Evinced SDK errors are thrown as runtime errors and stops current test execution. Otherwise errors are printed into console except critical ones.

false by default.

Example

The feature **is not yet implemented** in this SDK. If you'd like to increase the priority of it, please contact support@evinced.com.

Engine Logging

Switches logging on for engine. Log messages from the engine will be printed to console.

Levels are: debug, info, warn, error

null by default.

error is a default level on engine side.

1const evincedService = new EvincedSDK(page);
2await page.goto('https://demo.evinced.com/');
3const issues = await evincedService.evAnalyze({
4 logging: {
5 LOGGING_LEVEL: 'debug',
6 ADD_LOGGING_CONTEXT: true,
7 },
8});

Reports Screenshots

The Screenshots feature allows to include screenshots to the Evinced reports. When the feature is enabled, Evinced will take screenshots of the page, highlight an element with a specific accessibility issue, and include them to the report.

screenshots_feature

false by default.

Note: The Screenshots feature may affect the test run performance.

Enabling the Screenshots feature

1const issues = await evincedService.evAnalyze({
2 enableScreenshots: true,
3});

Toggles

Toggles are feature flags which controls SDK and Engine behavior. With toggles we introduce experimental features or manage the way how accessibility issues are gathered. The list of toggles is not specified so we suggest not to rely on specific toggle name or value and use them in investigation purposes only.

Example enabling aXe Best Practices and Needs Review Issues

1const issues = await evincedService.evAnalyze({
2 toggles: {
3 USE_AXE_NEEDS_REVIEW: true,
4 USE_AXE_BEST_PRACTICES: true
5 }
6});

Skip Validations

Skips specific validations for the given URL pattern and the selector.

SkipValidation[] by default.

Example

1const skipSelector = {
2 selector: 'testValue.slds-checkbox',
3 urlRegex: '.*',
4 validationTypes: ["WRONG_SEMANTIC_ROLE", "NOT_FOCUSABLE", "NO_DESCRIPTIVE_TEXT"],
5};
6await evincedService.evStart({ skipValidations: [skipSelector] });

How to identify ID for the specific issue type is described in web reports section.

Knowledge Base Link overrides

This custom parameter helps to customize knowledge base links in the reports. Those links are displayed in the reports as follows:

kb_links

The knowledge base link can be overridden for every issue type ID.

Example

1const issues = await evincedService.evAnalyze({
2 issuesContentPerType: {
3 "WRONG_SEMANTIC_ROLE": "https://yourKnowlegdeBase.com/"
4 }
5});

Where WRONG_SEMANTIC_ROLE is ID of the type of the specific issue. In this example, the corresponding issue type for this ID is Interactable Role. How to identify ID for the specific issue type is described in web reports section. After passing the type ID and a custom knowledge base link we are set to see it in the reports.

Shadow DOM support

Example

The feature **is not yet implemented** in this SDK. If you'd like to increase the priority of it, please contact support@evinced.com.

IFrames support

When set to true, the accessibility tests run analysis on iframes that exist inside the page. Default is false.

The feature **is not yet implemented** in this SDK. If you'd like to increase the priority of it, please contact support@evinced.com.

Global Switch

Global Switch allows to disable or enable Evinced functionality. It could be needed, for example, while working on functional tests in your local environment or for running some CI jobs that are not intended to gather information regarding accessibility issues.

When switched off

  • evStart() and evSaveFile() will be bypassed.
  • evStop() and evAnalyze() will return an empty report.
  • evValid() will never fail.

true by default.

Important! Global Switch environment variable overrides the global configuration option.

Switch on/off Evinced functionality in config

In file evConfig.json add the following parameter to properties on top level.

1switchOn: false

So it may look like:

1{
2 "logging": {
3 "LOGGING_LEVEL": "error",
4 "ADD_LOGGING_CONTEXT": true
5 },
6 "switchOn": false
7}

Switching off Evinced functionality with environment variable

1export EV_SWITCH_ON=false

Tutorials

You can find fully functional example projects on our GitHub

Support

Please feel free to reach out to support@evinced.com with any questions.

FAQ

  1. Can I configure which validations to run?

Yes, see the configuration section for details on how to configure Axe validations to your needs.

  1. Can I run tests with Evinced using cloud-based services like Sauce Labs, Perfecto, or BrowserStack?

Yes, we have tested the Evinced SDK on many of these types of cloud-based services and expect no issues.