Giter Site home page Giter Site logo

ashot's Introduction

aShot

release Maven Central

WebDriver Screenshot utility

  • Takes a screenshot of the WebElement on different platforms (i.e. desktop browsers, iOS Simulator Mobile Safari, Android Emulator Browser)
  • Decorates screenshots
  • Provides flexible screenshot comparison

#####WebElement view

aShot takes a screenshot in three simple steps:

  • Capture the entire page screenshot
  • Find element's size and position
  • Crop the original screenshot

As a result aShot provides an image of the WebElement images snippet

Maven dependency

<dependency>
    <groupId>ru.yandex.qatools.ashot</groupId>
    <artifactId>ashot</artifactId>
    <version>1.5.1</version>
</dependency>

#####Capturing the entire page

Different WebDrivers take screenshots in different ways. Some WebDrivers provide a screenshot of the entire page while others handle the viewport only. aShot might be configured to handle browsers with the viewport problem. This example configuration gives a screenshot of the entire page even for Chrome, Mobile Safari, etc.

new AShot()
  .shootingStrategy(new ViewportPastingStrategy())
  .takeScreenshot(webDriver);

#####Capturing the WebElement

One can take a screenshot of the particular WebElement(s). Just specify the element(s).

WebElement myWebElement = webDriver.findElement(By.cssSelector("#my_element"));
new AShot()
  .takeScreenshot(webDriver, myWebElement);

As noted earlier aShot will find element's size and position and crop original image. WebDriver API provides a method to find the WebElement's coordinates but different WebDriver implementations behave differently. In my opinion the most general approach to find coordinates is to use jQuery. So aShot uses jQuery by default. But some drivers have problems with Javascript execution such as OperaDriver. In this case there is another way to find the WebElement's coordinates.

new AShot()
  .coordsProvider(new WebDriverCoordsProvider()) //find coordinates with WebDriver API
  .takeScreenshot(webDriver, myWebElement);

Feel free to implement own CoordsProvider. Pull requests are welcome.

#####Prettifying the screenshot

So, let's take a simple screenshot of the weather snippet at Yandex.com.

new AShot()
  .takeScreenshot(webDriver, yandexWeatherElement);

Here is the result. simple weather snippet

DefaultCropper is used by default. Can we do better? Yes, we can.

new AShot()
  .withCropper(new IndentCropper()           // set custom cropper with indentation
                 .addIndentFilter(blur()))   // add filter for indented areas
  .takeScreenshot(driver, yandexWeatherElement);

indent blur weather snippet This screenshot provides more information about position relative other elements and blurs indent in order to focus on the WebElement.

#####Screenshot comparison .takeScreenshot() returns a Screenshot object which contains an image and data for comparison. One can ignore some WebElements from comparison.

Screenshot myScreenshot = new AShot()
  .addIgnoredElement(By.cssSelector("#weather .blinking_element")) // ignored element(s)
  .takeScreenshot(driver, yandexWeatherElement);

Use ImageDiffer to find a difference between two images.

ImageDiff diff = new ImageDiffer().makeDiff(myScreenshot, anotherScreenshot);
BufferedImage diffImage = diff.getMarkedImage(); // comparison result with marked differences

#####Several elements comparison (since 1.2)
Sometimes one need to take a screenshot of several independent elements. In this case aShot computes complex comparison area.

List<WebElement> elements = webDriver.findElements(By.cssSelector("#my_element, #popup"));
new AShot()
  .withCropper(new IndentCropper() 
                 .addIndentFilter(blur()))
  .takeScreenshot(webDriver, elements);

Here is the result. complex comparison area

One can see only specified elements (the header and the popup) are focused and will be compared if needed.

ashot's People

Contributors

pazone avatar vbauer avatar leonsabr avatar lanwen avatar qatools-ci avatar smecsia avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.