Selenium Java SDK
For VisualTest's page on Sonatype, go here.
Requirements
Selenium 3+
Java 1.8+
Set-up
Create a new Java Gradle project:
gradle init --type java-application
Add VisualTest dependency into the dependency manager file. POM files and snippets are available on this Sonatype page).
Import VisualTest:
import com.smartbear.visualtest.models.CaptureResult; import com.smartbear.visualtest.VisualTest;
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");
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);
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 |
---|---|
|
|
|
Variable | Options |
---|---|
|
|
| |
|
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.
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:
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
Chrome users must keep ChromeDriver up-to-date: https://chromedriver.chromium.org/downloads.
If you experience problems with SSL services, visit Troubleshooting to learn more about this issue.