Skip to content

Commit b4e6bd6

Browse files
Reduced all code to one main method
1 parent 78fa2f8 commit b4e6bd6

File tree

1 file changed

+94
-122
lines changed

1 file changed

+94
-122
lines changed

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

Lines changed: 94 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -27,140 +27,112 @@ public class AcmeBankTests {
2727
// Test constants
2828
private final static BatchInfo BATCH = new BatchInfo("Example: Selenium Java Basic with the Ultrafast Grid");
2929

30-
// Test objects
31-
private VisualGridRunner runner;
32-
private Eyes eyes;
33-
private WebDriver driver;
34-
35-
public void setUpBrowserWithEyes() {
36-
// This method sets up the configuration for running visual tests in the Ultrafast Grid.
37-
38-
// Create the runner for the Ultrafast Grid.
39-
// Concurrency refers to the number of visual checkpoints Applitools will perform in parallel.
40-
// Warning: If you have a free account, then concurrency will be limited to 1.
41-
runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5));
42-
43-
// Create the Applitools Eyes object connected to the VisualGridRunner and set its configuration.
44-
eyes = new Eyes(runner);
45-
46-
// Create a configuration for Applitools Eyes.
47-
Configuration config = eyes.getConfiguration();
48-
49-
// Set the Applitools API key so test results are uploaded to your account.
50-
// If you don't explicitly set the API key with this call,
51-
// then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it.
52-
config.setApiKey(System.getenv("APPLITOOLS_API_KEY"));
53-
54-
// Create a new batch for tests.
55-
// A batch is the collection of visual tests.
56-
// Batches are displayed in the dashboard, so use meaningful names.
57-
config.setBatch(BATCH);
58-
59-
// Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid.
60-
// Other browsers are also available, like Edge and IE.
61-
config.addBrowser(800, 600, BrowserType.CHROME);
62-
config.addBrowser(1600, 1200, BrowserType.FIREFOX);
63-
config.addBrowser(1024, 768, BrowserType.SAFARI);
64-
65-
// Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid.
66-
// Other mobile devices are available, including iOS.
67-
config.addDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT);
68-
config.addDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE);
69-
70-
// Set the configuration for Eyes
71-
eyes.setConfiguration(config);
72-
73-
// Open the browser with the ChromeDriver instance.
74-
// Even though this test will run visual checkpoints on different browsers in the Ultrafast Grid,
75-
// it still needs to run the test one time locally to capture snapshots.
76-
driver = new ChromeDriver();
77-
78-
// Set an implicit wait of 10 seconds.
79-
// For larger projects, use explicit waits for better control.
80-
// https://www.selenium.dev/documentation/webdriver/waits/
81-
// The following call works for Selenium 4:
82-
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
83-
84-
// If you are using Selenium 3, use the following call instead:
85-
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
86-
}
87-
88-
public void logIntoBankAccount() {
89-
// This test covers login for the Applitools demo site, which is a dummy banking app.
90-
// The interactions use typical Selenium WebDriver calls,
91-
// but the verifications use one-line snapshot calls with Applitools Eyes.
92-
// If the page ever changes, then Applitools will detect the changes and highlight them in the dashboard.
93-
// Traditional assertions that scrape the page for text values are not needed here.
94-
95-
// Open Eyes to start visual testing.
96-
// It is a recommended practice to set all four inputs:
97-
eyes.open(
98-
driver, // WebDriver object to "watch"
99-
"ACME Bank Web App", // The name of the app under test
100-
"Log into bank account", // The name of the test case
101-
new RectangleSize(1200, 600)); // The viewport size for the local browser
102-
103-
// Load the login page.
104-
driver.get("https://demo.applitools.com");
105-
106-
// Verify the full login page loaded correctly.
107-
eyes.check(Target.window().fully().withName("Login page"));
108-
109-
// Perform login.
110-
driver.findElement(By.id("username")).sendKeys("applibot");
111-
driver.findElement(By.id("password")).sendKeys("I<3VisualTests");
112-
driver.findElement(By.id("log-in")).click();
113-
114-
// Verify the full main page loaded correctly.
115-
// This snapshot uses LAYOUT match level to avoid differences in closing time text.
116-
eyes.check(Target.window().fully().withName("Main page").layout());
117-
118-
// Close Eyes to tell the server it should display the results.
119-
eyes.closeAsync();
120-
}
121-
122-
public void cleanUpTests() {
123-
124-
// Quit the WebDriver instance.
125-
driver.quit();
126-
}
127-
128-
public void abortTests() {
129-
130-
// Abort tests if things go wrong.
131-
eyes.abortAsync();
132-
}
133-
134-
public void printResults() {
135-
136-
// Close the batch and report visual differences to the console.
137-
// Note that it forces execution to wait synchronously for all visual checkpoints to complete.
138-
TestResultsSummary allTestResults = runner.getAllTestResults();
139-
System.out.println(allTestResults);
140-
}
141-
14230
public static void main(String [] args) {
14331

144-
// Construct the test object.
145-
AcmeBankTests tests = new AcmeBankTests();
32+
VisualGridRunner runner = null;
33+
Eyes eyes = null;
34+
WebDriver driver = null;
14635

14736
try {
148-
// Safely perform setup.
149-
tests.setUpBrowserWithEyes();
150-
151-
// Run the test steps.
152-
tests.logIntoBankAccount();
37+
// Create the runner for the Ultrafast Grid.
38+
// Concurrency refers to the number of visual checkpoints Applitools will perform in parallel.
39+
// Warning: If you have a free account, then concurrency will be limited to 1.
40+
runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5));
41+
42+
// Create the Applitools Eyes object connected to the VisualGridRunner and set its configuration.
43+
eyes = new Eyes(runner);
44+
45+
// Create a configuration for Applitools Eyes.
46+
Configuration config = eyes.getConfiguration();
47+
48+
// Set the Applitools API key so test results are uploaded to your account.
49+
// If you don't explicitly set the API key with this call,
50+
// then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it.
51+
config.setApiKey(System.getenv("APPLITOOLS_API_KEY"));
52+
53+
// Create a new batch for tests.
54+
// A batch is the collection of visual tests.
55+
// Batches are displayed in the dashboard, so use meaningful names.
56+
config.setBatch(BATCH);
57+
58+
// Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid.
59+
// Other browsers are also available, like Edge and IE.
60+
config.addBrowser(800, 600, BrowserType.CHROME);
61+
config.addBrowser(1600, 1200, BrowserType.FIREFOX);
62+
config.addBrowser(1024, 768, BrowserType.SAFARI);
63+
64+
// Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid.
65+
// Other mobile devices are available, including iOS.
66+
config.addDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT);
67+
config.addDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE);
68+
69+
// Set the configuration for Eyes
70+
eyes.setConfiguration(config);
71+
72+
// Open the browser with the ChromeDriver instance.
73+
// Even though this test will run visual checkpoints on different browsers in the Ultrafast Grid,
74+
// it still needs to run the test one time locally to capture snapshots.
75+
driver = new ChromeDriver();
76+
77+
// Set an implicit wait of 10 seconds.
78+
// For larger projects, use explicit waits for better control.
79+
// https://www.selenium.dev/documentation/webdriver/waits/
80+
// The following call works for Selenium 4:
81+
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
82+
83+
// If you are using Selenium 3, use the following call instead:
84+
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
85+
86+
// The steps below are a test covering login for the Applitools demo site, which is a dummy banking app.
87+
// The interactions use typical Selenium WebDriver calls,
88+
// but the verifications use one-line snapshot calls with Applitools Eyes.
89+
// If the page ever changes, then Applitools will detect the changes and highlight them in the dashboard.
90+
// Traditional assertions that scrape the page for text values are not needed here.
91+
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+
100+
// Load the login page.
101+
driver.get("https://demo.applitools.com");
102+
103+
// Verify the full login page loaded correctly.
104+
eyes.check(Target.window().fully().withName("Login page"));
105+
106+
// Perform login.
107+
driver.findElement(By.id("username")).sendKeys("applibot");
108+
driver.findElement(By.id("password")).sendKeys("I<3VisualTests");
109+
driver.findElement(By.id("log-in")).click();
110+
111+
// Verify the full main page loaded correctly.
112+
// This snapshot uses LAYOUT match level to avoid differences in closing time text.
113+
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();
153117
}
154118
catch (Exception e) {
155119
// Dump any errors and abort any tests.
156120
e.printStackTrace();
157-
tests.abortTests();
121+
if (eyes != null)
122+
eyes.abortAsync();
158123
}
159124

160125
try {
161126
// No matter what, perform cleanup.
162-
tests.cleanUpTests();
163-
tests.printResults();
127+
if (driver != null)
128+
driver.quit();
129+
130+
// Close the batch and report visual differences to the console.
131+
// Note that it forces execution to wait synchronously for all visual checkpoints to complete.
132+
if (runner != null) {
133+
TestResultsSummary allTestResults = runner.getAllTestResults();
134+
System.out.println(allTestResults);
135+
}
164136
}
165137
catch (Exception e) {
166138
// Dump any cleanup errors.

0 commit comments

Comments
 (0)