Screenshot specific element with Selenium - java

NOTE: I am aware this question has been asked before a few times however I am having a problem that others seem to not be having.
Even though I appear to be getting the point of the element along with its width and height correctly, the final crop is incorrect. It is like the screenshot I am taking has different dimensions to the webpage. I am using the Chrome Driver.
This is my code for attempting to get a screenshot of the google logo image:
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
WebElement ele = driver.findElement(By.id("hplogo"));
//Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = null;
try {
fullImg = ImageIO.read(screenshot);
} catch (IOException e) {
}
//Get the location of element on the page
Point point = ele.getLocation();
//Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
eleHeight);
try {
ImageIO.write(eleScreenshot, "png", screenshot);
} catch (IOException e) {
}
//Copy the element screenshot to disk
File screenshotLocation = new File("/Users/M/Desktop/stuff/logo.png");
try{
FileUtils.copyFile(screenshot, screenshotLocation);
}catch(IOException e){
}
Here is my final logo image:
Here is what it should be getting (just as a png not as gif):
Does anyone have any idea what might be going on?

Check if you have custom scaling on your operating system. I had the same problem using 125% scaling on windows.

Refer to the below screenshot for more details.
Go to your system settings > Search for the display > change the value of "Make Everything Bigger" dropdown to 100%. Then run the code again this will work fine for you.

Related

Selenide and Firefox record faulty screenshot

I'm using Selenide and Phantomjs to test a Webapplication.
Im taking the Screenshot as follows:
byte[] bytes = ((TakesScreenshot)webDriver).getScreenshotAs(OutputType.BYTES);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
returnImage = ImageIO.read(bis);
For input Elements, however, the screenshot looks like this:faulty screenshot
It actually looks like this (when using chrome/firefox)
how it's supposed to look
The interesting thing is, that when I set Selenide to use phantomjs (Configuration.Browser = "phantomjs") it takes the screenshots correctly.
It only occurs on that kind of element, too. Buttons etc are being recorded fine. Any ideas?
PS: The screenshots attached to this post are cropped, the code here takes screenshots of the entire page. In my code, I crop the screenshot only to the desired element but even on the screenshot displaying the entire screen the element is not recorded correctly.
You can take screenshot a element using below code:-
WebElement ele = driver.findElement(By.id("hplogo"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("C:\\images\\GoogleLogo_screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);
Now in above code you are getting the getWidth() and getHeight(). You can try to adjust it by adding or subtracting the value.

Exception when using headless Chrome to take screenshot of a video

I'm trying to take screenshots of a video using Selenium and Java. It works fine when I use headed Chrome, but when I want to make it headless a "java.awt.image.RasterFormatException: (y + height) is outside of Raster" is thrown in
getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
Here's the code:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
// Get URL and find image
driver.get("https://www.webcamtaxi.com/en/netherlands/north-holland/purmerend-traffic-cam.html");
Thread.sleep(5000);
while (true) {
WebElement ele = driver.findElement(By.id("insideCam"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
}
Does someone know how to fix this?

Java-Selenium : TakesScreenshot size issue

I have created 'takescreenshot' reusable method to capture screenshot and have been calling wherever I need it.
however I am facing one strange issue here. Every time this function is called the captured image size keeps increasing
e.g 252K -> 278K -> 310K -> 400K ...
These captured images I am using in ExtentReport. Also apart from selenium session image, I do see a black background image being captured not sure where it is coming from.
method code is as below:
public static void takescreenshot(ExtentTest Test,String Status){
Date d=new Date();
String CurrentTimeStamp=d.toString().replace(":", "_").replace(" ", "_");
File scrFile =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(CurrentTimeStamp+".png"));
if(Status.equals("pass")){
Test.log(LogStatus.PASS, "snapshot below:-"+CurrentTimeStamp+".png"));
}else if(Status.equals("fail")){
Test.log(LogStatus.FAIL, "snapshot below:-"+CurrentTimeStamp+".png"));
}
}
if I hardcode some existing image in extentreport code then everything works fine.
Has anyone come across this issue ever.
I had written a code to capture screenshot of elements which works fine. May be this will help you. I don't know what Extendreport is, so can't help you there.
public static void takeElementScreenshot(WebDriver driver, WebElement element){
try{
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = element.getLocation();
// Get width and height of the element
int eleWidth = element.getSize().getWidth();
int eleHeight = element.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("D:\\Screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);
}
catch(Exception e){
}
}

Understand an gif is animated or not in JAVA

I have a gif image and I would like to be able to detect whether the gif is an animated gif or not using JAVA. This question is about detection rather than displaying the gif.
I see that MIME type of animated gif isn't different of static gif.
How I can do it?
You need an ImageReader. if size is 1 its not animated, everything above is animated.
Check this out:
public Snippet() {
File f = new File("test.gif");
ImageReader is = ImageIO.getImageReadersBySuffix("GIF").next();
ImageInputStream iis;
try {
iis = ImageIO.createImageInputStream(f);
is.setInput(iis);
int images= is.getNumImages(true);
} catch (IOException e) {
e.printStackTrace();
}
}

Add image to PDF from a URL?

Im trying to add a image from a URL address to my pdf. The code is:
Image image=Image.getInstance("http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif");
image.scaleToFit((float)200.0, (float)49.0);
paragraph.add(image);
But it does not work. What can be wrong?
This is a known issue when loading .gif from a remote location with iText.
A fix for this would be to download the .gif with Java (not via the getInstance method of iText's Image class) and to use the downloaded bytes in the getInstance method of the Image class.
Edit:
I went ahead and fixed remote gif loading in iText, it is included from iText 5.4.1 and later.
Adding Image into Itext PDF is not possible through URL .
Only way to add image in PDF is download all images in to local directory and apply below code
String photoPath = Environment.getExternalStorageDirectory() + "/abc.png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap b = BitmapFactory.decodeFile(photoPath, options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap.createScaledBitmap(b, 10, 10, false);
b.compress(Bitmap.CompressFormat.PNG, 30, stream);
Image img = null;
byte[] byteArray = stream.toByteArray();
try {
img = Image.getInstance(byteArray);
} catch (BadElementException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
The way you have used to add images to IText PDF is the way that is used for adding local files, not URLs.
For URLs, this way will solve the problem.
String imageUrl = "http://www.google.com/intl/en_ALL/"
+ "images/logos/images_logo_lg.gif";
Image image = Image.getInstance(new URL(imageUrl));
You may then proceed to add this image to some previously open document, using document.add(image).
For further reference, please visit the [Java IText: Image docs].

Categories

Resources