Selenium Java Tutorial

Getting Started with Applitools

Want to learn more about the Applitools platform and how it works? First get started with one of the following:

Running Tests with Applitools

Prerequisites
  1. A free Applitools account and Applitools API KEY

Tip: Unsure how to set up your API key?

Learn how to get started at Setting Up

  1. Java v8 or higher https://www.java.com/en/download/help/download_options.xml

  2. Either Eclipse or IntelliJ editor

  3. Chromedriver https://chromedriver.chromium.org/getting-started

Tip: Make sure it's in your `PATH`!

Here are some resources:

On Mac, place the chromedriver executable in /usr/local/bin folder so Eclipse and IntelliJ can find it.

  1. Maven https://maven.apache.org/

Tip: Not sure how to install?

Here are some resources:

Make sure it's in your PATH!

To test that it's working:

  • Make sure to restart the Terminal or Command line prompt to load the new environment variables.
  • Run mvn -v

You should see the version info for Maven.

TIP

  1. It's better if you add it permanently to the environment so when you open a new Terminal the values will persist. Otherwise, you may have to redo it for everytime you open the Terminal. This means you should put it in the ~/.bash_profile file (Mac) or in System variables in Windows. For more, see the Steps for adding Chromedriver to the PATH below.
  2. The Maven executable is inside /bin folder of the extracted Maven directory. So you must include /bin. It should look something like: /Users/raja/apps/apache-maven-3.5.4/bin
Option 1 - Run With The Ultrafast Grid
  1. Clone or download the repository and navigate to that folder
git clone https://github.com/applitools/tutorial-selenium-java-visualgrid.git
cd tutorial-selenium-java-visualgrid
1
2

Note: you can alternatively download the project as a Zip file and extract it

  1. Open the folder as a Maven file in Eclipse or IntelliJ.

TIP

  • Open Eclipse from Terminal so it can load the environment variables. On Mac, do something like: open /Applications/Eclipse.app

  • You may need to force download Maven dependencies

  • In Eclipse:

    • Right click on the main Project folder > Maven > Update Project
    • Select "Force Update of Snapshots/Releases" checkbox
    • Press OK
  1. Change the Applitools API key in the AppTest.java

  2. Run the test

Adding Applitools Eyes to an Existing Project

  1. Create a Maven project and add the following properties and dependencies to the pom.xml
  <dependencies>
    <!-- This is the Applitools Selenium Java SDK -->
    <dependency>
     <groupId>com.applitools</groupId>
     <artifactId>eyes-selenium-java3</artifactId>
     <version>RELEASE</version>
    </dependency>
  </dependencies>

1
2
3
4
5
6
7
8
9
  1. Create a class with name AppTest.java and add the following code. And replace the APPLITOOLS_API_KEY with your own.
package com.applitools.quickstarts;

import com.applitools.eyes.BatchInfo;
import com.applitools.eyes.RectangleSize;
import com.applitools.eyes.TestResultsSummary;
import com.applitools.eyes.selenium.BrowserType;
import com.applitools.eyes.selenium.Configuration;
import com.applitools.eyes.selenium.Eyes;
import com.applitools.eyes.selenium.fluent.Target;
import com.applitools.eyes.visualgrid.model.DeviceName;
import com.applitools.eyes.visualgrid.model.ScreenOrientation;
import com.applitools.eyes.visualgrid.services.VisualGridRunner;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * Unit test for simple App.
 */
public class AppTest {

  public static void main(String[] args) {
    // Create a new chrome web driver
    WebDriver webDriver = new ChromeDriver();

    // Create a runner with concurrency of 1
    VisualGridRunner runner = new VisualGridRunner(1);

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

    setUp(eyes);

    try {
      // ⭐️ 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)
      ultraFastTest(webDriver, eyes);

    } finally {
      tearDown(webDriver, runner);
    }

  }

  public static void setUp(Eyes eyes) {

    // Initialize eyes Configuration
    Configuration config = new Configuration();

    // You can get your api key from the Applitools dashboard
    config.setApiKey("APPLITOOLS_API_KEY");

    // create a new batch info instance and set it to the configuration
    config.setBatch(new BatchInfo("Ultrafast Batch"));

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

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

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

  }

