Testing web apps in JavaScript using Selenium WebDriver and Jest | Applitools Documentation
Quickstart for Visual Testing in JavaScript
This quickstart will show you how to visually test web apps in JavaScript using Selenium WebDriver and Jest. Visual testing can help you catch problems that traditional automation struggles to find.
After preparing your environment, this three-step quickstart should take about 15 minutes to complete.
Step 1: Preparing your environment
You'll need a few things to run this quickstart:
- An Applitools account, which you can register for free.
- A recent version of Node.js.
- A good JavaScript editor like Visual Studio Code.
- An up-to-date version of Google Chrome.
- A corresponding version of ChromeDriver.
Installing ChromeDriver
The major version numbers of Chrome and ChromeDriver must be the same. Otherwise, Selenium WebDriver may raise an exception when it is initialized. For example, Chrome v101 requires ChromeDriver v101. ChromeDriver must be installed into a directory covered by the system PATH variable. Follow the instructions on Selenium's Install browser drivers page. On macOS and Linux, the recommended location for the chromedriver executable is the /usr/local/bin directory. You can test that ChromeDriver is working by running the chromedriver -v command to print its version.
Step 2: Getting your example project
Downloading the example project
The example project is located at https://github.com/applitools/example-selenium-javascript-jest. Clone this repository to your local machine:
git clone https://github.com/applitools/example-selenium-javascript-jest.git
cd example-selenium-javascript-jest
Don't have Git?
Instead of running git clone, you can download the project as a ZIP file and extract it.
Installing the dependencies
The example project uses npm for package management. Run the following commands to install the dependencies, which work for both example projects on any operating system:
npm install
Adding Applitools to Another Selenium JavaScript Project? This example project already has the Applitools Eyes SDK as a dependency. If you want to add the Applitools Eyes SDK as a new dependency to another project, run the following command:
npm install --save-dev @applitools/eyes-selenium
Deciding how to run tests
The test uses Selenium WebDriver to automate the browser. There are two places to run your Selenium WebDriver session:
- On your local machine (which is the default option)
- On Applitools Execution Cloud
If you run WebDriver locally, then you need to set it up and manage it yourself. If you use Execution Cloud, then Applitools will manage WebDriver for you. It will also automatically heal broken locators and wait for elements to be ready. If you would like to try Execution Cloud, please request access. The docs for Execution Cloud provide more information.
There are two ways to test the visual snapshots captured by the test:
- Using Applitools Ultrafast Grid for cross-browser testing in the cloud
- Using Applitools Classic runner on your local machine
Walking through the code
The project contains one visual test case, which is located at test/acme-bank.test.js.
acme-bank.test.js is a Jest test file that covers login behavior for the ACME Bank demo web app. The variables at the top control how tests will run:
- Set
USE_ULTRAFAST_GRIDtotrueto use Ultrafast Grid orfalseto use the Classic runner. - Set
USE_EXECUTION_CLOUDtotrueto use Execution Cloud orfalseto run WebDriver sessions locally.
Setup varies slightly for these different options. In-line comments explain every section of its code. Read it top to bottom to understand how it works:
'use strict'
const { Builder, By } = require('selenium-webdriver');
const { Eyes,
ClassicRunner,
VisualGridRunner,
RunnerOptions,
Target,
RectangleSize,
Configuration,
BatchInfo,
BrowserType,
ScreenOrientation,
DeviceName } = require('@applitools/eyes-selenium');
describe('ACME Bank', () => {
const USE_ULTRAFAST_GRID = true;
const USE_EXECUTION_CLOUD = false;
var applitoolsApiKey;
var headless;
let batch;
let config;
let runner;
let driver;
let eyes;
beforeAll(async () => {
applitoolsApiKey = process.env.APPLITOOLS_API_KEY;
headless = process.env.HEADLESS? ['headless'] : []
if (USE_ULTRAFAST_GRID) {
runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5));
} else {
runner = new ClassicRunner();
}
const runnerName = (USE_ULTRAFAST_GRID) ? 'Ultrafast Grid' : 'Classic runner';
batch = new BatchInfo(`Example: Selenium JavaScript Jest with the ${runnerName}`);
config = new Configuration();
config.setApiKey(applitoolsApiKey);
config.setBatch(batch);
if (USE_ULTRAFAST_GRID) {
config.addBrowser(800, 600, BrowserType.CHROME);
config.addBrowser(1600, 1200, BrowserType.FIREFOX);
config.addBrowser(1024, 768, BrowserType.SAFARI);
config.addDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT);
config.addDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE);
}
}, 60000);
beforeEach(async function() {
var capabilities = {
browserName: 'chrome',
'goog:chromeOptions': {
args: headless,
},
};
if (USE_EXECUTION_CLOUD) {
let url = await Eyes.getExecutionCloudUrl();
driver = new Builder().usingServer(url).withCapabilities(capabilities).build();
} else {
driver = new Builder().withCapabilities(capabilities).build();
}
await driver.manage().setTimeouts({ implicit: 10000 });
eyes = new Eyes(runner);
eyes.setConfiguration(config);
await eyes.open(
driver,
'ACME Bank',
expect.getState().currentTestName,
new RectangleSize(1200, 600)
);
}, 60000);
test('should log into a bank account', async () => {
await driver.get("https://demo.applitools.com");
await eyes.check(Target.window().fully().withName("Login page"));
await driver.findElement(By.css("#username")).sendKeys("andy");
await driver.findElement(By.css("#password")).sendKeys("i<3pandas");
await driver.findElement(By.id("log-in")).click();
await eyes.check(Target.window().fully().withName("Main page").layout());
}, 60000);
afterEach(async function() {
await eyes.closeAsync();
await driver.quit();
}, 60000);
afterAll(async () => {
const allTestResults = await runner.getAllTestResults();
console.log(allTestResults);
}, 120000);
});
Step 3: Running your tests
Setting Applitools variables
Before running the visual test, you must find your Applitools API key and set it as an environment variable named APPLITOOLS_API_KEY.
You may set it through your IDE (if applicable), or you may set it from the command line like this:
- macOS and Linux
export APPLITOOLS_API_KEY=<your-api-key>
- Windows
set APPLITOOLS_API_KEY=<your-api-key>
Launching visual tests
To launch the tests, run:
npm test
To run tests using headless Chrome, set the HEADLESS environment variable to true.
After your tests run, you should see results in the Eyes Test Manager. You can log into the Test Manager at eyes.applitools.com.
Need extra help? If you get stuck on this example, please reach out to us to get help.
Taking the next steps with Applitools
Congratulations on completing this quickstart! There's still so much to learn about visual testing with Applitools, but you're off to a great start.