From 4ff031643b49fa6d2fe5eb9140d03daed01db996 Mon Sep 17 00:00:00 2001 From: mjapplitools Date: Sun, 26 Nov 2023 09:31:55 -0800 Subject: [PATCH 1/2] setting up new format --- .../com/applitools/example/AcmeBankTests.java | 172 ++++-------------- 1 file changed, 35 insertions(+), 137 deletions(-) diff --git a/src/test/java/com/applitools/example/AcmeBankTests.java b/src/test/java/com/applitools/example/AcmeBankTests.java index 610a9fa..12c4fe0 100644 --- a/src/test/java/com/applitools/example/AcmeBankTests.java +++ b/src/test/java/com/applitools/example/AcmeBankTests.java @@ -1,14 +1,14 @@ package com.applitools.example; - import com.applitools.eyes.BatchInfo; import com.applitools.eyes.EyesRunner; import com.applitools.eyes.RectangleSize; import com.applitools.eyes.TestResultsSummary; import com.applitools.eyes.selenium.BrowserType; -import com.applitools.eyes.selenium.ClassicRunner; import com.applitools.eyes.selenium.Configuration; import com.applitools.eyes.selenium.Eyes; import com.applitools.eyes.selenium.fluent.Target; +import com.applitools.eyes.visualgrid.model.ChromeEmulationInfo; +import com.applitools.eyes.visualgrid.model.DesktopBrowserInfo; import com.applitools.eyes.visualgrid.model.DeviceName; import com.applitools.eyes.visualgrid.model.ScreenOrientation; import com.applitools.eyes.visualgrid.services.RunnerOptions; @@ -17,23 +17,11 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; -import org.openqa.selenium.remote.RemoteWebDriver; - -import java.net.URL; import java.time.Duration; - public class AcmeBankTests { - // This class contains everything needed to run a full visual test against the ACME bank site. - // It runs the test once locally. - // If you use the Ultrafast Grid, then it performs cross-browser testing against multiple unique browsers. - // It runs the test from a main function, not through a test framework. - // Test constants - private final static boolean USE_ULTRAFAST_GRID = true; - private final static boolean USE_EXECUTION_CLOUD = false; - private final static String RUNNER_NAME = (USE_ULTRAFAST_GRID) ? "Ultrafast Grid" : "Classic runner"; - private final static BatchInfo BATCH = new BatchInfo("Example: Selenium Java Basic with the " + RUNNER_NAME); + private final static BatchInfo BATCH = new BatchInfo("Applitools Quickstart"); public static void main(String [] args) { @@ -42,151 +30,61 @@ public static void main(String [] args) { WebDriver driver = null; try { - // The following steps set up Applitools for testing. - - if (USE_ULTRAFAST_GRID) { - // Create the runner for the Ultrafast Grid. - // Concurrency refers to the number of visual checkpoints Applitools will perform in parallel. - // Warning: If you have a free account, then concurrency will be limited to 1. - runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5)); - } - else { - // Create the Classic runner. - runner = new ClassicRunner(); - } - - // Create the Applitools Eyes object connected to the runner and set its configuration. + // Configure Applitools SDK to run on the Ultrafast Grid + runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5)); eyes = new Eyes(runner); - - // Create a configuration for Applitools Eyes. Configuration config = eyes.getConfiguration(); - - // Set the Applitools API key so test results are uploaded to your account. - // If you don't explicitly set the API key with this call, - // then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it. config.setApiKey(System.getenv("APPLITOOLS_API_KEY")); - - // Read the headless mode setting from an environment variable. - // Use headless mode for Continuous Integration (CI) execution. - // Use headed mode for local development. - boolean headless = Boolean.parseBoolean(System.getenv().getOrDefault("HEADLESS", "false")); - - // Create a new batch for tests. - // A batch is the collection of visual tests. - // Batches are displayed in the Eyes Test Manager, so use meaningful names. config.setBatch(BATCH); - - // If running tests on the Ultrafast Grid, configure browsers. - if (USE_ULTRAFAST_GRID) { - - // Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid. - // Other browsers are also available, like Edge and IE. - config.addBrowser(800, 600, BrowserType.CHROME); - config.addBrowser(1600, 1200, BrowserType.FIREFOX); - config.addBrowser(1024, 768, BrowserType.SAFARI); - - // Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid. - // Other mobile devices are available, including iOS. - config.addDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT); - config.addDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE); - } - - // Set the configuration for Eyes + config.addBrowsers( + new DesktopBrowserInfo(800, 1024, BrowserType.CHROME), + new DesktopBrowserInfo(1600, 1200, BrowserType.FIREFOX), + new DesktopBrowserInfo(1024, 768, BrowserType.SAFARI), + new ChromeEmulationInfo(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT), + new ChromeEmulationInfo(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE) + ); eyes.setConfiguration(config); - - // Create ChromeDriver options - ChromeOptions options = new ChromeOptions().setHeadless(headless); - - if (USE_EXECUTION_CLOUD) { - // Open the browser remotely in the Execution Cloud. - driver = new RemoteWebDriver(new URL(Eyes.getExecutionCloudURL()), options); - } - else { - // Open the browser with a local ChromeDriver instance. - driver = new ChromeDriver(options); - } - - // Set an implicit wait of 10 seconds. - // For larger projects, use explicit waits for better control. - // https://www.selenium.dev/documentation/webdriver/waits/ - // The following call works for Selenium 4: + ChromeOptions options = new ChromeOptions().addArguments("--headless=new"); + driver = new ChromeDriver(options); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); - // If you are using Selenium 3, use the following call instead: - // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); - + // Start Applitools Visual AI Test + eyes.open(driver,"ACME Bank", "Log into a bank account", new RectangleSize(1200, 600)); + driver.get("https://sandbox.applitools.com/bank?layoutAlgo=true"); - // The following steps are a test covering login for the Applitools demo site, which is a dummy banking app. - // The interactions use typical Selenium WebDriver calls, - // but the verifications use one-line snapshot calls with Applitools Eyes. - // If the page ever changes, then Applitools will detect the changes and highlight them in the Eyes Test Manager. - // Traditional assertions that scrape the page for text values are not needed here. - - // Open Eyes to start visual testing. - // It is a recommended practice to set all four inputs: - eyes.open( - - // WebDriver object to "watch". - driver, - - // The name of the application under test. - // All tests for the same app should share the same app name. - // Set this name wisely: Applitools features rely on a shared app name across tests. - "ACME Bank Web App", - - // The name of the test case for the given application. - // Additional unique characteristics of the test may also be specified as part of the test name, - // such as localization information ("Home Page - EN") or different user permissions ("Login by admin"). - "Log into bank account", - - // The viewport size for the local browser. - // Eyes will resize the web browser to match the requested viewport size. - // This parameter is optional but encouraged in order to produce consistent results. - new RectangleSize(1200, 600)); - - // Load the login page. - driver.get("https://demo.applitools.com"); - - // Verify the full login page loaded correctly. + // Full Page - Visual AI Assertion eyes.check(Target.window().fully().withName("Login page")); - // Perform login. - driver.findElement(By.id("username")).sendKeys("applibot"); - driver.findElement(By.id("password")).sendKeys("I<3VisualTests"); + driver.findElement(By.id("username")).sendKeys("user"); + driver.findElement(By.id("password")).sendKeys("password"); driver.findElement(By.id("log-in")).click(); - // Verify the full main page loaded correctly. - // This snapshot uses LAYOUT match level to avoid differences in closing time text. - eyes.check(Target.window().fully().withName("Main page").layout()); - - // Close Eyes to tell the server it should display the results. + // Full Page - Visual AI Assertion + eyes.check( + Target.window().fully().withName("Main page") + // Uncomment to apply Layout regions and have test pass + /* + .layout( + By.cssSelector(".dashboardOverview_accountBalances__3TUPB"), + By.cssSelector(".dashboardTable_dbTable___R5Du") + ) */ + ); + + // End Applitools Visual AI Test eyes.closeAsync(); } catch (Exception e) { - // Dump any errors and abort any tests. e.printStackTrace(); if (eyes != null) eyes.abortAsync(); - } - - try { - // No matter what, perform cleanup. + } finally { if (driver != null) driver.quit(); - - // Close the batch and report visual differences to the console. - // Note that it forces execution to wait synchronously for all visual checkpoints to complete. if (runner != null) { TestResultsSummary allTestResults = runner.getAllTestResults(); System.out.println(allTestResults); } + System.exit(0); } - catch (Exception e) { - // Dump any cleanup errors. - e.printStackTrace(); - } - - // Always force execution to end. - System.exit(0); } -} +} \ No newline at end of file From 552a06a639b4ae925db80114075707122fd72a4f Mon Sep 17 00:00:00 2001 From: mjapplitools Date: Mon, 27 Nov 2023 15:09:34 -0800 Subject: [PATCH 2/2] added layout example --- .../com/applitools/example/AcmeBankTests.java | 3 +- .../applitools/example/LayoutRegionsTest.java | 90 +++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/applitools/example/LayoutRegionsTest.java diff --git a/src/test/java/com/applitools/example/AcmeBankTests.java b/src/test/java/com/applitools/example/AcmeBankTests.java index 12c4fe0..8c5e7a2 100644 --- a/src/test/java/com/applitools/example/AcmeBankTests.java +++ b/src/test/java/com/applitools/example/AcmeBankTests.java @@ -63,8 +63,7 @@ public static void main(String [] args) { eyes.check( Target.window().fully().withName("Main page") // Uncomment to apply Layout regions and have test pass - /* - .layout( + /* .layout( By.cssSelector(".dashboardOverview_accountBalances__3TUPB"), By.cssSelector(".dashboardTable_dbTable___R5Du") ) */ diff --git a/src/test/java/com/applitools/example/LayoutRegionsTest.java b/src/test/java/com/applitools/example/LayoutRegionsTest.java new file mode 100644 index 0000000..415af29 --- /dev/null +++ b/src/test/java/com/applitools/example/LayoutRegionsTest.java @@ -0,0 +1,90 @@ +package com.applitools.example; + +import com.applitools.eyes.BatchInfo; +import com.applitools.eyes.EyesRunner; +import com.applitools.eyes.RectangleSize; +import com.applitools.eyes.TestResultsSummary; +import com.applitools.eyes.selenium.BrowserType; +import com.applitools.eyes.selenium.Configuration; +import com.applitools.eyes.selenium.Eyes; +import com.applitools.eyes.selenium.fluent.Target; +import com.applitools.eyes.visualgrid.model.ChromeEmulationInfo; +import com.applitools.eyes.visualgrid.model.DesktopBrowserInfo; +import com.applitools.eyes.visualgrid.model.DeviceName; +import com.applitools.eyes.visualgrid.model.ScreenOrientation; +import com.applitools.eyes.visualgrid.services.RunnerOptions; +import com.applitools.eyes.visualgrid.services.VisualGridRunner; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; + +import java.time.Duration; + +public class LayoutRegionsTest { + + private final static BatchInfo BATCH = new BatchInfo("Applitools Quickstart"); + + public static void main(String [] args) { + + EyesRunner runner = null; + Eyes eyes = null; + WebDriver driver = null; + + try { + // Configure Applitools SDK to run on the Ultrafast Grid + runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5)); + eyes = new Eyes(runner); + Configuration config = eyes.getConfiguration(); + config.setApiKey(System.getenv("APPLITOOLS_API_KEY")); + config.setBatch(BATCH); + config.addBrowsers( + new DesktopBrowserInfo(800, 1024, BrowserType.CHROME), + new DesktopBrowserInfo(1600, 1200, BrowserType.FIREFOX), + new DesktopBrowserInfo(1024, 768, BrowserType.SAFARI), + new ChromeEmulationInfo(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT), + new ChromeEmulationInfo(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE) + ); + eyes.setConfiguration(config); + ChromeOptions options = new ChromeOptions().addArguments("--headless=new"); + driver = new ChromeDriver(options); + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); + + // Start Applitools Visual AI Test + eyes.open(driver,"ACME Bank", "Log into a bank account", new RectangleSize(1200, 600)); + driver.get("https://sandbox.applitools.com/bank?layoutAlgo=true"); + + // Full Page - Visual AI Assertion + eyes.check(Target.window().fully().withName("Login page")); + + driver.findElement(By.id("username")).sendKeys("user"); + driver.findElement(By.id("password")).sendKeys("password"); + driver.findElement(By.id("log-in")).click(); + + // Full Page - Visual AI Assertion + eyes.check( + Target.window().fully().withName("Main page") + .layout( + By.cssSelector(".dashboardOverview_accountBalances__3TUPB"), + By.cssSelector(".dashboardTable_dbTable___R5Du") + ) + ); + + // End Applitools Visual AI Test + eyes.closeAsync(); + } + catch (Exception e) { + e.printStackTrace(); + if (eyes != null) + eyes.abortAsync(); + } finally { + if (driver != null) + driver.quit(); + if (runner != null) { + TestResultsSummary allTestResults = runner.getAllTestResults(); + System.out.println(allTestResults); + } + System.exit(0); + } + } +} \ No newline at end of file