How to capture screenshot in selenium webdriver using java?
- General
- Performance and Security
- Quality Engineering
How to capture screenshot in selenium webdriver using java?
In Selenium WebDriver, capturing screenshots serves the purpose of analyzing bugs or issues that may occur during test execution. While Selenium WebDriver can automatically take screenshots during a test run, users also have the option to manually trigger screenshot capture by using the “TakeScreenshot” method. This method instructs the WebDriver to capture a screenshot and save it for further analysis within the Selenium framework.
Code implementation:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package mobileautomationtests; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Screenshot { public static void main(String[] args) throws IOException, InterruptedException { System.setProperty("webdriver.chrome.driver", "/home/auriga/Documents/chromedriver"); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.reduceimages.com/"); // call below two methods as per the requirement FullPageCaptureScreenshot(driver,"/home/auriga/Documents/fullscreenshot.png"); PartiCaptureScreenshot(driver,"/home/auriga/Documents/partialscreenshot.png"); System.out.println("Captured Screenshot"); Thread.sleep(2000); driver.close(); } /*** To Capture Full Page Screenshot ***/ public static void FullPageCaptureScreenshot(WebDriver webdriver, String srcfilepath) throws IOException { // converting webdriver to TakesScreenshot object TakesScreenshot srcshot = ((TakesScreenshot) webdriver); // using getScreenshotAs method and storing screenshot image in a File object File scrshotfile = srcshot.getScreenshotAs(OutputType.FILE); // moving source path to a File object. File DestiFile = new File(srcfilepath); // using copyFile method from FileUtils class to copy the image to destination file FileUtils.copyFile(scrshotfile, DestiFile); } /*** To Capture Particular Portion Screenshot ***/ public static void PartiPorCaptureScreenshot(WebDriver wdriver, String Parscrpath) throws IOException, InterruptedException { Thread.sleep(5000); WebElement PartiSection = wdriver.findElement(By.xpath("/html/body/div[4]/div[5]/div/div/div[1]/div[1]/div")); File ParSrcShot = PartiSection.getScreenshotAs(OutputType.FILE); File ParSrcShotDesti = new File(Parscrpath); FileUtils.copyFile(ParSrcShot, ParSrcShotDesti); } } |
Note:- After execution of the code, you will get the screenshot with the name(fullscreenshot.png or partialscreenshot.png) in your system at the location given in ‘srcfilepath’.
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s