@@ -27,148 +27,122 @@ 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+ // The following steps set up Applitools for testing.
38+
39+ // Create the runner for the Ultrafast Grid.
40+ // Concurrency refers to the number of visual checkpoints Applitools will perform in parallel.
41+ // Warning: If you have a free account, then concurrency will be limited to 1.
42+ runner = new VisualGridRunner (new RunnerOptions ().testConcurrency (5 ));
43+
44+ // Create the Applitools Eyes object connected to the VisualGridRunner and set its configuration.
45+ eyes = new Eyes (runner );
46+
47+ // Create a configuration for Applitools Eyes.
48+ Configuration config = eyes .getConfiguration ();
49+
50+ // Set the Applitools API key so test results are uploaded to your account.
51+ // If you don't explicitly set the API key with this call,
52+ // then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it.
53+ config .setApiKey (System .getenv ("APPLITOOLS_API_KEY" ));
54+
55+ // Create a new batch for tests.
56+ // A batch is the collection of visual tests.
57+ // Batches are displayed in the dashboard, so use meaningful names.
58+ config .setBatch (BATCH );
59+
60+ // Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid.
61+ // Other browsers are also available, like Edge and IE.
62+ config .addBrowser (800 , 600 , BrowserType .CHROME );
63+ config .addBrowser (1600 , 1200 , BrowserType .FIREFOX );
64+ config .addBrowser (1024 , 768 , BrowserType .SAFARI );
65+
66+ // Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid.
67+ // Other mobile devices are available, including iOS.
68+ config .addDeviceEmulation (DeviceName .Pixel_2 , ScreenOrientation .PORTRAIT );
69+ config .addDeviceEmulation (DeviceName .Nexus_10 , ScreenOrientation .LANDSCAPE );
70+
71+ // Set the configuration for Eyes
72+ eyes .setConfiguration (config );
73+
74+ // Open the browser with the ChromeDriver instance.
75+ // Even though this test will run visual checkpoints on different browsers in the Ultrafast Grid,
76+ // it still needs to run the test one time locally to capture snapshots.
77+ driver = new ChromeDriver ();
78+
79+ // Set an implicit wait of 10 seconds.
80+ // For larger projects, use explicit waits for better control.
81+ // https://www.selenium.dev/documentation/webdriver/waits/
82+ // The following call works for Selenium 4:
83+ driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (10 ));
84+
85+ // If you are using Selenium 3, use the following call instead:
86+ // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
87+
88+
89+ // The following steps are a test covering 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 ();
153120 }
154121 catch (Exception e ) {
155122 // Dump any errors and abort any tests.
156123 e .printStackTrace ();
157- tests .abortTests ();
124+ if (eyes != null )
125+ eyes .abortAsync ();
158126 }
159127
160128 try {
161129 // No matter what, perform cleanup.
162- tests .cleanUpTests ();
163- tests .printResults ();
130+ if (driver != null )
131+ driver .quit ();
132+
133+ // Close the batch and report visual differences to the console.
134+ // Note that it forces execution to wait synchronously for all visual checkpoints to complete.
135+ if (runner != null ) {
136+ TestResultsSummary allTestResults = runner .getAllTestResults ();
137+ System .out .println (allTestResults );
138+ }
164139 }
165140 catch (Exception e ) {
166141 // Dump any cleanup errors.
167142 e .printStackTrace ();
168143 }
169- finally {
170- // Always force execution to end.
171- System .exit (0 );
172- }
144+
145+ // Always force execution to end.
146+ System .exit (0 );
173147 }
174148}
0 commit comments