2

Hi everyone again Im triying to make an automated testing of a web application so i need to know how to take multiple screen shots during the test

thats my code

 @Test
    public void TestJavaS1() {


        WebDriver driver;
        System.setProperty("webdriver.gecko.driver", "C:\\selenium\\geckodriver.exe");


        driver = new FirefoxDriver();


        Screenshot.captureScreenShot(driver);


        driver.get("http://hotmail.com");
        Take.captureScreenShot(driver);
acikojevic
  • 915
  • 1
  • 7
  • 16
Edward Lopez
  • 41
  • 1
  • 1
  • 4

2 Answers2

1

There are multiple ways to do this.

Create one separate class file as ScreenCapture and inside this class file create two methods.

One method is for when your particular testcase is run successfully and another method is for when your testcase is failed during execution of your test scripts.

I have provided you one class file.

package com.dummy;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

public class ScreenCapture {

    public static void passScreenCapture() throws IOException
    {
        Date d = new Date();
        System.out.println(d.toString());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");         // Your each screenshot will be taken as this format "Year-Month-Date-Hours-Minutes-Seconds"
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile,  new File("D:\\RND\\"+sdf.format(d)+".png"));      //your screenshot path and convert date string to SimpleDateFormat because windows can't capture screenshot with(:)
    }

    public static void failScreenCapture() throws IOException
    {
        Date d = new Date();
        System.out.println(d.toString());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");   
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy somewhere
        FileUtils.copyFile(scrFile, new File("D:\\RND\\"+sdf.format(d)+".png"));

    }

}

Now your screenCapture class file is ready along with two different methods. you need to call this methods, where you want to call.

You can directly call this methods to any class as follow.

ScreenCapture.passScreenCapture();     //classname.methodname
ScreenCapture.failScreenCapture();

OR

Another way as follow.

Create One class file as below.

package com.dummy;

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.testng.annotations.Test;

public class ScreenShots {

    public void captureScreen() throws IOException
    {
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy somewhere
        FileUtils.copyFile(scrFile, new File("D:\\RND\\Modulename.png"));
    }

}

you call this methods to any class, and you can call this method as like this

 public void captureScreen() throws Exception
 {
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy somewhere
       FileUtils.copyFile(scrFile, new File("D:\\RND\\Modulepage.png"));
       System.out.println("Module Page Screen is taken successfully.");
 }
Jainish Kapadia
  • 2,603
  • 5
  • 18
  • 29
1

Multiple screenshot for every page, Just create one common method which takes screenshot and call that method in your code wherever you want to take screenshot. as @Jainish mentioned.

The another alternative is if you want to take screenshot after some time interval e.g should capture screenshot in every 5 seconds. You can use some scheduler task in java -

Place this in your code

Runnable takeScreenshot = new Runnable()
{
        public void run() {
            try {
                captureScreenShot();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 };
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    executor.scheduleAtFixedRate(takeScreenshot, 0, 3, TimeUnit.SECONDS);

Method

public void captureScreenShot() throws IOException
{
    Date d =new Date();
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    // Now you can do whatever you need to do with it, for example copy somewhere
    FileUtils.copyFile(scrFile, new File("D:\\My_Folder\\"+d.toString().replace(":", "_")+".png"));
}
NarendraR
  • 7,577
  • 10
  • 44
  • 82