Capturing full Screenshot in Selenium WebDriver using java.awt.Robot Class!!!
AWT stands for (Abstract Window Toolkit) is mainly useful to develop windows based applications using Core Java.
Basic Example using AWT as shown below:
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
* Basic Example in AWT, Opens a frame with a button having text 'click me;.
* Clicking on the button closes the frame.
*
*/
class BasicAwtEx
{
public static void main(String[] args) {
BasicAwtEx b = new BasicAwtEx();
b.openDialog();
}
public void openDialog()
{
final Frame f = new Frame();
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.dispose();
}
});
f.add(b);//adding button into frame
f.setSize(300,300);//frame size 300 width and 300 height
f.setLayout(null);//no layout manager
f.setVisible(true);//now frame will be visible, by default not visible
}
}
Output:
Basic Example using AWT as shown below:
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
* Basic Example in AWT, Opens a frame with a button having text 'click me;.
* Clicking on the button closes the frame.
*
*/
class BasicAwtEx
{
public static void main(String[] args) {
BasicAwtEx b = new BasicAwtEx();
b.openDialog();
}
public void openDialog()
{
final Frame f = new Frame();
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
f.dispose();
}
});
f.add(b);//adding button into frame
f.setSize(300,300);//frame size 300 width and 300 height
f.setLayout(null);//no layout manager
f.setVisible(true);//now frame will be visible, by default not visible
}
}
Output:
Basic Example for taking screenshot using java.awt.Robot Class as shown below:
import java.awt.AWTException;
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.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/*
*
* Taking a screenshot using Robot class in java rather than
* TakesScreenshot Interface in Selenium Webdriver.
*
*/
public class CaptureFullScreen {
public static void main(String[] args) throws AWTException {
try
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
//Implementing the Robot Class
Robot robot = new Robot();
String format = "jpg";
String imgFileName= new SimpleDateFormat("mm-dd-yyyy_HH-ss").
format( new GregorianCalendar().getTime());
System.out.println(new GregorianCalendar().getTime());
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, format, new File("d:\\"+imgFileName+".jpg"));
System.out.println("Screenshot Saved in today's date format in d:\\");
}
catch ( IOException ex)
{
System.err.println(ex);
}
}
}
Output:
0 comments:
Post a Comment