Import and Initialize with Java
Importing and initializing axe DevTools for Web with Java
Once you've installed axe DevTools for Web, you need to import it into your testing file and initialize the tool in conjunction with its webdriver.
Prerequisites
In order to import and initialize axe DevTools for Web, it needs to be installed first. If you haven't yet, read the guide on how to install it from a bundle, from your artifact repository, or from Deque's Agora. You also need a downloaded and configured Selenium WebDriver of your choice.
Importing
In order to use axe DevTools several packages need to be imported. Depending on your configuration, these packages may vary. Deque offers four Java integrations:
- Selenium (Writing Tests using Selenium and Java Selenium API Reference)
- Hamcrest (Writing Tests using Hamcrest and Hamcrest API Reference)
- Cucumber (Writing Tests using Cucumber)
- Playwright (Using Playwright and Playwright API Reference)
Selenium Configuration Import Statements
To use axe DevTools with Selenium follow this portion of the guide. Use this configuration if you aren't coupling axe DevTools with an existing assertion library and instead write your own tests. In addition, import statements will be required for the webdriver of your choice.
The only package that is explicitly required to be imported is the axe DevTools Selenium package. This is what communicates between axe DevTools and the Selenium WebDriver to run the scans.
import com.deque.html.axedevtools.selenium.*;
For the this configuration, two more packages are recommended to allow you to generate and interpret results. The results package generates a JSON object containing the scan results, and the reporter package allows generation of HTML, JUnit XML, or CSV reports of the scan results.
import com.deque.html.axedevtools.selenium.results.*;
import com.deque.html.axedevtools.selenium.reporter.*;
Hamcrest Import Statements
For testing environments that make use of Hamcrest, there are only two packages that must be imported. The first package contains everything needed for standard scans.
import com.deque.html.axedevtools.matchers.*;
The second package adds the ability to scope the scans by selectors. To use this functionality, the additional import of this package is required.
import com.deque.html.axedevtools.matchers.selectors;
Cucumber Import Statements
In order to pair axe DevTools with Cucumber, only one import package is required.
import com.deque.html.axedevtools.cucumber.*;
Playwright Import Statements
You need to import two packages to use the Playwright integration.
import com.deque.html.maven.axedevtools.playwright.AxePlaywrightBuilder;
import com.deque.html.maven.axedevtools.utility.axeresults.AxeResults;
You also need to import Microsoft's Playwright packages.
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
Initializing (non-Playwright)
After the required package import statements have been added to your test file, you will need to initialize axe DevTools with its web driver in order to run accessibility scans. Using the standard configuration, this code will initialize axe DevTools. This example uses ChromeDriver, but other browser drivers like GeckoDriver work equally well.
WebDriver webdriver = new ChromeDriver();
AxeDriver axedriver = new AxeDriver(webdriver);
Additionally, if you need a generated results object and/or a report, an axe Selenium object must be imported, as described above, and initialized
AxeSelenium axeSelenium = new AxeSelenium();
Initializing Playwright
To initialize Playwright, you construct a new Playwright object, use it to create a new Browser object, and finally use the Browser object to create a Page object.
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium()
.launch(new BrowserType.LaunchOptions().setHeadless(true));
Page page = browser.newPage();
page.navigate("https://dequeuniversity.com/demo/mars/");
Next Steps
Once you've installed, imported, and initialized axe DevTools, you're ready to move on to scanning for accessibility and writing accessibility tests. To do that, see the guide on writing tests with Selenium, writing tests with Hamcrest, writing tests with Cucumber, or writing tests with Playwright.
Troubleshooting
If you have trouble with any of the importing or initializing of axe DevTools, contact your Deque representative directly, reach us via our support desk, or send us an email. We'll be happy to help.