Run sample web app visual test

Step 1: Setup environment

Requirements

  • JDK (Highly recommended to use JDK 8 SE)
  • Java build framework
  • Access key
    • You can generate an Access key using the following instruction – Access key
  • Billing unit
    • The billing unit is your team’s unique identifier, or just the word “personal” in the case of an individual account.
      More information can be found here.

Step 2: Run sample test

After setup up the environment, you can run your first web visual test

package com.mobitru;

import org.junit.jupiter.api.*;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.AbstractDriverOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import static java.lang.String.format;
import static java.time.Duration.ofMinutes;
import static java.time.Duration.ofSeconds;
import static java.util.Optional.ofNullable;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.openqa.selenium.By.className;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfAllElementsLocatedBy;


public class SeleniumVisualDemoCapsTest {

    private static final String PROJECT_NAME = "--------- BILLING_UNIT -----------";

    private static final String API_KEY = "--------- YOU API KEY -----------";
    private static final String SELENIUM_HUB = "browserhub-us.mobitru.com";

    private final AbstractDriverOptions<? extends MutableCapabilities> options;

    private RemoteWebDriver driver = null;

    public SeleniumVisualDemoCapsTest() {
        options = new ChromeOptions();
        Map<String, Object> mobitruOptions = new HashMap<>();
        Map<String, Object> visualTestingBuildInfo = Map.of(
                "name", "Build 1",
                "project", "Java Automation Demo for JavaExecutor",
                "branch", "main",
                "failOnError", false
        );
        mobitruOptions.put("visualTesting", visualTestingBuildInfo);
        options.setCapability("mobitru:options", mobitruOptions);
    }

    private static final String VISUAL_SUITE_NAME = "Java Web Selenium Automation Demo";
    private static final String VISUAL_TEST_NAME = "Web Selenium demo";

    @BeforeEach
    public void before() throws Exception {
        URL connectionUrl = new URL(format("https://%s:%s@%s/wd/hub", PROJECT_NAME, API_KEY, SELENIUM_HUB));
        driver = new RemoteWebDriver(connectionUrl, options);
        driver.manage().timeouts()
                .pageLoadTimeout(ofMinutes(1));
    }

    @Test
    public void demoTest() {
        final String openUrl = "https://mobitru.com/";
        driver.get(openUrl);
        new WebDriverWait(driver, ofMinutes(1), ofSeconds(1))
                .withMessage("Page was not loaded")
                .until(driver -> presenceOfAllElementsLocatedBy(className("get-access")));
        assertEquals(openUrl, driver.getCurrentUrl(),
                "Current url is incorrect");
        assertEquals("Mobitru: Real Devices, Browsers & AI - in One Enterprise Testing Platform", driver.getTitle(),
                "Page title is incorrect");
        submitSnapshot("after_open", VISUAL_TEST_NAME, VISUAL_SUITE_NAME, driver);
    }

    @AfterEach
    public void after() {
        ofNullable(driver).ifPresent(RemoteWebDriver::quit);
    }

    private void submitSnapshot(String name, String testName, String suiteName, RemoteWebDriver driver) {
        Map<String, Object> snapshotData = Map.of(
                "name", name,
                "testName", testName,
                "suiteName", suiteName
        );
        driver.executeScript("mobitru:visualCheck", snapshotData);
    }
}

Scroll to Top