Skip to content

Instantly share code, notes, and snippets.

@dyorgio
Created January 8, 2018 19:09
Show Gist options
  • Save dyorgio/35e9ef0ae8f37e4ce0980f64ed834366 to your computer and use it in GitHub Desktop.
Save dyorgio/35e9ef0ae8f37e4ce0980f64ed834366 to your computer and use it in GitHub Desktop.
Pure Java FullHD webpage Screenshot example
import com.machinepublishers.jbrowserdriver.JBrowserDriver;
import com.machinepublishers.jbrowserdriver.RequestHeaders;
import com.machinepublishers.jbrowserdriver.Settings;
import com.machinepublishers.jbrowserdriver.Timezone;
import com.machinepublishers.jbrowserdriver.UserAgent;
import java.io.File;
import java.util.logging.Level;
import lombok.extern.java.Log;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
/**
* <dependency>
* <groupId>org.projectlombok</groupId>
* <artifactId>lombok</artifactId>
* <version>1.16.18</version>
* <scope>provided</scope>
* </dependency>
* <dependency>
* <groupId>com.machinepublishers</groupId>
* <artifactId>jbrowserdriver</artifactId>
* <version>0.17.11</version>
* </dependency>
*
* @author dyorgio
*/
@Log
public class JBrowserDriverSandbox {
public static void main(String[] args) {
log.info("Initing browser.");
Dimension screendim = new Dimension(1920, 1080);
JBrowserDriver driver = new JBrowserDriver(//
Settings.builder()//
.timezone(Timezone.AMERICA_NEWYORK)//
.screen(screendim)//
.headless(true)
.requestHeaders(RequestHeaders.CHROME)//
.quickRender(false)//
.loggerLevel(Level.INFO)
.javaOptions("-Xmx256m")
.cache(true)//
.cacheDir(new File("./target/maven-archiver"))
.saveMedia(true)
.userAgent(UserAgent.CHROME)//
.build());
log.info("Requesting page.");
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
log.info("Element Found 1");
element.sendKeys("Cheese!");
element.submit();
log.info("Submitted 1");
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
scrFile.renameTo(new File("./screen1.png"));
log.info("Screen 1");
element = driver.findElement(By.name("q"));
log.info("Element Found 2");
element.sendKeys("Cheese 2!");
element.submit();
log.info("Submitted 2");
scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
scrFile.renameTo(new File("./screen2.png"));
log.info("Screen 2");
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment