Aug 27 2009 03:31:26 AM Posted By : rajasankar
Comments (0)

ScreenCapture in UI Automation

In UI automation, how to know if the script played correctly?

One way is check the log messages and the check the output. That will be an cumbersome process to do. The better way is to see screenshots of the page. QEngine has a function to do this.

Code:
saveScreenShot(outFileName,waittime)


You can call this way using the python function, if you need to generate filename automatically,

Code:

import time

def screenshot():
filename=time.strftime("%Y_%m_%d_%H_%M_%S")
saveScreenShot(filename,"1")


There is an one limitation. This saves the file as a JPEG format and save the files in dataset folder in the projects.

You can write an Java class to save the images in the PNG format which will occupy less space than JPEG file.

Here is the code
Code:

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.util.Calendar;

public class SaveScreen {

public static void main (String args[])
{
}


public void saveScreen() throws Exception
{

Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH);
int day = now.get(Calendar.DAY_OF_MONTH);
int hour = now.get(Calendar.HOUR_OF_DAY);
int min = now.get(Calendar.MINUTE);
int sec = now.get(Calendar.SECOND);
String fileNameone = "Screenshot_" year "_" month "_" day "_" hour "_" min "_" sec ".png";

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(fileNameone));
}

}

You can also change the
Code:

String fileNameone = "Screenshot_" year "_" month "_" day "_" hour "_" min "_" sec ".png";

into
Code:

String fileNameone = foldername "Screenshot_" year "_" month "_" day "_" hour "_" min "_" sec ".png";

if you need to save the files in the required folder.

Refer to the previous blog entry for how to import your own code in the QEngine.

Rajasankar
rajasankar at zohocorp dot com

No one has commented yet! Be the first one to comment!

Post Comment