Posted

2 minutes read

Create automated website tests for free with LoadFocus.com.

If you want to take snapshots of the browser while running tests using Selenium Webdriver these are some ways to do it.
Basically the only thing you need to do is use the class below to take snapshots of the urls you want:

package com.webdriver.automation.tests;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestUtils {

private final static Logger log = LoggerFactory.getLogger(TestUtils.class);

public static void takeDriverSnapShot(WebDriver driver, String screenSnapshotName) {
File browserFile = new File(System.getProperty("java.io.tmpdir") + File.separator +screenSnapshotName + ".png");
snapshotBrowser((TakesScreenshot) driver, screenSnapshotName, browserFile);
}

private static void snapshotBrowser(TakesScreenshot driver, String screenSnapshotName, File browserFile) {
try {

File scrFile = driver.getScreenshotAs(OutputType.FILE);
log.info("PNG browser snapshot file name: \"{}\"", browserFile.toURI().toString());

FileUtils.deleteQuietly(browserFile);
FileUtils.moveFile(scrFile, browserFile);
} catch (Exception e) {
log.error("Could not create browser snapshot: " + screenSnapshotName, e);
}
}

public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");

TestUtils.takeDriverSnapShot(driver, "google_com");
driver.close();
}
}

Have a look at the main method which uses the code for taking a snapshot.

If your using TestNG as an unit testing framework, it’s even easier to take screen shots.
The code below takes screenshots of failed tests and stores in the Surefire reports directory: “/target/surefire-reports/screenShots/”.

@AfterMethod(alwaysRun = true)
public void afterMethod(ITestResult result) throws Exception {
if (!result.isSuccess())
takeScreenShoot(threadDriver, result.getMethod());

// Quit environment.
getDriver().close();
getDriver().quit();
}

public void takeScreenShoot(ThreadLocal threadDriver, ITestNGMethod testMethod) throws Exception {
WebDriver augmentedDriver = new Augmenter().augment(threadDriver.get());
File screenshot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
String nameScreenshot = testMethod.getMethodName();
String path = getPath(nameScreenshot);
FileUtils.copyFile(screenshot, new File(path));
Reporter.log("<a href="file://&quot;" target="_blank">" + this.getFileName(nameScreenshot) + "</a>");
}

private String getFileName(String nameTest) throws IOException {
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_hh.mm.ss");
Date date = new Date();
return dateFormat.format(date) + "_" + nameTest + ".png";
}

private String getPath(String nameTest) throws IOException {
File directory = new File(".");
String newFileNamePath = directory.getCanonicalPath() + "/target/surefire-reports/screenShots/" + getFileName(nameTest);
return newFileNamePath;
}

Use Selenium WebDriver to take screenshots of failed tests to have a better understanding of your failures and debug webdriver tests easier.

Also you can take a look on the previous Selenium WebDriver blog post to locate elements in a page.


Thanks for reading. I hope you learned something interesting about React!
If you found this article useful, please share it with others. Don’t forget to subscribe to get notified for the upcoming articles.