Using the Ashot API

The Ashot API provides the following features:

  • Takes a screenshot of an individual WebElement on different platforms (such as desktop browsers, iOS simulator, mobile Safari)
  • Decorates the screenshots
  • Provides screenshot comparison

Taking screenshots of individual WebElements is also possible with AShot.

AShot takes a screenshot in three steps:

  1. Captures a screenshot of the full page
  2. Finds the element's size and coordinates
  3. Adjusts the original screenshot by cropping

This way, AShot provides an image of the WebElement 

To use AShot, add the dependency given here to pom.xml:

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

The following code helps to take a full-page screenshot:

public class TakeScreenShot {

public static void main(String[] args) {

// Open Firefox
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir")
+ "\src\main\resources\chromedriver.exe");

WebDriver driver = new ChromeDriver();

// Maximize the window
driver.manage().window().maximize();

// Pass the url
driver.get("http://www.freecrm.com");

Screenshot screenshot = new AShot().shootingStrategy(
ShootingStrategies.viewportPasting(1000))
.takeScreenshot(driver);
try {
ImageIO.write(screenshot.getImage(), "PNG",
new File(System.getProperty("user.dir") + "//test.png"));
} catch (IOException e) {
throw new IOException();
}
}
}

The two lines mentioned here are sufficient to take a full-page screenshot:

    Screenshot screenshot = new AShot().shootingStrategy(
ShootingStrategies.viewportPasting(1000))
.takeScreenshot(driver);
try {
ImageIO.write(screenshot.getImage(), "PNG",
new File(System.getProperty("user.dir") + "//test.png"));

Next, we will see how to take screenshot of an individual WebElement.

The two lines are all that change for taking the screenshot of an individual WebElement.

Given here is the code to take a screenshot of an individual WebElement:

public class TakeScreenShot {

public static void main(String[] args) {

// Open Firefox
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir")
+ "\src\main\resources\chromedriver.exe");

WebDriver driver = new ChromeDriver();

// Maximize the window
driver.manage().window().maximize();

// Pass the url
driver.get("http://www.freecrm.com");

// Take screenshot and store as a file format
// File src = ((TakesScreenshot)
// driver).getScreenshotAs(OutputType.FILE);
WebElement element = driver.findElement(By.name("username"));
Screenshot screenshot = new AShot().shootingStrategy(
ShootingStrategies.viewportPasting(1000)).takeScreenshot(
driver, element);
try {
ImageIO.write(screenshot.getImage(), "PNG",
new File(System.getProperty("user.dir") + "//test.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

We will look at some new location techniques in Selenium WebDriver 3 next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.142.115