Skip to content

Instantly share code, notes, and snippets.

@fcfort
Created January 19, 2016 18:01
Show Gist options
  • Select an option

  • Save fcfort/3ac069ad18de203b90ce to your computer and use it in GitHub Desktop.

Select an option

Save fcfort/3ac069ad18de203b90ce to your computer and use it in GitHub Desktop.
Take screenshots automatically
package asdff;
import com.google.common.base.Predicate;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.util.Date;
public class Selenium2Example {
private static final String MAP_URL =
"http://www.fun.com/map/";
private static final String MAP_URL_2 = "file:///test.html";
private static final int PAGE_TIMEOUT_SECONDS = 40;
private static final Dimension WINDOW_DIMENSIONS = new Dimension(4000, 4000);
private static String createIframeHtml() {
return String.format(
"<html><body><iframe style=\"height: %dpx; width: %dpx;\" src=\"%s\"></iframe><body></html>",
WINDOW_DIMENSIONS.height, WINDOW_DIMENSIONS.width, MAP_URL);
}
public static void main(String[] args) throws IOException {
// Write iframe html
File tempFile = File.createTempFile("iframe", ".html");
Files.write(
tempFile.toPath(), createIframeHtml().getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
// Convert temp file to URI
String asURI = tempFile.toURI().toString();
System.out.println(asURI);
// Launch driver
WebDriver driver = new FirefoxDriver();
// driver.get(MAP_URL_2);
driver.get(asURI);
// Wait for the page to load
Date d = new Date();
System.out.println("Starting timer at " + d.toString());
// driver.manage().timeouts().pageLoadTimeout(PAGE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
try {
(new WebDriverWait(driver, PAGE_TIMEOUT_SECONDS))
.until(new Predicate<WebDriver>() {
public boolean apply(WebDriver arg0) {
return false;
}
});
} catch (TimeoutException e) {
}
// Take screenshot
WebDriver augmentedDriver = new Augmenter().augment(driver);
File screenshot = ((TakesScreenshot) augmentedDriver).getScreenshotAs(OutputType.FILE);
File out = new File("test");
Files.copy(screenshot.toPath(), out.toPath(), StandardCopyOption.REPLACE_EXISTING);
// Close the browser
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment