11package com .applitools .example ;
2-
32import com .applitools .eyes .BatchInfo ;
43import com .applitools .eyes .EyesRunner ;
54import com .applitools .eyes .RectangleSize ;
65import com .applitools .eyes .TestResultsSummary ;
76import com .applitools .eyes .selenium .BrowserType ;
8- import com .applitools .eyes .selenium .ClassicRunner ;
97import com .applitools .eyes .selenium .Configuration ;
108import com .applitools .eyes .selenium .Eyes ;
119import com .applitools .eyes .selenium .fluent .Target ;
10+ import com .applitools .eyes .visualgrid .model .ChromeEmulationInfo ;
11+ import com .applitools .eyes .visualgrid .model .DesktopBrowserInfo ;
1212import com .applitools .eyes .visualgrid .model .DeviceName ;
1313import com .applitools .eyes .visualgrid .model .ScreenOrientation ;
1414import com .applitools .eyes .visualgrid .services .RunnerOptions ;
1717import org .openqa .selenium .WebDriver ;
1818import org .openqa .selenium .chrome .ChromeDriver ;
1919import org .openqa .selenium .chrome .ChromeOptions ;
20- import org .openqa .selenium .remote .RemoteWebDriver ;
21-
22- import java .net .URL ;
2320import java .time .Duration ;
2421
25-
2622public 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.
3123
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" );
3725
3826 public static void main (String [] args ) {
3927
@@ -42,151 +30,60 @@ public static void main(String [] args) {
4230 WebDriver driver = null ;
4331
4432 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 ));
5935 eyes = new Eyes (runner );
60-
61- // Create a configuration for Applitools Eyes.
6236 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.
6737 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.
7738 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+ );
9546 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 );
11349 driver .manage ().timeouts ().implicitlyWait (Duration .ofSeconds (10 ));
11450
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" );
11854
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
15156 eyes .check (Target .window ().fully ().withName ("Login page" ));
15257
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" );
15660 driver .findElement (By .id ("log-in" )).click ();
15761
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
16373 eyes .closeAsync ();
16474 }
16575 catch (Exception e ) {
166- // Dump any errors and abort any tests.
16776 e .printStackTrace ();
16877 if (eyes != null )
16978 eyes .abortAsync ();
170- }
171-
172- try {
173- // No matter what, perform cleanup.
79+ } finally {
17480 if (driver != null )
17581 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.
17982 if (runner != null ) {
18083 TestResultsSummary allTestResults = runner .getAllTestResults ();
18184 System .out .println (allTestResults );
18285 }
86+ System .exit (0 );
18387 }
184- catch (Exception e ) {
185- // Dump any cleanup errors.
186- e .printStackTrace ();
187- }
188-
189- // Always force execution to end.
190- System .exit (0 );
19188 }
192- }
89+ }
0 commit comments