Testing web apps using Protractor | Applitools Documentation

On this page

Protractor End-of-Life

The Angular team plans to end development of Protractor at the end of 2022 (in conjunction with Angular v15). We recommend using a different browser automation tool like Cypress, Playwright, WebdriverIO, or any of the tools supported by our other Web SDKs.

βœ… Prerequisites

  1. Create a free Applitools account and get the Applitools API KEY

  2. Please set the APPLITOOLS_API_KEY environment variable

    • Mac: export APPLITOOLS_API_KEY='YOUR_API_KEY'

    • Windows: set APPLITOOLS_API_KEY='YOUR_API_KEY'

Tip

  1. You may skip this step if you want to hard code the API KEY inside the tutorial project's code.

  2. It's better to set APPLITOOLS_API_KEY as a system variable (in Windows) or in the ~/.zshrc recipe file (in MacOS) so that it will be automatically set when you open a new terminal shell.

  3. Install node.js from https://nodejs.org​

  4. Install git from https://git-scm.com​

Tip

Installing git is optional. You need this mainly to clone the demo project from the Github repository. Instead of installing git, you can simply download the Zip file from the repo. Further, If you are Mac, you already have git.

  1. Install Google Chrome browser from https://www.google.com/chrome/

  2. Install ChromeDriver on your machine and make sure it's is in the PATH.

Below are some resources from the internet that'll help you

πŸš€ Run Tests

There are two main ways in which you can run tests.

Option 1 - Run With The Ultrafast Grid

1. Introduction To the Ultrafast Grid

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 Ultrafast Grid 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. What Is The Ultrafast Grid?

The Ultrafast Grid 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 Ultrafast 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 Ultrafast 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 Ultrafast 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 Ultrafast 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 Ultrafast Grid behind the scenes and in batches. The Ultrafast 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 highlights bugs.

3. Getting Started With The Ultrafast Grid

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.

Download the demo project

  1. Get the ready-to-use code from the Github repo
    • git clone https://github.com/applitools/tutorial-protractor-ultrafastgrid.git
    • Download it as a Zip file and unzip it.
  2. cd tutorial-protractor-ultrafastgrid folder
  3. Set the Applitoools API key in the code
  4. npm install
  5. npm run test

Add to existing project

Install the SDK

npm install "@applitools/eyes-protractor" --save-dev

Example test

'use strict';

const {
    VisualGridRunner,
    Eyes,
    Target,
    Configuration,
    RectangleSize,
    BatchInfo,
    BrowserType,
    DeviceName,
    ScreenOrientation,
    RunnerOptions
} = require('@applitools/eyes-protractor');

let eyes;

describe('protractor', function () {
    let runner

beforeAll(async () => {
        browser.waitForAngularEnabled(false)

// Create a runner with concurrency of 1
        const runnerOptions = new RunnerOptions().testConcurrency(5)
        runner = new VisualGridRunner(runnerOptions);

// Create Eyes object with the runner, meaning it'll be a Visual Grid eyes.
        eyes = new Eyes(runner);

// Initialize the eyes configuration
        const configuration = new Configuration();

// You can get your api key from the Applitools dashboard
        // configuration.setApiKey('APPLITOOLS_API_KEY')

// create a new batch info instance and set it to the configuration
        configuration.setBatch(new BatchInfo('Demo Batch - Protractor - Ultrafast'))

// Add browsers with different viewports
        configuration.addBrowser(800, 600, BrowserType.CHROME);
        configuration.addBrowser(700, 500, BrowserType.FIREFOX);
        configuration.addBrowser(1600, 1200, BrowserType.IE_11);
        configuration.addBrowser(1024, 768, BrowserType.EDGE_CHROMIUM);
        configuration.addBrowser(800, 600, BrowserType.SAFARI);

// Add mobile emulation devices in Portrait mode
        configuration.addDeviceEmulation(DeviceName.iPhone_X, ScreenOrientation.PORTRAIT);
        configuration.addDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT);

// Set the configuration to eyes
        eyes.setConfiguration(configuration);
    });

it('ultraFastTest', async () => {

// Navigate to the url we want to test
        // ⭐️ Note to see visual bugs, run the test using the above URL for the 1st run.
        // but then change the above URL to https://demo.applitools.com/index_v2.html
        // (for the 2nd run)
        await browser.get('https://demo.applitools.com');

// Call Open on eyes to initialize a test session
        await eyes.open(browser, 'Demo App - Protractor - Ultrafast', 'Smoke Test - Protractor - Ultrafast', new RectangleSize(800, 600));

// check the login page with fluent api, see more info here
        // https://applitools.com/docs/topics/sdk/the-eyes-sdk-check-fluent-api.html
        await eyes.check('Login Window', Target.window().fully());

// Click the "Log in" button.
        element(by.id("log-in")).click();

// Check the app page
        await eyes.check('App Window', Target.window().fully());

// Call Close on eyes to let the server know it should display the results
        await eyes.close();
    });

afterAll(async () => {
        // If the test was aborted before eyes.close was called, ends the test as aborted.
        await eyes.abort();

// we pass false to this method to suppress the exception that is thrown if we
        // find visual differences
        const results = await runner.getAllTestResults();
        console.log(results);
    });
});

