|
| 1 | +package com.applitools.example; |
| 2 | + |
| 3 | +import com.applitools.eyes.BatchInfo; |
| 4 | +import com.applitools.eyes.RectangleSize; |
| 5 | +import com.applitools.eyes.TestResultsSummary; |
| 6 | +import com.applitools.eyes.selenium.BrowserType; |
| 7 | +import com.applitools.eyes.selenium.Configuration; |
| 8 | +import com.applitools.eyes.selenium.Eyes; |
| 9 | +import com.applitools.eyes.selenium.fluent.Target; |
| 10 | +import com.applitools.eyes.visualgrid.model.DeviceName; |
| 11 | +import com.applitools.eyes.visualgrid.model.ScreenOrientation; |
| 12 | +import com.applitools.eyes.visualgrid.services.RunnerOptions; |
| 13 | +import com.applitools.eyes.visualgrid.services.VisualGridRunner; |
| 14 | +import org.junit.jupiter.api.*; |
| 15 | +import org.openqa.selenium.By; |
| 16 | +import org.openqa.selenium.WebDriver; |
| 17 | +import org.openqa.selenium.chrome.ChromeDriver; |
| 18 | +import org.openqa.selenium.chrome.ChromeOptions; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +public class AcmeBankTests { |
| 24 | + // This JUnit test case class contains everything needed to run a full visual test against the ACME bank site. |
| 25 | + // It runs the test once locally, |
| 26 | + // and then it performs cross-browser testing against multiple unique browsers in Applitools Ultrafast Grid. |
| 27 | + |
| 28 | + // Test control inputs to read once and share for all tests |
| 29 | + private static String applitoolsApiKey; |
| 30 | + private static boolean headless; |
| 31 | + |
| 32 | + // Applitools objects to share for all tests |
| 33 | + private static BatchInfo batch; |
| 34 | + private static Configuration config; |
| 35 | + private static VisualGridRunner runner; |
| 36 | + |
| 37 | + // Test-specific objects |
| 38 | + private WebDriver driver; |
| 39 | + private Eyes eyes; |
| 40 | + |
| 41 | + @BeforeAll |
| 42 | + public static void setUpConfigAndRunner() { |
| 43 | + // This method sets up the configuration for running visual tests in the Ultrafast Grid. |
| 44 | + // The configuration is shared by all tests in a test suite, so it belongs in a `BeforeAll` method. |
| 45 | + // If you have more than one test class, then you should abstract this configuration to avoid duplication. |
| 46 | + |
| 47 | + // Read the Applitools API key from an environment variable. |
| 48 | + // To find your Applitools API key: |
| 49 | + // https://applitools.com/tutorials/getting-started/setting-up-your-environment.html |
| 50 | + applitoolsApiKey = System.getenv("APPLITOOLS_API_KEY"); |
| 51 | + |
| 52 | + // Read the headless mode setting from an environment variable. |
| 53 | + // Use headless mode for Continuous Integration (CI) execution. |
| 54 | + // Use headed mode for local development. |
| 55 | + headless = Boolean.parseBoolean(System.getenv().getOrDefault("HEADLESS", "true")); |
| 56 | + |
| 57 | + // Create the runner for the Ultrafast Grid. |
| 58 | + // Concurrency refers to the number of visual checkpoints Applitools will perform in parallel. |
| 59 | + // Warning: If you have a free account, then concurrency will be limited to 1. |
| 60 | + runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5)); |
| 61 | + |
| 62 | + // Create a new batch for tests. |
| 63 | + // A batch is the collection of visual checkpoints for a test suite. |
| 64 | + // Batches are displayed in the dashboard, so use meaningful names. |
| 65 | + batch = new BatchInfo("Applitools Example: Selenium Java JUnit with the Ultrafast Grid"); |
| 66 | + |
| 67 | + // Create a configuration for Applitools Eyes. |
| 68 | + config = new Configuration(); |
| 69 | + |
| 70 | + // Set the Applitools API key so test results are uploaded to your account. |
| 71 | + // If you don't explicitly set the API key with this call, |
| 72 | + // then the SDK will automatically read the `APPLITOOLS_API_KEY` environment variable to fetch it. |
| 73 | + config.setApiKey(applitoolsApiKey); |
| 74 | + |
| 75 | + // Set the batch for the config. |
| 76 | + config.setBatch(batch); |
| 77 | + |
| 78 | + // Add 3 desktop browsers with different viewports for cross-browser testing in the Ultrafast Grid. |
| 79 | + // Other browsers are also available, like Edge and IE. |
| 80 | + config.addBrowser(800, 600, BrowserType.CHROME); |
| 81 | + config.addBrowser(1600, 1200, BrowserType.FIREFOX); |
| 82 | + config.addBrowser(1024, 768, BrowserType.SAFARI); |
| 83 | + |
| 84 | + // Add 2 mobile emulation devices with different orientations for cross-browser testing in the Ultrafast Grid. |
| 85 | + // Other mobile devices are available, including iOS. |
| 86 | + config.addDeviceEmulation(DeviceName.Pixel_2, ScreenOrientation.PORTRAIT); |
| 87 | + config.addDeviceEmulation(DeviceName.Nexus_10, ScreenOrientation.LANDSCAPE); |
| 88 | + } |
| 89 | + |
| 90 | + @BeforeEach |
| 91 | + public void openBrowserAndEyes(TestInfo testInfo) { |
| 92 | + // This method sets up each test with its own ChromeDriver and Applitools Eyes objects. |
| 93 | + |
| 94 | + // Open the browser with the ChromeDriver instance. |
| 95 | + // Even though this test will run visual checkpoints on different browsers in the Ultrafast Grid, |
| 96 | + // it still needs to run the test one time locally to capture snapshots. |
| 97 | + driver = new ChromeDriver(new ChromeOptions().setHeadless(headless)); |
| 98 | + |
| 99 | + // Set an implicit wait of 10 seconds. |
| 100 | + // For larger projects, use explicit waits for better control. |
| 101 | + // https://www.selenium.dev/documentation/webdriver/waits/ |
| 102 | + // The following call works for Selenium 4: |
| 103 | + driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); |
| 104 | + |
| 105 | + // If you are using Selenium 3, use the following call instead: |
| 106 | + // driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); |
| 107 | + |
| 108 | + // Create the Applitools Eyes object connected to the VisualGridRunner and set its configuration. |
| 109 | + eyes = new Eyes(runner); |
| 110 | + eyes.setConfiguration(config); |
| 111 | + |
| 112 | + // Open Eyes to start visual testing. |
| 113 | + // It is a recommended practice to set all four inputs: |
| 114 | + eyes.open( |
| 115 | + driver, // WebDriver object to "watch" |
| 116 | + "ACME Bank Web App", // The name of the app under test |
| 117 | + testInfo.getDisplayName(), // The name of the test case |
| 118 | + new RectangleSize(1024, 768)); // The viewport size for the local browser |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void logIntoBankAccount() { |
| 123 | + // This test covers login for the Applitools demo site, which is a dummy banking app. |
| 124 | + // The interactions use typical Selenium WebDriver calls, |
| 125 | + // but the verifications use one-line snapshot calls with Applitools Eyes. |
| 126 | + // If the page ever changes, then Applitools will detect the changes and highlight them in the dashboard. |
| 127 | + // Traditional assertions that scrape the page for text values are not needed here. |
| 128 | + |
| 129 | + // Load the login page. |
| 130 | + driver.get("https://demo.applitools.com"); |
| 131 | + |
| 132 | + // Verify the full login page loaded correctly. |
| 133 | + eyes.check(Target.window().fully().withName("Login page")); |
| 134 | + |
| 135 | + // Perform login. |
| 136 | + driver.findElement(By.id("username")).sendKeys("applibot"); |
| 137 | + driver.findElement(By.id("password")).sendKeys("I<3VisualTests"); |
| 138 | + driver.findElement(By.id("log-in")).click(); |
| 139 | + |
| 140 | + // Verify the full main page loaded correctly. |
| 141 | + // This snapshot uses LAYOUT match level to avoid differences in closing time text. |
| 142 | + eyes.check(Target.window().fully().withName("Main page").layout()); |
| 143 | + } |
| 144 | + |
| 145 | + @AfterEach |
| 146 | + public void cleanUpTest() { |
| 147 | + |
| 148 | + // Quit the WebDriver instance. |
| 149 | + driver.quit(); |
| 150 | + |
| 151 | + // Close Eyes to tell the server it should display the results. |
| 152 | + eyes.closeAsync(); |
| 153 | + |
| 154 | + // Warning: `eyes.closeAsync()` will NOT wait for visual checkpoints to complete. |
| 155 | + // You will need to check the Applitools dashboard for visual results per checkpoint. |
| 156 | + // Note that "unresolved" and "failed" visual checkpoints will not cause the JUnit test to fail. |
| 157 | + |
| 158 | + // If you want the JUnit test to wait synchronously for all checkpoints to complete, then use `eyes.close()`. |
| 159 | + // If any checkpoints are unresolved or failed, then `eyes.close()` will make the JUnit test fail. |
| 160 | + } |
| 161 | + |
| 162 | + @AfterAll |
| 163 | + public static void printResults() { |
| 164 | + |
| 165 | + // Close the batch and report visual differences to the console. |
| 166 | + // Note that it forces JUnit to wait synchronously for all visual checkpoints to complete. |
| 167 | + TestResultsSummary allTestResults = runner.getAllTestResults(); |
| 168 | + System.out.println(allTestResults); |
| 169 | + } |
| 170 | +} |
0 commit comments