1
1
package com .applitools .example ;
2
-
3
2
import com .applitools .eyes .BatchInfo ;
4
3
import com .applitools .eyes .EyesRunner ;
5
4
import com .applitools .eyes .RectangleSize ;
6
5
import com .applitools .eyes .TestResultsSummary ;
7
6
import com .applitools .eyes .selenium .BrowserType ;
8
- import com .applitools .eyes .selenium .ClassicRunner ;
9
7
import com .applitools .eyes .selenium .Configuration ;
10
8
import com .applitools .eyes .selenium .Eyes ;
11
9
import com .applitools .eyes .selenium .fluent .Target ;
10
+ import com .applitools .eyes .visualgrid .model .ChromeEmulationInfo ;
11
+ import com .applitools .eyes .visualgrid .model .DesktopBrowserInfo ;
12
12
import com .applitools .eyes .visualgrid .model .DeviceName ;
13
13
import com .applitools .eyes .visualgrid .model .ScreenOrientation ;
14
14
import com .applitools .eyes .visualgrid .services .RunnerOptions ;
17
17
import org .openqa .selenium .WebDriver ;
18
18
import org .openqa .selenium .chrome .ChromeDriver ;
19
19
import org .openqa .selenium .chrome .ChromeOptions ;
20
- import org .openqa .selenium .remote .RemoteWebDriver ;
21
-
22
- import java .net .URL ;
23
20
import java .time .Duration ;
24
21
25
-
26
22
public class AcmeBankTests {
27
- // This class contains everything needed to run a full visual test against the ACME bank site.
28
- // It runs the test once locally.
29
- // If you use the Ultrafast Grid, then it performs cross-browser testing against multiple unique browsers.
30
- // It runs the test from a main function, not through a test framework.
31
23
32
- // Test constants
33
- private final static boolean USE_ULTRAFAST_GRID = true ;
34
- private final static boolean USE_EXECUTION_CLOUD = false ;
35
- private final static String RUNNER_NAME = (USE_ULTRAFAST_GRID ) ? "Ultrafast Grid" : "Classic runner" ;
36
- private final static BatchInfo BATCH = new BatchInfo ("Example: Selenium Java Basic with the " + RUNNER_NAME );
24
+ private final static BatchInfo BATCH = new BatchInfo ("Applitools Quickstart" );
37
25
38
26
public static void main (String [] args ) {
39
27
@@ -42,151 +30,60 @@ public static void main(String [] args) {
42
30
WebDriver driver = null ;
43
31
44
32
try {
45
- // The following steps set up Applitools for testing.
46
-
47
- if (USE_ULTRAFAST_GRID ) {
48
- // Create the runner for the Ultrafast Grid.
49
- // Concurrency refers to the number of visual checkpoints Applitools will perform in parallel.
50
- // Warning: If you have a free account, then concurrency will be limited to 1.
51
- runner = new VisualGridRunner (new RunnerOptions ().testConcurrency (5 ));
52
- }
53
- else {
54
- // Create the Classic runner.
55
- runner = new ClassicRunner ();
56
- }
57
-
58
- // Create the Applitools Eyes object connected to the runner and set its configuration.
33
+ // Configure Applitools SDK to run on the Ultrafast Grid
34
+ runner = new VisualGridRunner (new RunnerOptions ().testConcurrency (5 ));
59
35
eyes = new Eyes (runner );
60
-
61
- // Create a configuration for Applitools Eyes.
62
36
Configuration config = eyes .getConfiguration ();
63
-
64
- // Set the Applitools API key so test results are uploaded to your account.
65
- // If you don't explicitly set the API key with this call,
66
- // then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it.
67
37
config .setApiKey (System .getenv ("APPLITOOLS_API_KEY" ));
68
-
69
- // Read the headless mode setting from an environment variable.
70
- // Use headless mode for Continuous Integration (CI) execution.
71
- // Use headed mode for local development.
72
- boolean headless = Boolean .parseBoolean (System .getenv ().getOrDefault ("HEADLESS" , "false" ));
73
-
74
- // Create a new batch for tests.
75
- // A batch is the collection of visual tests.
76
- // Batches are displayed in the Eyes Test Manager, so use meaningful names.
77
38
config .setBatch (BATCH );
78
-
79
- // If running tests on the Ultrafast Grid, configure browsers.
80
- if (USE_ULTRAFAST_GRID ) {
81
-
82
- // Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid.
83
- // Other browsers are also available, like Edge and IE.
84
- config .addBrowser (800 , 600 , BrowserType .CHROME );
85
- config .addBrowser (1600 , 1200 , BrowserType .FIREFOX );
86
- config .addBrowser (1024 , 768 , BrowserType .SAFARI );
87
-
88
- // Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid.
89
- // Other mobile devices are available, including iOS.
90
- config .addDeviceEmulation (DeviceName .Pixel_2 , ScreenOrientation .PORTRAIT );
91
- config .addDeviceEmulation (DeviceName .Nexus_10 , ScreenOrientation .LANDSCAPE );
92
- }
93
-
94
- // Set the configuration for Eyes
39
+ config .addBrowsers (
40
+ new DesktopBrowserInfo (800 , 1024 , BrowserType .CHROME ),
41
+ new DesktopBrowserInfo (1600 , 1200 , BrowserType .FIREFOX ),
42
+ new DesktopBrowserInfo (1024 , 768 , BrowserType .SAFARI ),
43
+ new ChromeEmulationInfo (DeviceName .Pixel_2 , ScreenOrientation .PORTRAIT ),
44
+ new ChromeEmulationInfo (DeviceName .Nexus_10 , ScreenOrientation .LANDSCAPE )
45
+ );
95
46
eyes .setConfiguration (config );
96
-
97
- // Create ChromeDriver options
98
- ChromeOptions options = new ChromeOptions ().setHeadless (headless );
99
-
100
- if (USE_EXECUTION_CLOUD ) {
101
- // Open the browser remotely in the Execution Cloud.
102
- driver = new RemoteWebDriver (new URL (Eyes .getExecutionCloudURL ()), options );
103
- }
104
- else {
105
- // Open the browser with a local ChromeDriver instance.
106
- driver = new ChromeDriver (options );
107
- }
108
-
109
- // Set an implicit wait of 10 seconds.
110
- // For larger projects, use explicit waits for better control.
111
- // https://www.selenium.dev/documentation/webdriver/waits/
112
- // The following call works for Selenium 4:
47
+ ChromeOptions options = new ChromeOptions ().addArguments ("--headless=new" );
48
+ driver = new ChromeDriver (options );
113
49
driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (10 ));
114
50
115
- // If you are using Selenium 3, use the following call instead:
116
- // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS );
117
-
51
+ // Start Applitools Visual AI Test
52
+ eyes . open ( driver , "ACME Bank" , "Log into a bank account" , new RectangleSize ( 1200 , 600 ) );
53
+ driver . get ( "https://sandbox.applitools.com/bank?layoutAlgo=true" );
118
54
119
- // The following steps are a test covering login for the Applitools demo site, which is a dummy banking app.
120
- // The interactions use typical Selenium WebDriver calls,
121
- // but the verifications use one-line snapshot calls with Applitools Eyes.
122
- // If the page ever changes, then Applitools will detect the changes and highlight them in the Eyes Test Manager.
123
- // Traditional assertions that scrape the page for text values are not needed here.
124
-
125
- // Open Eyes to start visual testing.
126
- // It is a recommended practice to set all four inputs:
127
- eyes .open (
128
-
129
- // WebDriver object to "watch".
130
- driver ,
131
-
132
- // The name of the application under test.
133
- // All tests for the same app should share the same app name.
134
- // Set this name wisely: Applitools features rely on a shared app name across tests.
135
- "ACME Bank Web App" ,
136
-
137
- // The name of the test case for the given application.
138
- // Additional unique characteristics of the test may also be specified as part of the test name,
139
- // such as localization information ("Home Page - EN") or different user permissions ("Login by admin").
140
- "Log into bank account" ,
141
-
142
- // The viewport size for the local browser.
143
- // Eyes will resize the web browser to match the requested viewport size.
144
- // This parameter is optional but encouraged in order to produce consistent results.
145
- new RectangleSize (1200 , 600 ));
146
-
147
- // Load the login page.
148
- driver .get ("https://demo.applitools.com" );
149
-
150
- // Verify the full login page loaded correctly.
55
+ // Full Page - Visual AI Assertion
151
56
eyes .check (Target .window ().fully ().withName ("Login page" ));
152
57
153
- // Perform login.
154
- driver .findElement (By .id ("username" )).sendKeys ("applibot" );
155
- driver .findElement (By .id ("password" )).sendKeys ("I<3VisualTests" );
58
+ driver .findElement (By .id ("username" )).sendKeys ("user" );
59
+ driver .findElement (By .id ("password" )).sendKeys ("password" );
156
60
driver .findElement (By .id ("log-in" )).click ();
157
61
158
- // Verify the full main page loaded correctly.
159
- // This snapshot uses LAYOUT match level to avoid differences in closing time text.
160
- eyes .check (Target .window ().fully ().withName ("Main page" ).layout ());
161
-
162
- // Close Eyes to tell the server it should display the results.
62
+ // Full Page - Visual AI Assertion
63
+ eyes .check (
64
+ Target .window ().fully ().withName ("Main page" )
65
+ // Uncomment to apply Layout regions and have test pass
66
+ /* .layout(
67
+ By.cssSelector(".dashboardOverview_accountBalances__3TUPB"),
68
+ By.cssSelector(".dashboardTable_dbTable___R5Du")
69
+ ) */
70
+ );
71
+
72
+ // End Applitools Visual AI Test
163
73
eyes .closeAsync ();
164
74
}
165
75
catch (Exception e ) {
166
- // Dump any errors and abort any tests.
167
76
e .printStackTrace ();
168
77
if (eyes != null )
169
78
eyes .abortAsync ();
170
- }
171
-
172
- try {
173
- // No matter what, perform cleanup.
79
+ } finally {
174
80
if (driver != null )
175
81
driver .quit ();
176
-
177
- // Close the batch and report visual differences to the console.
178
- // Note that it forces execution to wait synchronously for all visual checkpoints to complete.
179
82
if (runner != null ) {
180
83
TestResultsSummary allTestResults = runner .getAllTestResults ();
181
84
System .out .println (allTestResults );
182
85
}
86
+ System .exit (0 );
183
87
}
184
- catch (Exception e ) {
185
- // Dump any cleanup errors.
186
- e .printStackTrace ();
187
- }
188
-
189
- // Always force execution to end.
190
- System .exit (0 );
191
88
}
192
- }
89
+ }
0 commit comments