Option 2 - Run Locally

Run the demo app

  1. Get the code:
    • Option 1: git clone https://github.com/applitools/tutorial-protractor-basic.git
    • Option 2: Download it as a Zip file and unzip it.
  2. cd tutorial-protractor-basic folder
  3. Note: Remember to set the APPLITOOLS_API_KEY env variable.
  4. npm install
  5. npm run test
  6. After you run one set of tests, you now have the baseline. 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". You can change the URL by opening the /test/classic-runner-example.test.js file.

Add Applitools to an existing project

Installing the Eyes SDK

npm install "@applitools/eyes-protractor" --save-dev

Example code

'use strict';

const { Eyes, ClassicRunner, Target, RectangleSize, Configuration, BatchInfo} = require('@applitools/eyes-protractor');

describe('DemoApp - ClassicRunner', function () {
  beforeAll(() => {
    browser.waitForAngularEnabled(false)
  })

let runner, eyes;

beforeEach(async () => {
    // Initialize the Runner for your test.
    runner = new ClassicRunner();

// Initialize the eyes SDK (IMPORTANT: make sure your API key is set in the APPLITOOLS_API_KEY env variable).
    eyes = new Eyes(runner);

// Initialize the eyes configuration.
    const conf = new Configuration()

// You can get your api key from the Applitools dashboard
    conf.setApiKey(process.env.APPLITOOLS_API_KEY)

// set new batch
    conf.setBatch(new BatchInfo("Demo Batch - Protractor - Classic"));

// set the configuration to eyes
    eyes.setConfiguration(conf)
  });

it('Smoke Test', async () => {
    // Start the test by setting AUT's name, test name and viewport size (width X height)
    await eyes.open(browser, 'Demo App - Protractor - Classic', 'Smoke Test - Protractor - Classic', new RectangleSize(800, 600));

// Navigate the browser to the "ACME" demo app.
    browser.get("https://demo.applitools.com");

// To see visual bugs after the first run, use the commented line below instead.
    // await browser.get("https://demo.applitools.com/index_v2.html");

// Visual checkpoint #1 - Check the login page.
    await eyes.check("Login Window", Target.window().fully());

// This will create a test with two test steps.
    element(by.id("log-in")).click();

// Visual checkpoint #2 - Check the app page.
    await eyes.check("App Window", Target.window().fully());

// End the test.
    await eyes.close();
  });

afterEach(async () => {
    // If the test was aborted before eyes.close was called, ends the test as aborted.
    await eyes.abort();

// Wait and collect all test results
    const allTestResults = await runner.getAllTestResults();
    console.log(allTestResults);
  });
});

Having Trouble?

Don't suffer in silence! Let us help you. Please reach out to us to get your project working.

Taking the next steps with Applitools Eyes

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.