Skip to content

TakesScreenshotTestWithTimeStamp.java #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,29 +1,75 @@
package com.in28minutes.webdriver.scenarios;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;

public class capturingScreenshot {

public static WebDriver driver;

private static void captureScreenshot() throws IOException {

Date d = new Date();

String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";

import com.in28minutes.webdriver.basics.AbstractChromeWebDriverTest;
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

FileHandler.copy(screenshot, new File("./target/" + FileName));

}

public static void captureeleschreenshot(WebElement ele) throws IOException {

Date d = new Date();

String FileName = d.toString().replace(":", "_").replace(" ", "_") + ".png";

File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

public class TakesScreenshotTest extends AbstractChromeWebDriverTest {
BufferedImage fullimg = ImageIO.read(screenshot);

Point point = ele.getLocation();

int elewidth = ele.getSize().getWidth();
int eleheight = ele.getSize().getHeight();

BufferedImage elescreenshot = fullimg.getSubimage(point.getX(), point.getY(), elewidth, eleheight);
ImageIO.write(elescreenshot, "png", screenshot);

File screenshotLocation = new File("./target/"+ FileName);

FileHandler.copy(screenshot, screenshotLocation);

}

public class TakesScreenshotTest extends AbstractChromeWebDriverTest {

@Test
public void testFrames() throws IOException {

driver.get("http://localhost:8080/pages/frames-example.html");

//Operations
captureScreenshot()

driver.navigate().to("https://webkul.com/about-us/our-team/");
WebElement ele = driver.findElement(By.xpath("/html[1]/body[1]/section[2]/div[2]/div[3]/div[47]/div[1]"));
captureeleschreenshot(ele);

File screenshot = ((TakesScreenshot)driver)
.getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(screenshot,
new File("./target/" + driver + "-screenshot.png"));
}

}