This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ChromeOptions chromeOptions = new ChromeOptions(); | |
chromeOptions.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors"); | |
driver = new ChromeDriver(chromeOptions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wait.until(ExpectedConditions.visibilityOfAllElements( | |
driver.findElements(By.xpath("//*[@id='tve_editor']/div[2]/div[2]/div/div")))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void waitUntilLoaded() { | |
wait.until(x -> | |
{ | |
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver; | |
String isReady = (String) javascriptExecutor.executeScript("return document.readyState"); | |
return isReady.equals("complete"); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |