0 votes
1 view
in Devops and Agile by (16.1k points)

I need a way to take screenshots of my functional tests. Right now I'm using Selenium 2 with C# bindings. I pretty much want to take a screenshot at the end of the test to make sure the desired page is displayed. Are there any particular tools you guys know of that I can incorporate into my C# code that will trigger a screenshot? I couldn't find a built-in Selenium 2 solution (without looking it over).

1 Answer

0 votes
by (44k points)

I describe taking a screenshot in Selenium as a three-step process, you can follow the below steps:

 Step 1) Convert web driver object to TakeScreenshot

TakesScreenshot scrShot =((TakesScreenshot)webdriver);

Step 2) Calling getScreenshotAs method which will create an image file

 File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

Step 3) Copy file to Desired Location

Example: In this example, we will take a screen capture of https://intellipaat.com & save it as C:/Test.png

package IntellipaatScreenshot;

import java.io.File;

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.firefox.FirefoxDriver;

import org.testng.annotations.Test;

public class IntellipaatScreenshot {

 

    @Test

    public void testIntellipaatScreenShot() throws Exception{

 

WebDriver driver ;

     System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");

     driver = new FirefoxDriver();

 

        //goto url

 

        driver.get("http://intellipaat.com/");

 

        //Call take a screenshot function

 

        this.takeSnapShot(driver, "c://test.png") ;     

}

/*

   * This function will take a screenshot

 

     * @param webdriver

 

     * @param fileWithPath

 

     * @throws Exception

 */

    public static void takeSnapShot(WebDriver webdriver,String fileWithPath) throws Exception{

 

        //Convert web driver object to TakeScreenshot

 

        TakesScreenshot scrShot =((TakesScreenshot)webdriver);

 

        //Call getScreenshotAs method to create an image file

 

                FileSrcFile=scrShot.getScreenshotAs(OutputType.FILE);

 

            //Move image file to the new destination

 

                File DestFile=new File(fileWithPath);

 

                //Copy file at the destination

 

                FileUtils.copyFile(SrcFile, DestFile);

 

    }

 

}

  

...