2

How do I take screenshot of webelement (partial screen not whole page) which fails in selenium .I have tried using Point to locate the position of webelement and 5aken the screenshot it worked , but that was hard coded I want a runtime solution. for eg I a webpage there are 3 fields 2 textbox and 1 button and test got failed ( any failure like element not found or element is disabled) due to second textbox I want to take screenshot of second textbox only at runtime . please help

1 Answers1

1

You can use a Third-Party library called aShot. It can be used for:

  • Takes a screenshot of a WebElement on different platforms (i.e. desktop browsers, iOS Simulator Mobile Safari, Android Emulator Browser)
  • Capturing Full Page Screenshots.
  • Decorates screenshots
  • Provides flexible screenshot comparison

Add this dependency to your pom.xml:

<!-- https://mvnrepository.com/artifact/ru.yandex.qatools.ashot/ashot -->
<dependency>
    <groupId>ru.yandex.qatools.ashot</groupId>
    <artifactId>ashot</artifactId>
    <version>1.5.3</version>
</dependency>

Now you can take screenshot simply by:

public void takeScreenShotWhenFailed(WebDriver driver, WebElement webElement ){
    Screenshot screenshot = new AShot().coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver, webElement);
    ImageIO.write(screenshot.getImage(),"PNG",new File(System.getProperty("user.dir") +"\\Images\\googleLogo.png"));
}

UPDATE: To detect which element failed the program you can simply surround each step with a try-catch block and in catch you can call the screenshot method, so for example:

WebElement element;
try{
   element = driver.findElement(By.id("id"));
   element.click();
}catch(Exception e){
    takeScreenShotWhenFailed(driver,element);
}
Mustahsan
  • 3,852
  • 1
  • 18
  • 34
  • Thank you .but i above code we are passing the webElement i.e. we know which webelement screenshot we want , My question is on runtime i dont know which webelement will cause failure, so need some dynamic code which will get the webelement which caused test failure at runtime and takes the screenshot? – vinay shetty Jun 11 '19 at 07:37
  • @vinayshetty you can know which element failed test by using `try-catch` block, so what you can possibly do is add the code i provided in a method which takes driver and web element as parameter, so whenever exception is thrown in catch block call this method. that will do the trick? – Mustahsan Jun 11 '19 at 07:39
  • yes i tried that also, but the problem is if whenever the exception occurs webelement is null. for eg : WebElement inputbox = null; try{ inputbox = driver.findElement(By.xpath("//input[@name='q']")); inputbox.click(); inputbox.sendKeys("java io"); }catch (Exception e){ takeshot(inputbox); } } – vinay shetty Jun 11 '19 at 07:42
  • if web element is null, it means the element is not found, so if an element does not exists how can you possibly take its screenshot? – Mustahsan Jun 11 '19 at 07:45
  • try and feel free to discuss anything else you need to solve this problem, it will work fine in case of if element is disabled but will not probably help if the element is not found, so if the element is not found you will need to capture full screen i guess – Mustahsan Jun 11 '19 at 08:01
  • Yes, just tried and it works of disabled element, thank you for help, one little help how to i set the size of the screenshot, say i am taking screenshot of textbox but also want screenshot of element besides them like label of inputbox – vinay shetty Jun 11 '19 at 08:31
  • Hi is there any way we can take on body section screenshot and ignore the header and footer section – vinay shetty Jun 11 '19 at 09:30
  • @vinayshetty yes just find the body webElement by its id or xpath and then take screenshot of that webElement – Mustahsan Jun 11 '19 at 09:32
  • `WebElement body = driver.findElement(By.id("body")); takeScreenShotWhenFailed(driver,body);` – Mustahsan Jun 11 '19 at 09:32