  public static void ultraFastTest(WebDriver webDriver, Eyes eyes) {

    try {

      // Navigate to the url we want to test
      webDriver.get("https://demo.applitools.com");

      // Call Open on eyes to initialize a test session
      eyes.open(webDriver, "Demo App", "Ultrafast grid demo", 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
      eyes.check(Target.window().fully().withName("Login page"));

      webDriver.findElement(By.id("log-in")).click();

      // Check the app page
      eyes.check(Target.window().fully().withName("App page"));

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

    } finally  {
      eyes.abortAsync();
    }

  }

  private static void tearDown(WebDriver webDriver, VisualGridRunner runner) {
    // Close the browser
    webDriver.quit();

    // we pass false to this method to suppress the exception that is thrown if we
    // find visual differences
    TestResultsSummary allTestResults = runner.getAllTestResults(false);
    System.out.println(allTestResults);
  }

}

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
Option 2 - Run Locally
  1. Clone or download the repository and navigate to that folder
git clone https://github.com/applitools/tutorial-selenium-java-basic.git
cd tutorial-selenium-java-basic
1
2

Note: you can alternatively download the project as a Zip file and extract it

  1. Open the folder as a Maven file in Eclipse or IntelliJ.

TIP

  • Open Eclipse from Terminal so it can load the environment variables. On Mac, do something like: open /Applications/Eclipse.app

  • You may need to force download Maven dependencies

  • In Eclipse:

    • Right click on the main Project folder > Maven > Update Project
    • Select "Force Update of Snapshots/Releases" checkbox
    • Press OK
  1. Change the Applitools API key in the BasicDemo.java

  2. Run the test

TIP

In Eclipse:

  • Right click on the Project (or anywhere in the code) > Run As > JUnit Test

In Command line, run the following Maven command:

mvn -D test=BasicDemo test
1

Adding Applitools Eyes to an Existing Project

  1. Create a Maven project and add the following properties and dependencies to the pom.xml
  <dependencies>
    <!-- This is the Applitools Selenium Java SDK -->
    <dependency>
     <groupId>com.applitools</groupId>
     <artifactId>eyes-selenium-java3</artifactId>
     <version>RELEASE</version>
    </dependency>
  </dependencies>

1
2
3
4
5
6
7
8
9
  1. Create a class with name AppTest.java and add the following code. And replace the APPLITOOLS_API_KEY with your own.
package com.applitools.quickstarts;

import static com.google.common.base.Strings.isNullOrEmpty;

import com.applitools.eyes.*;
import com.applitools.eyes.selenium.ClassicRunner;
import com.applitools.eyes.selenium.Eyes;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * Runs Applitools test for the demo app https://demo.applitools.com
 */
@RunWith(JUnit4.class)
public class BasicDemo {

  private EyesRunner runner;
  private Eyes eyes;
  private static BatchInfo batch;
  private WebDriver driver;

  @BeforeClass
  public static void setBatch() {
    // Must be before ALL tests (at Class-level)
    batch = new BatchInfo("Demo batch");
  }

  @Before
  public void beforeEach() {
    // Initialize the Runner for your test.
    runner = new ClassicRunner();

    // Initialize the eyes SDK
    eyes = new Eyes(runner);

    // Raise an error if no API Key has been found.
    if(isNullOrEmpty(System.getenv("APPLITOOLS_API_KEY"))) {
      throw new RuntimeException("No API Key found; Please set environment variable 'APPLITOOLS_API_KEY'.");
    }

    // Set your personal Applitols API Key from your environment variables.
    eyes.setApiKey(System.getenv("APPLITOOLS_API_KEY"));

    // set batch name
    eyes.setBatch(batch);

    // Use Chrome browser
    driver = new ChromeDriver();

  }

  @Test
  public void basicTest() {
    // Set AUT's name, test name and viewport size (width X height)
    // We have set it to 800 x 600 to accommodate various screens. Feel free to
    // change it.
    eyes.open(driver, "Demo App", "Smoke Test", new RectangleSize(800, 800));

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

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

    // Visual checkpoint #1 - Check the login page.
    eyes.checkWindow("Login Window");

    // This will create a test with two test steps.
    driver.findElement(By.id("log-in")).click();

    // Visual checkpoint #2 - Check the app page.
    eyes.checkWindow("App Window");

    // End the test.
    eyes.closeAsync();
  }

  @After
  public void afterEach() {
    // Close the browser.
    driver.quit();

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

    // Wait and collect all test results
    TestResultsSummary allTestResults = runner.getAllTestResults();

    // Print results
    System.out.println(allTestResults);
  }
}
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
97
98
99
100
101

More information

The documentation for the Applitools Eyes Selenium 3 Java SDK contains more information about advanced configuration.

Resources




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

Last Updated: 4/26/2021, 12:44:59 PM