Skip to content

Instantly share code, notes, and snippets.

View angelovstanton's full-sized avatar
💭
UPDATED STATUS

Anton Angelov angelovstanton

💭
UPDATED STATUS
View GitHub Profile
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
driver = new ChromeDriver(chromeOptions);
wait.until(ExpectedConditions.visibilityOfAllElements(
driver.findElements(By.xpath("//*[@id='tve_editor']/div[2]/div[2]/div/div"))));
private void waitUntilLoaded() {
wait.until(x ->
{
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
String isReady = (String) javascriptExecutor.executeScript("return document.readyState");
return isReady.equals("complete");
});
}
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
@Test
public void executeJavaScript() {
driver.navigate().to("http://automatetheplanet.com");
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
String title = (String) javascriptExecutor.executeScript("return document.title");
System.out.println(title);
}
@Test
public void getHtmlSourceOfWebElement() {
driver.navigate().to("http://automatetheplanet.com");
var element = wait.until(ExpectedConditions.visibilityOfElementLocated(
By.xpath("/html/body/div[1]/header/div/div[2]/div/div[2]/nav")));
String sourceHtml = element.getAttribute("innerHTML");
System.out.println(sourceHtml);
}
@Test
public void takeFullScreenshot_test() throws Exception {
driver.navigate().to("http://automatetheplanet.com");
takeFullScreenshot("testImage");
}
@Test
public void takeElementScreenshot_test() throws Exception {
driver.navigate().to("http://automatetheplanet.com");
var element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/header/div/div[2]/div/div[2]/nav")));
@Test
public void takeFullScreenshot_test() throws Exception {
driver.navigate().to("http://automatetheplanet.com");
takeFullScreenshot("testImage");
}
@Test
public void takeElementScreenshot_test() throws Exception {
driver.navigate().to("http://automatetheplanet.com");
var element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/header/div/div[2]/div/div[2]/nav")));
public void takeScreenshotOfElement(WebElement element, String fileName) throws Exception {
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshotFile);
Point point = element.getLocation();
int elementWidth = element.getSize().getWidth();
int elementHeight = element.getSize().getHeight();
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), elementWidth, elementHeight);
ImageIO.write(eleScreenshot, "png", screenshotFile);
public void takeFullScreenshot(String fileName) throws Exception {
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String tempDir = getProperty("java.io.tmpdir");
File destFile = new File(Paths.get(tempDir, fileName + ".png").toString());
FileUtils.getFileUtils().copyFile(srcFile, destFile);
}