1

I am using selenium Web Driver(Java) to automate our web application. I need to capture and compare the each icon of the application in all browsers.

For that first I've opened the application in Firefox and captured icons images with its xpath and then saving them at a particular path. Later comparing the saved images when the application opened in another browser.

For this I have used the below code to capture the images, but the element image is not capturing, some unknown region in the screen is saving.

Please help, how to get the correct image of the element.

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage  fullImg = ImageIO.read(screenshot);
Point point = x.getLocation();
//Get width and height of the element
int eleWidth = x.getSize().getWidth();
int eleHeight = x.getSize().getHeight();
Rectangle rect = new Rectangle(point.getX(),point.getY(),eleWidth, eleHeight);
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), rect.width, rect.height);
ImageIO.write(eleScreenshot, "png", screenshot);
//Copy the element screenshot to disk
FileUtils.copyFile(screenshot, new File("E:\\ICONS\\Icon1.jpg"));
LittlePanda
  • 2,496
  • 1
  • 21
  • 33
SeJaPy
  • 284
  • 1
  • 6
  • 18
  • Your code is very similar to the code given in this answer - http://stackoverflow.com/questions/13832322/how-to-capture-the-screenshot-of-only-a-specific-element-using-selenium-webdrive. The line `Rectangle rect = new Rectangle(point.getX(),point.getY(),eleWidth, eleHeight);` is not necessary. You can write - `BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);`. I have only compared your code with the code in the question link. – LittlePanda Apr 13 '15 at 09:08
  • Yes, Initially i have searched the code to capture code in Stackoverflow and written, but its not working for me..hence posted the Question again.. – SeJaPy Apr 13 '15 at 09:16
  • Any Idea why its not capturing the specific region in my application – SeJaPy Apr 13 '15 at 09:17
  • Before you get `x.getLocation();`, can you try putting an explicit wait for the element x to be visible/clickable? Also, post the code you are trying to find element x. – LittlePanda Apr 13 '15 at 09:27
  • WebElement x=driver.findElement(By.xpath("//*[@id='CCDLinkedformToolbar_cmdPrint']")); Thread.sleep(2000); – SeJaPy Apr 13 '15 at 09:31
  • This is the code i used to identify element x. And i also tried to clicking on it and its working. – SeJaPy Apr 13 '15 at 09:33
  • Thank you, but i did everything. No use, tried with so many possibilities, fed up and finally posted here. But in the same application when the element is a linktext then its capturing properly.say WebElement x=driver.findElement(By.linkText("Help")); Please find the code below for the linktext :- – SeJaPy Apr 13 '15 at 10:28
  • Code for icon
    – SeJaPy Apr 13 '15 at 10:30
  • I dont know if this will help but try out the codes given in this question - http://stackoverflow.com/questions/2386064/how-do-i-crop-an-image-in-java – LittlePanda Apr 13 '15 at 10:50
  • Finally Succeeded in capturing the image, i have just made changes to my code as "BufferedImage eleScreenshot= fullImg.getSubimage(point.getX()+30, 95, eleWidth, eleHeight);" This is working to capture image in my application. – SeJaPy Apr 27 '15 at 10:35
  • You can post your solution as an answer. – LittlePanda Apr 27 '15 at 10:42

2 Answers2

0
    driver.switchTo().defaultContent();
    driver.switchTo().frame(driver.findElement(By.xpath("//*[@id='CWinBtn']")));
    WebElement ele =driver.findElement(By.xpath("//*[@id='CCDLinkedformToolbar_cmdPrint']"));
    try{         
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    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()+30, 95, eleWidth, eleHeight);
    ImageIO.write(eleScreenshot, "png", screenshot);
    FileUtils.copyFile(screenshot, new File("E:\\ ICONS\\Icon1.png"));
    }
    catch(Exception e){
         e.printStackTrace();
    }

Changes that i have made to my previous code are, added value to X coordinate and passed static value to Y coordinate, as per my application resolution.

SeJaPy
  • 284
  • 1
  • 6
  • 18
0

I solved this problem. This is because of Scale settings in Windows. You should open Display settings -> Scale and layout -> reset to 100% .

You can also multiply the coordinate values by the percentage of monitor scaling. For example: if the scale is 175%, then @param monitorScale = 1.75

.getSubimage((int) (point.getX()*monitorScale ), (int) (point.getY()*monitorScale ), (int) (elemWidth*monitorScale ), (int) (elemHeight*monitorScale ))
ILYA
  • 1
  • 2