Selenium JavaScript tutorial

Please select a Tutorial

1. Introduction In real world testing, you'll need to test your app on multiple browsers, viewports and mobile devices. So you'll need to run and re-run ALL the tests in different browsers and multiple viewports to ensure things are fine - which could take hours and hours to complete. You may also encounter browsers not opening properly, or hanging and what not.

What if you could run in just one browser, just once, and still do cross-browser tests across various browsers, viewports and multiple mobile emulators? That's where Visual Grid [BETA] comes in.

TL;DR

When you execute this tutorial, it'll run the test locally in Chrome browser but will show results in the following 5 combinations:

  1. Chrome browser (800 X 600 viewport)
  2. Chrome browser (700 X 500 viewport)
  3. Firefox browser (1200 X 800 viewport)
  4. Firefox browser (1600 X 1200 viewport)
  5. An iPhone4 emulator

... all in ~30 seconds!β°πŸš€

2. The Visual Grid

The Visual Grid[BETA] provides an elegant and an ultra fast way to render pages in different browsers, viewports and mobile simulators and then take screenshots and then send those screenshots to Applitools AI server. With Visual Grid you just need to run your test in a single browser but perform end-to-end and cross-browser tests in various browsers such as Chrome,Firefox, IE, and also numerous combinations of viewports and in mobile simulators -- all in under a minute. Without Visual Grid, you'll be wasting a lot of time and money running those tests in multiple browsers which could take hours and are error-prone.

And since Visual Grid is integrated with our Eyes AI, you'll actually find a lot more functional and visual bugs than you'd normally find.

Further, you can use our Root-Cause-Analysis (RCA) feature to figure out exactly which DOM element or CSS caused difference! So you can go backwards, from the screenshot to the code!

Best part: All you need to do is to simply make eyes.check calls in your existing Selenium tests after adding our SDK and instantiating Visual Grid. And that's all there is to it!

How it works

Once you add and instantiate the Applitools SDK, simply add eyes.check calls wherever you want to check if the state of the page looks OK. Then run the Selenium test as usual. And while running the tests, anytime it encounters eyes.check (or similar methods), Applitools SDK uploads the DOM resource of that page to our Visual Grid behind the scenes and in batches. The Visual Grid then renders every page in different browsers and mobile emulators (in parallel) and takes screenshots as per the configuration you set. Finally, it sends those screenshots to our AI Server.

When you first run the test, our AI server simply stores those 1st set of screenshots as Baseline images. When you run the same test again, the AI server will compare the new set of screenshots, aka Checkpoint images, with the corresponding Baseline images and higlights bugs.

Visual Grid architecture picture
3. Marking tests as pass or fail

When the AI compares the baseline and checkpoint image, if it finds a legitimate difference, it'll mark the test as "Undecided". You need to then login to our dashboard and then either mark it as "Fail" or "Pass" manually. If you mark it as a "Fail", any further runs with similar difference will be automatically marked as "Failed" tests. If you mark it as a "Pass", then we update the new checkpoint image as the new "Baseline" and if you run tests again we'll consider those as "Passed" tests.

Note:

  • Visual Grid is currently in Early access.

  • Visual Grid does not run tests, it just opens/renders webpages in different browsers, in different viewports, and in mobile emulators, in landscape and portrait modes. You still need to run a single set of tests either on a local machine or in the cloud (like AWS, GCloud, Azure or in commercial testing vendor clouds). But you don't need to run all the tests on each browsers and each mobile devices and emulators.

  • Applitools AI has been trained with 100s of millions of images. It doesn't do a pixel-by-pixel comparison because it leads to a lot of false positives, but instead simulates real human eyes and ignore normal differences that humans would ignore and only highlight those that humans would highlight as bugs.

  • ACCURACY: A.I's current accuracy rate is 99.999%! Which means for most applications that odds that you'll see false-positives is 1 in a million!

4. Get Started πŸš€

Pre-requisites

  1. Install Node.js from here
  2. Register to Applitools and have the APPlitools API Key
    • Get the API key by logging into Applitools > Person Icon > My API Key
  3. Chrome Webdriver is on your machine and is in the PATH. Here are some resources from the internet that'll help you.

To run this example:

  1. Get the code:
    • git clone git clone https://github.com/applitools/tutorial-selenium-javascript-visualgrid.git,
    • or, download it as a Zip file and unzip it.
  2. cd tutorial-selenium-javascript-visualgrid folder
  3. npm install
  4. npm run test

Adding to an existing Chrome and Selenium Webdriver project

npm install "@applitools/eyes-selenium" --save-dev
1

Source code

The following code imports the Applitools SDK to a regular Selenium JS project. Then it configures Visual Grid options to render the app in 1. Chrome browser, 2. Firefox browser and 3. iPhone4 Emulator (Portrait mode). And when you run the test in Chrome browser.

Note

Everytime we call eyes.check, we upload the resources(HTML, CSS, fonts), esseentially the state of the DOM so that we can render the app in different browsers inside Visual Grid.


'use strict';

// Import chromedriver
require('chromedriver');

// Import Selenium Webdriver
const { Builder, Capabilities, By } = require('selenium-webdriver');

// Import Applitools SDK and relevant methods
const { Eyes, Target, VisualGridRunner, Configuration, BrowserType, DeviceName, ScreenOrientation } = require('@applitools/eyes-selenium');

(async () => {
  // Open a Chrome browser.
  const driver = new Builder()
    .withCapabilities(Capabilities.chrome())
    .build();

  // Initialize the eyes SDK and set your private API key.
  const eyes = new Eyes(new VisualGridRunner()); // Add a runner if you want to use VisualGrid service

  // How, you have an ability to use new Configuration class to setup everything
  // It has all configuration methods from Eyes class and more (to setup emulation devices)
  const config = new Configuration();

  // Add your API key
  config.setApiKey('{APPLITOOLS_API_KEY}'); // πŸ‘ˆπŸΌ REPLACE ME!

  // Set the App name and the Test name
  config.setAppName('Demo app');
  config.setTestName('JS Smoke test');

  // Set concurrent sessions (upto 100, depending on your license)
  config.setConcurrentSessions(4);

  // Add Chrome browser with two different viewports
  config.addBrowser(800, 600, BrowserType.CHROME);
  config.addBrowser(700, 500, BrowserType.CHROME);

  // Add Firefox browser with two different viewports
  config.addBrowser(1200, 800, BrowserType.FIREFOX);
  config.addBrowser(1600, 1200, BrowserType.FIREFOX);

  // Add iPhone 4 with Portrait mode
  config.addDeviceEmulation(DeviceName.iPhone_4, ScreenOrientation.PORTRAIT);

  // Set the config to eyes
  eyes.setConfiguration(config);

  try {
    // Start the test. All configuration you have set above
    await eyes.open(driver);

    // Navigate the browser to the "hello world!" web-site.
    await driver.get('https://demo.applitools.com');

    // ⭐️To see visual bugs, change the above URL to:
    // https://demo.applitools.com/index_v2.html and run the test again

    // Visual checkpoint #1.
    await eyes.check('Login Page', Target.window().fully());

    // Click the "Click me!" button.
    await driver.findElement(By.id('log-in')).click();

    // Visual checkpoint #2.
    await eyes.check('Click!', Target.window().fully());

    console.log(
      `Please wait, we are now..
      1. Uploading the app's resources (html, css, images)
      2. Rendering them in different browsers, emulators
      3. Analyzing them using our A.I. 

      ...you should see the result within 15 - 60 seconds depending on your internet speed, # combinations and how heavy your app is.`
    );

    // End the test.
    // const results = await eyes.close(); // will return only first TestResults, but as we have multiple browsers, we need more results

    // This will return all results as an array
    const results = await eyes.getRunner().getAllResults();

    console.log(results);
  } catch (e) {
    // When you use VisualGrid, we don't throw an error on validation mismatch
    console.error(e);
  } finally {
    // Close the browser.
    await driver.quit();

    // If the test was aborted before eyes.close was called ends the test as aborted.
    await eyes.abortIfNotClosed();
  }
})();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

Note:

To see visual bugs: After you run one set of tests, you now have the baselines. Run the same test but with this URL: https://demo.applitools.com/index_v2.html. This version of the demo app has some visual bugs so you can see how it all works. And the second set of screenshots are called "Checkpoint images".


Resources




Terms & Conditions Privacy Policy GDPRΒ© 2019 Applitools. All rights reserved.

Last Updated: 4/9/2019, 2:11:57 PM