Skip to content

Selenium WebDriver screenshot and comparison

License

Notifications You must be signed in to change notification settings

anonmations/webdrivershooter

 
 

Repository files navigation

GitHub forks GitHub stars GitHub watchers

Maven Central javadoc GitHub release badge-jdk License: MIT

Table of Contents generated with DocToc

WebDriverShooter

Targets

  • Page
  • Frame
  • Element

Strategies

  • Viewport
  • Vertical Scroll
  • Horizontal Scroll
  • Full Scroll (Vertical & Horizontal)

Versions

  • Selenium: 4.9.1
  • Selenide: 6.14.1
  • JUnit5: 5.9.3
  • TestNG: 7.8.0

Extensions

It automatically provides the current WebDriver instance to com.github.ngoanh2n.wdc.WebDriverShooter.

You don't need to pass the WebDriver instance to the argument of shooting methods.

When project is using Selenide directly.

When project is using JUnit Jupiter as a testing framework.

When project is using TestNG as a testing framework.

Declarations

Gradle

Add to build.gradle

implementation("com.github.ngoanh2n:webdrivershooter:1.0.0")

Maven

Add to pom.xml

<dependency>
    <groupId>com.github.ngoanh2n</groupId>
    <artifactId>webdrivershooter</artifactId>
    <version>1.0.0</version>
</dependency>

Usages

ShooterOptions

ShooterOptions.builder()
        .setScrollDelay(300)                                // Set delay duration between scrolling times (Default to 200)
        .setMaskedColor(Color.GRAY)                         // Set color to mask areas (Default to GRAY)
        .checkDevicePixelRatio(true)                        // Indicate to check device pixel ratio or not (Default to true)
        
        /* [Shooting Strategies] */                         // Default to shootFullScroll()
        .shootViewport()                                    // Mark as taking by viewport strategy
        //.shootVerticalScroll()                            // Mark as taking by vertical scroll strategy
        //.shootHorizontalScroll()                          // Mark as taking by horizontal scroll strategy
        //.shootFullScroll()                                // Mark as taking by full scroll strategy
        
        .maskElements(bysToMask)                            // Set locators to mask over screenshot
        //.maskElements(elementsToMask)                     // Set elements to mask over screenshot
        //.maskExceptingElements(bysToIgnoreMasking)        // Set locators are not being masked over screenshot
        //.maskExceptingElements(elementsToIgnoreMasking)   // Set elements are not being masked over screenshot
        .build();

PageShooter

  • Take full page screenshot
    Screenshot screenshot = WebDriverShooter.page(driver);
  • Take full page screenshot and mask some elements
    Screenshot screenshot = WebDriverShooter.page(elementsToMask, driver);
  • Take horizontal page screenshot
    ShooterOptions options = ShooterOptions.builder()
            .shootHorizontalScroll()
            .build();
    Screenshot screenshot = WebDriverShooter.page(options, driver);

FrameShooter

  • Take full iframe screenshot
    Screenshot screenshot = WebDriverShooter.frame(frame, driver);
  • Take full iframe screenshot and mask some elements (Should be org.openqa.selenium.By)
    Screenshot screenshot = WebDriverShooter.frame(frame, bysToMask, driver);
  • Take horizontal iframe screenshot
    ShooterOptions options = ShooterOptions.builder()
            .shootHorizontalScroll()
            .build();
    Screenshot screenshot = WebDriverShooter.frame(options, frame, driver);

ElementShooter

  • Take full element screenshot
    Screenshot screenshot = WebDriverShooter.element(element, driver);
  • Take full element screenshot and mask some elements
    Screenshot screenshot = WebDriverShooter.element(element, elementsToMask, driver);
  • Take horizontal element screenshot
    ShooterOptions options = ShooterOptions.builder()
            .shootHorizontalScroll()
            .build();
    Screenshot screenshot = WebDriverShooter.element(options, element, driver);

Screenshot

  • Save the image
    Screenshot screenshot = WebDriverShooter.page(driver);
    screenshot.saveImage()
    //screenshot.saveImage(fileOuput)
  • Save the masked image
    Screenshot screenshot = WebDriverShooter.page(driver);
    screenshot.saveMaskedImage()
    //screenshot.saveMaskedImage(fileOuput)
  • Compare with other image
    Screenshot screenshot = WebDriverShooter.page(driver);
    ImageComparisonResult result = screenshot.compare(image);
  • Compare with other screenshot
    Screenshot screenshot = WebDriverShooter.page(driver);
    ImageComparisonResult result = screenshot.compare(screenshot);

About

Selenium WebDriver screenshot and comparison

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 98.5%
  • JavaScript 1.5%