1.

How to take multiple screenshots in Selenium WebDriver?

Answer»

In testing, it is very much necessary to store test result DATA to prove the certain test is passed/failed or functionality is covered or not and you can take SCREENSHOTS as test result or proof, which can be used for reference later. The same way we need to take screenshots as proof in automation testing also. During automated testing of continuous application flow, you might want to capture a series of screenshots. Selenium WebDriver has a nice little utility MultiScreenShot to capture multiple screenshots. This utility ALLOWS you to take a screenshot of individual elements and minimize the browser.

We will see how to setup MultiScreenShot utility with selenium WebDriver and capture multiple screenshots. We will use Google.com for the explanation.

  • First, download the MultiScreenShot jar file.
  • Add jar in your Selenium Webdriver project by adding it to Java Build Path.
  • Now import the package in your class file.
  • Let us create an object to use MultiScreenShot. Pass two arguments that are the path to SAVE screenshot and class name to create the folder and save the screenshot inside that.
    Example: path=C:UsersxxxxxxDesktopTestMultiScrShot
                     className=ScreenShots

Full page screenshot is taken when you call the method and this method accepts WebDriver object as an argument.

Example: multiScrShot.multiScreenShot(driver);

Screenshot of elements on a page is taken when you call method and method accepts WebDriver object as argument and WebElement object

Example: mShot.elementScreenShot(driver, driver.findElement(By.id("search-submit")));

Minimize the browser window when the browser window is maximized. No argument is required to pass.

Example:  multiScrShot.minimize();

package testPackage; import java.awt.AWTException; import java.io.IOException; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import multiScreenShot.MultiScreenShot; public class TestMultiScrShot  {     public static void main(String[] args) throws IOException, AWTException      {       MultiScreenShot multiScrShot=new MultiScreenShot("C:New","TestMultiScrShot");         WebDriver driver =new FirefoxDriver();         driver.get("https://google.com");         //maximize the window         driver.manage().window().maximize();         //take full screenshot using MultiScreenShot class         multiScrShot.multiScreenShot(driver);         //take element screenshot using MultiScreenShot class         multiScrShot.elementScreenShot(driver, driver.findElement(By.name("btnK")));         //NAVIGATE to https://nikasio.com         driver.navigate().to("https://nikasio.com");         //take element screenshot using MultiScreenShot class        multiScrShot.elementScreenShot(driver,   driver.findElement(By.id("slide-2-layer-2")));         //take full screenshot using MultiScreenShot class         multiScrShot.multiScreenShot(driver);         //minimize the window using MultiScreenShot class         multiScrShot.minimize();         driver.quit();     } }


Discussion

No Comment Found