Selenium Java SDK

For VisualTest's page on Sonatype, go here.

Requirements

  • Selenium 3+

  • Java 1.8+

Set-up

  1. Create a new Java Gradle project:

    gradle init --type java-application
  2. Add VisualTest dependency into the dependency manager file. POM files and snippets are available on this Sonatype page).

  3. Import VisualTest:

    import com.smartbear.visualtest.models.CaptureResult;
    import com.smartbear.visualtest.VisualTest;
    
  4. Hand off your webdriver to VisualTest, replacing 'PROJECT_TOKEN' with your project token:

    FirefoxDriver driver = new FirefoxDriver(options);
    driver.get("https://www.smartbear.com");
    VisualTest visualTest = new VisualTest(driver, "PROJECT_TOKEN");
  5. At each point within your functional test where you want to capture an image (fullpage, viewport, or element) for regression testing, call the capture method:

    // FULLPAGE SCREENSHOT
    visualTest.capture("Home Page");
    
    // VIEWPORT SCREENSHOT
    visualTest.capture("Home Page Viewport", true);
    
    // ELEMENT SCREENSHOT
    WebElement element = driver.findElement(By.className("Menu"))
    visualTest.capture("Menu", element);
  6. Once your test is complete, VisualTest adds a tile to your Test Runs screen, where you can review and accept any found image differences.

    Tip

    You can have VisualTest add reporting of results to the console output, including:

    • URLs to the comparisons in the VisualTest app

    • results summaries

    See Customizing VisualTest below for more information.

Customizing VisualTest

VisualTest allows script customizations that improve the quality of your testing. This section provides ways to optimize your test scripts.

Over-ride Comparison Mode

VisualTest is set to the detailed comparison mode by default (see the Comparison Mode section for more information). You can change this setting to layout mode by going to Settings.

If your tests need a combination of detailed and layout modes, you can code a given screen capture to override what you have in Settings. Below is an example. Choose from the following options:

Variable

Options

comparisonMode

'detailed'

'layout'

Variable

Options

sensitivity (layout mode only)

'low'

'medium'

'high'

VisualTest visualTest = new VisualTest(driver, "PROJECT_TOKEN");
HashMap<String, Object> options = new HashMap<String, Object>();
options.put("comparisonMode", "layout");
options.put("sensitivity", "medium");

visualTest.capture("Home Page", options);

Assign Captures to a Test Group Name

To organize your captures, assign them the same test group name by including testGroupName as a setting in the VisualTest constructor:

VisualTest visualTest = new VisualTest(driver, new HashMap<String,Object>(){{
    put("testGroupName", "YourTestGroupName");
    put("projectToken", "PROJECT_TOKEN")
}});

Explore and manage your grouped test runs effortlessly by clicking the list view icon . Easily review and navigate through the organized test runs, all while retaining access to the detailed specifics of each individual test run.

project-test-group-name.png

Lazy-loading Sites

The below runs fullpage regression tests for lazy-loaded websites, setting time to elapse between scrolls. Note that the value (1000) is the number of milliseconds to wait between scrolls:

visualTest.capture("Home Page", 1000); // number is milliseconds between scrolls

For code examples that handle dynamic, moving content, see Screenshot Consistency.

Reporting in Console Output

VisualTest can print a summary of the test run results to the console, along with a link to review the results in the VisualTest app.

For this, add the following code at the end of the test:

visualTest.printReport(); 

It produces results in this format:

console-log-output.png

Assert Comparison Results

You can aggregate all passed and failed VisualTest comparisons with the following assert:

import com.smartbear.visualtest.models.TestRunResult; //import at top of file
...
//at end of test
TestRunResult result = visualTest.getTestRunResult();
Assert.assertEquals(result.getFailed(), 0);

The structure returned in Java is as follows:

public class TestRunResult {
    Integer passed;
    Integer failed;
}

Maintenance

See Also

Publication date: