Wednesday, June 26, 2024

Capturing Screenshots in Selenium WebDriver

 

Capturing Screenshots in Selenium WebDriver:

 

·  Capture Screenshot: The TakesScreenshot interface is used to capture the screenshot. The getScreenshotAs method captures the screenshot and stores it in a File object.

·  Save Screenshot: The screenshot is saved to a specified location using FileHandler.copy.

 

package AlertFrameWindowsBasics;

 

import java.io.File;

import java.io.IOException;

 

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class TakeScreenShotOfPage {

 

            public static void main(String[] args) throws IOException {

                        // class + interface ==  some predefined methods

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                        // . Represents current project folder name

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();

//                     driver.get("https://www.google.com/");

                        driver.get("https://mvnrepository.com/artifact/commons-io/commons-io/2.16.0");

                        //**************************************

 

                        //                     TakesScreenshot(I)  - predefined Interface in Selenium

                        //   getScreenshotAs() - used to take screenshot of web page

                        // OutputType --  interface   

                        File srcFile =    ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE) ;

 

                        System.out.println("src file="+srcFile);

                        // src file=C:\Users\Lenovo\AppData\Local\Temp\screenshot12776774740905017780.png

 

                        //         Fileutils -->    commons-io .jar file

                        // Download jar file from maven repository  and add to current project

                        // https://mvnrepository.com/

                        //

                        // add "commons -io .jar" file to project =  similar to adding selenium-java .jar file to project

 

                        // copy file into destFile

                        File destFile = new File("C:\\brahma\\Practise\\SelniumPractiseNew\\March52024MyWorkspace\\SeleniumMay52024\\src\\AlertFrameWindowsBasics\\screenshot1.png");

                        FileUtils.copyFile(srcFile, destFile);

 

                        System.out.println("ends ");

 

 

            }

 

}

           

 

 

*** FAQ / IQ  Write a code to take screen shot of the page ?

 

 

How to take screenshot manually ?

press 'prnt Sc ' button in keyboard  and Paste in word document-  -- full screens shot  with task manager and date and time

           (Or)

Alt + Prt Scr  --  and paste in word document  -->     no date and time


No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...