Thursday, 9 February 2017

SELENIUM: HOW TO TAKE SCREENSHOT USING ROBOT CLASS


HOW TO TAKE SCREENSHOT USING ROBOT CLASS:



Sometimes we may face problem while taking Screenshot using Selenium API .
That time we will use Robot class from java.awt.* package


Example:

package com.rameshsoft.rameshselenium;

import java.awt.AWTException;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class ScrShotUsingRobot {

          @Test
          public void test() throws InterruptedException, HeadlessException, AWTException, IOException

          {
          System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(55, TimeUnit.SECONDS);
driver.manage().deleteAllCookies();
driver.get("https://www.gmail.com");
Thread.sleep(2000);
BufferedImage image=new Robot().createScreenCapture(new Rectangle (Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "jpeg", new File("C:\\Users\\ramesh\\Desktop\\Screenshot"));
}
} 

No comments:

Post a Comment