Skip to content

Commit d15356a

Browse files
Updated code based on feedback
1 parent 79f4c17 commit d15356a

File tree

1 file changed

+32
-60
lines changed

1 file changed

+32
-60
lines changed

src/test/java/com/applitools/example/AcmeBankTests.java

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.openqa.selenium.By;
1515
import org.openqa.selenium.WebDriver;
1616
import org.openqa.selenium.chrome.ChromeDriver;
17-
import org.openqa.selenium.chrome.ChromeOptions;
1817

1918
import java.time.Duration;
2019

@@ -25,52 +24,34 @@ public class AcmeBankTests {
2524
// and then it performs cross-browser testing against multiple unique browsers in Applitools Ultrafast Grid.
2625
// It runs the test from a main function, not through a test framework.
2726

28-
// Test control inputs to read once and share for all tests
29-
private String applitoolsApiKey;
30-
private boolean headless;
31-
32-
// Applitools objects to share for all tests
33-
private BatchInfo batch;
34-
private Configuration config;
27+
// Test objects
3528
private VisualGridRunner runner;
36-
37-
// Test-specific objects
38-
private WebDriver driver;
3929
private Eyes eyes;
30+
private WebDriver driver;
4031

41-
public void setUpConfigAndRunner() {
32+
public void setUpBrowserWithEyes() {
4233
// This method sets up the configuration for running visual tests in the Ultrafast Grid.
4334

44-
// Read the Applitools API key from an environment variable.
45-
// To find your Applitools API key:
46-
// https://applitools.com/tutorials/getting-started/setting-up-your-environment.html
47-
applitoolsApiKey = System.getenv("APPLITOOLS_API_KEY");
48-
49-
// Read the headless mode setting from an environment variable.
50-
// Use headless mode for Continuous Integration (CI) execution.
51-
// Use headed mode for local development.
52-
headless = Boolean.parseBoolean(System.getenv().getOrDefault("HEADLESS", "true"));
53-
5435
// Create the runner for the Ultrafast Grid.
5536
// Concurrency refers to the number of visual checkpoints Applitools will perform in parallel.
5637
// Warning: If you have a free account, then concurrency will be limited to 1.
5738
runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5));
5839

59-
// Create a new batch for tests.
60-
// A batch is the collection of visual checkpoints for a test suite.
61-
// Batches are displayed in the dashboard, so use meaningful names.
62-
batch = new BatchInfo("Example: Selenium Java Basic with the Ultrafast Grid");
40+
// Create the Applitools Eyes object connected to the VisualGridRunner and set its configuration.
41+
eyes = new Eyes(runner);
6342

6443
// Create a configuration for Applitools Eyes.
65-
config = new Configuration();
44+
Configuration config = eyes.getConfiguration();
6645

6746
// Set the Applitools API key so test results are uploaded to your account.
6847
// If you don't explicitly set the API key with this call,
6948
// then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it.
70-
config.setApiKey(applitoolsApiKey);
49+
config.setApiKey(System.getenv("APPLITOOLS_API_KEY"));
7150

72-
// Set the batch for the config.
73-
config.setBatch(batch);
51+
// Create a new batch for tests.
52+
// A batch is the collection of visual tests.
53+
// Batches are displayed in the dashboard, so use meaningful names.
54+
config.setBatch(new BatchInfo("Example: Selenium Java Basic with the Ultrafast Grid"));
7455

7556
// Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid.
7657
// Other browsers are also available, like Edge and IE.
@@ -82,15 +63,14 @@ public void setUpConfigAndRunner() {
8263
// Other mobile devices are available, including iOS.
8364
config.addDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT);
8465
config.addDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE);
85-
}
8666

