Samples

Sample scripts to install SDKs

To integrate VisualTest into your UI tests, install one of our SDKs to add screenshot capture commands where you'd like to add visual coverage.

Playwright JavaScript

// npm install @smartbear/visualtest-playwright@latest
// npx sbvt-setup

import {sbvtCapture} from "@smartbear/visualtest-playwright";
import {test} from "@playwright/test";
test(`should visit Playwright example page, then take sbvtCaptures`, async ({ page })=> {
    await page.goto('https://playwright.dev/')
    await sbvtCapture(page, 'Playwright Example Page')
    await sbvtCapture(page, 'element', {
        elementCapture: ".hero"
    });
    await sbvtCapture(page, 'viewport', {
        capture: "viewport"
    })
})

Cypress

// npm install @smartbear/visualtest-cypress@latest
// npx sbvt-setup

describe('Starting Example Spec', () => {
   beforeEach('visit cypress example website', () => {
      cy.visit('https://example.cypress.io/')
   })
   it('should check cypress example page', () => {
      cy.sbvtCapture('Cypress Example Page')
   });
   it('should check an element on the page', () => {
      cy.get('.container').eq(2).sbvtCapture('element')
   });
   it('should check a viewport screenshot', () => {
      cy.sbvtCapture('viewport', {
         capture: "viewport"
      })
   });
})

Selenium JavaScript

const {Builder} = require("selenium-webdriver");
const {sbvtCapture} = require("@smartbear/visualtest-selenium");
(async () => {
    const driver = new Builder().forBrowser('chrome').build();

    await driver.get('https://www.smartbear.com/');

    await sbvtCapture(driver, 'fullpage')
    await sbvtCapture(driver, 'viewport', {
        viewport: true    })
    const element = await driver.findElement({css: "div.mx-auto:nth-child(3)" })
    await sbvtCapture(driver, 'element', {
        element    });
})();

Selenium Python

# Install the Python package:

pip install visualtest-python

# Integrate into your existing Selenium Python scripts:
from selenium import webdriver
from sbvt.visualtest import VisualTest

driver = webdriver.Safari()
settings = {'projectToken': "INSERT_PROJECT_TOKEN"}
visualTest = VisualTest(driver, settings)
try:
   driver.get("https://www.google.com/search?q=smartbear")
   visualTest.capture("Google results")
   visualTest.capture("Google results viewport", {'viewport': True})
finally:
   driver.quit()

Selenium Java

// This Sonatype page provides snippets and POM files for various dependency managers.
// Copy the code below to add the Java JAR file into your project, inserting the Test Run name and VisualTest Project Token into the file.
import com.smartbear.visualtest.models.CaptureResult;
import com.smartbear.visualtest.VisualTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import java.io.IOException;

public class Main {
   public static final String PROJECT_TOKEN = "<INSERT_VISUAL_TEST_PROJECT_TOKEN>";
   public static final String TESTRUN_NAME = "<INSERT_TEST_RUN_NAME>";
   public static void main(String[] args) throws IOException {

      // Mobile web capabilities
      FirefoxOptions options = new FirefoxOptions();
      options.addArguments("--headless");

      FirefoxDriver driver = new FirefoxDriver(options);

      driver.get("https://www.smartbear.com");

      VisualTest visualTest = new VisualTest(driver, PROJECT_TOKEN, TESTRUN_NAME);

      // FULLPAGE SCREENSHOT
      try {
         CaptureResult result = visualTest.capture(String.format("Test__%s__%s__2", "fullpage", "firefox"));
      } catch (Exception e) {
         e.printStackTrace();
      }

      // ELEMENT SCREENSHOT
      try {
         WebElement element = driver.findElement(By.xpath("//div[@class='hero__content']"));
         CaptureResult result = visualTest.capture(String.format("Test__%s__%s__2", "element", "firefox"), element);
      } catch (Exception e) {
         e.printStackTrace();
      }

      // VIEWPORT SCREENSHOT
      try {
         CaptureResult result = visualTest.capture(String.format("Test__%s__%s__2", "viewport", "firefox"), true);
      } catch (Exception e) {
         e.printStackTrace();
      }

      driver.quit();
   }
}

For detailed guidance on integrating VisualTest SDKs into your UI tests, see Software Development Kits (SDKs).

Public Repos

  • Playwright: page on npm here.

  • Cypress: page on npm here.

  • Python: page on PyPi here.

  • Java: page on Maven here.

See Also

Publication date: