Skip to content

Commit

Permalink
New format (#3)
Browse files Browse the repository at this point in the history
New quickstart/integrate format
  • Loading branch information
MJApplitools authored Nov 30, 2023
1 parent c403c26 commit 78c3dc3
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 137 deletions.
171 changes: 34 additions & 137 deletions src/test/java/com/applitools/example/AcmeBankTests.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {

Expand All @@ -42,151 +30,60 @@ 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);
}
}
}
90 changes: 90 additions & 0 deletions src/test/java/com/applitools/example/LayoutRegionsTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 78c3dc3

Please sign in to comment.