87-
public void openBrowserAndEyes() {
88-
// This method sets up each test with its own ChromeDriver and Applitools Eyes objects.
67+
// Set the configuration for Eyes
68+
eyes.setConfiguration(config);
8969

9070
// Open the browser with the ChromeDriver instance.
9171
// Even though this test will run visual checkpoints on different browsers in the Ultrafast Grid,
9272
// it still needs to run the test one time locally to capture snapshots.
93-
driver = new ChromeDriver(new ChromeOptions().setHeadless(headless));
73+
driver = new ChromeDriver();
9474

9575
// Set an implicit wait of 10 seconds.
9676
// For larger projects, use explicit waits for better control.
@@ -100,18 +80,6 @@ public void openBrowserAndEyes() {
10080

10181
// If you are using Selenium 3, use the following call instead:
10282
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
103-
104-
// Create the Applitools Eyes object connected to the VisualGridRunner and set its configuration.
105-
eyes = new Eyes(runner);
106-
eyes.setConfiguration(config);
107-
108-
// Open Eyes to start visual testing.
109-
// It is a recommended practice to set all four inputs:
110-
eyes.open(
111-
driver, // WebDriver object to "watch"
112-
"ACME Bank Web App", // The name of the app under test
113-
"Log into bank account", // The name of the test case
114-
new RectangleSize(1024, 768)); // The viewport size for the local browser
11583
}
11684

11785
public void logIntoBankAccount() {
@@ -121,6 +89,14 @@ public void logIntoBankAccount() {
12189
// If the page ever changes, then Applitools will detect the changes and highlight them in the dashboard.
12290
// Traditional assertions that scrape the page for text values are not needed here.
12391

92+
// Open Eyes to start visual testing.
93+
// It is a recommended practice to set all four inputs:
94+
eyes.open(
95+
driver, // WebDriver object to "watch"
96+
"ACME Bank Web App", // The name of the app under test
97+
"Log into bank account", // The name of the test case
98+
new RectangleSize(1200, 600)); // The viewport size for the local browser
99+
124100
// Load the login page.
125101
driver.get("https://demo.applitools.com");
126102

@@ -135,22 +111,19 @@ public void logIntoBankAccount() {
135111
// Verify the full main page loaded correctly.
136112
// This snapshot uses LAYOUT match level to avoid differences in closing time text.
137113
eyes.check(Target.window().fully().withName("Main page").layout());
114+
115+
// Close Eyes to tell the server it should display the results.
116+
eyes.closeAsync();
138117
}
139118

140-
public void cleanUpTest() {
119+
public void cleanUpTests() {
141120

142121
// Quit the WebDriver instance.
143122
driver.quit();
144-
145-
// Close Eyes to tell the server it should display the results.
146-
eyes.closeAsync();
147-
148-
// Warning: `eyes.closeAsync()` will NOT wait for visual checkpoints to complete.
149-
// You will need to check the Applitools dashboard for visual results per checkpoint.
150-
// If you want to wait synchronously for all checkpoints to complete, then use `eyes.close()`.
151123
}
152124

153125
public void abortTests() {
126+
154127
// Abort tests if things go wrong.
155128
eyes.abortAsync();
156129
}
@@ -170,26 +143,25 @@ public static void main(String [] args) {
170143

171144
try {
172145
// Safely perform setup.
173-
tests.setUpConfigAndRunner();
174-
tests.openBrowserAndEyes();
146+
tests.setUpBrowserWithEyes();
175147

176148
// Run the test steps.
177149
tests.logIntoBankAccount();
178150
}
179151
catch (Exception e) {
180-
// Dump any errors.
152+
// Dump any errors and abort any tests.
181153
e.printStackTrace();
154+
tests.abortTests();
182155
}
183156

184157
try {
185158
// No matter what, perform cleanup.
186-
tests.cleanUpTest();
159+
tests.cleanUpTests();
187160
tests.printResults();
188161
}
189162
catch (Exception e) {
190-
// Dump any errors and abort any remaining tests.
163+
// Dump any cleanup errors.
191164
e.printStackTrace();
192-
tests.abortTests();
193165
}
194166
finally {
195167
// Always force execution to end.

0 commit comments

Comments
 (0)