Capture Multiple Screenshots in Selenium WebDriver
User want to capture multiple screen shots of your application of each page or each object/element screen shot on the page . There is a utility called multiple screenshots.jar.
How to Download and setup
1) Download the jar file from the below link
https://github.com/Pavan-and-Vikas-Utilities/MultiScreenShot/releases
2) Click on multiScreenshot.jar file
3) Associate the jar file to your project.
4) Import the packages.
Declaration of the class
MultiScreenShot mShot=new MultiScreenShot("C:\\temp\\","TestMultiScreenShot");
To use MultiScreenShot, you need to create an object of it as shown in above code. It takes 2 arguments. First is a path to save the screenshot and second is a class name. Here, class name is used to create the folder and save the screensahot inside that.
Methods
multiScreenShot (driver): Takes the full screenshot of page whenever you call the method and this method accepts WebDriver object as argument.
Ex: mShot.multiScreenShot(driver)
elementScreenshot():Takes the element screenshot in a page whenever you call the method and this method accepts 2 arguments one is WebDriver object as argument and another one is WebElement object,(i.e ) which element you want to take as screenshot.
Eg:mShot.elementScreenShot(driver, driver.findElement(By.id("search-submit")));
Minimize() : This method minimizes the browser window if the browser is maximized on windows os,it does not accept any arguments
EX: mShot.minimize();
Sample code for multiple screen shots- for Full screen shots of pages
importjava.io.IOException;
importmultiScreenShot.MultiScreenShot;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
publicclassMultiscreenshotexp {
publicstaticvoidmain(String args[]) throwsInterruptedException, IOException{
WebDriver driver;
driver =newFirefoxDriver();
MultiScreenShot mShot=new MultiScreenShot("C:\\temp\\","TestMultiScreenShot");
driver.get("https://irctc.co.in");
driver.manage().window().maximize();
mShot.multiScreenShot(driver);
WebElement ele=driver.findElement(By.xpath("//*[@id='usernameId']"));
ele.sendKeys("hello");
WebElement ele1=driver.findElement(By.xpath("//input[@name='j_password']"));
ele1.sendKeys("helloe");
WebElement ele2=driver.findElement(By.xpath("//input[@name='j_captcha']"));
ele2.sendKeys("apdid");
WebElement ele3=driver.findElement(By.xpath("//*[@id='loginbutton']"));
ele3.click();
mShot.multiScreenShot(driver);
}
}
Note : in the above code you should call multiscreenshot method in each beginning of the page, so it will take the full screen shot of the page.
Sample code for multiple screen shots- for elementScreenshot of page.
importjava.io.IOException;
importmultiScreenShot.MultiScreenShot;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
publicclassMultiscreenshotexp {
publicstaticvoidmain(String args[]) throwsInterruptedException, IOException{
WebDriver driver;
driver =newFirefoxDriver();
MultiScreenShot mShot=new MultiScreenShot("C:\\temp\\","TestMultiScreenShot");
driver.get("https://irctc.co.in");
driver.manage().window().maximize();
WebElement ele=driver.findElement(By.xpath("//*[@id='usernameId']"));
mShot.elementScreenShot(driver, ele);
ele.sendKeys("hello");
WebElement ele1=driver.findElement(By.xpath("//input[@name='j_password']"));
mShot.elementScreenShot(driver, ele1);
ele1.sendKeys("helloe");
WebElement ele2=driver.findElement(By.xpath("//input[@name='j_captcha']"));
mShot.elementScreenShot(driver, ele2);
ele2.sendKeys("apdid");
WebElement ele3=driver.findElement(By.xpath("//*[@id='loginbutton']"));
mShot.elementScreenShot(driver, ele3);
ele3.click();
}
}
Output
Note: if uses using both the methods in same programme that time output will be as below.
Comments
Post a Comment