Showing posts with label TakesScreenshot. Show all posts
Showing posts with label TakesScreenshot. Show all posts

Friday 23 August 2013

Selenium WebDriver : - How to capture screenshot

#SeleniumWebDriver  #TakeScreenshot #TheBuddhaTree

The Problem
When we do nightly test execution, we need historical evidence to analyze & debug script failure. Sometime, it can be challenging to track down the issue that caused a failure using failure message along with stack trace. Especially when you run the test again and it passes.

A Solution
A simple way to gain insight into your test failures is to capture screenshots of AUT at the moment of failure and add the screenshots into test execution result. 

How to Implement using Selenium
Selenium WebDriver provides the TakesScreenshot interface for capturing a screenshot of a web page. This helps in test runs, showing exactly what happened when an exception or error occurred during execution, and so on.


TakesScreenshot interface provides the getScreenshotAs() method to capture a screenshot of the page displayed in the driver instance.  Example code is as follows –


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.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class TakeScreenshotOfApplication {
                WebDriver driver;

    @Test
       public void compareTitle() {
              driver = new FirefoxDriver();

              driver.get("http://google.co.in");

            // compare title & take screenshot of application if it fails
       if (driver.getTitle().contains("facebook")) {
                 System.out.println("Title matched!!");
       } else {
                 System.out.println("Title did not match");
                 takeScreenshotMethod();
            }
        }

        private void takeScreenshotMethod() {
             // code to take screen shot of browser
            File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

             // file name where image to be saved
             String screenshotpath = "C:\\google.png";
               try {
                       // save image to drive
                        FileUtils.copyFile(scrFile, new File(screenshotpath));
               } catch (IOException e) {
                       e.printStackTrace();
              }
          }

}

Expected Behavior
  1. Load the URL
  2. Take Screenshot
  3. Save Screenshot Image to local drive
Happy Learning
}

Friday 19 July 2013

How To Take A Screenshot

#SeleniumWebDriver  #TakeScreenshot #TheBuddhaTree

The Problem
When we do nightly test execution, we need historical evidence to analyze & debug script failure. Sometime, it can be challenging to track down the issue that caused a failure using failure message along with stack trace. Especially when you run the test again and it passes.

A Solution
A simple way to gain insight into your test failures is to capture screenshots of AUT at the moment of failure and add the screenshots into test execution result. 

How to Implement using Selenium
Selenium WebDriver provides the TakesScreenshot interface for capturing a screenshot of a web page. This helps in test runs, showing exactly what happened when an exception or error occurred during execution, and so on.


TakesScreenshot interface provides the getScreenshotAs() method to capture a screenshot of the page displayed in the driver instance.  Example code is as follows –


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.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class TakeScreenshotOfApplication {
                WebDriver driver;

    @Test
       public void compareTitle() {
              driver = new FirefoxDriver();

              driver.get("http://google.co.in");

            // compare title & take screenshot of application if it fails
       if (driver.getTitle().contains("facebook")) {
                 System.out.println("Title matched!!");
       } else {
                 System.out.println("Title did not match");
                 takeScreenshotMethod();
            }
        }

        private void takeScreenshotMethod() {
             // code to take screen shot of browser
            File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

             // file name where image to be saved
             String screenshotpath = "C:\\google.png";
               try {
                       // save image to drive
                        FileUtils.copyFile(scrFile, new File(screenshotpath));
               } catch (IOException e) {
                       e.printStackTrace();
              }
          }

}

Expected Behavior
  1. Load the URL
  2. Take Screenshot
  3. Save Screenshot Image to local drive
Happy Learning