Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » How to write a function that will give HTML report in OATS

I have written  this function in VBS for HP QTP -http://www.askqtp.com/2008/12/html-report-generation-through-qtp.html
Now it is time to write a similar function for Popular OATS(Oracle Test Automation Suite-Open script)

OATS is based on Java.All the function i am going to write is on java.
The OATS sections are-


  1. All imports
  2. Class deceleration that will  call initialize function
  3. call run method
  4. call finish method
  5. All custom functions

All imports:

In this section we are going to import all the required files to be imported for our test script

Class Deceleration:

This section is the starting point of OATS. Hence we will provide all the static content or any value that will be used throughout the script. We will also call the initialize method here.

Run method:

This section's code will be executed each iteration for this virtual user. This is the main section for any OATS script. All external function will be triggered from here. (I mean this is the best practice)

Finish method:

This method is kind of finally method in core java. So after every run if you want to update anything or want to close any open object , you can specify here.

All custom functions:

This is an optional part of OATS.In this section people can write their own functions which can be called from any of the part.

Well , Now lets go to the report program.
The structure will be something for all pass results we will get a green row in the output report file and for a failed result ,we will get a red row. As this is a failed case we might be interested to cross check the application what went wrong.So I have given a  flexibility to capture screen shot and attach a link to the report file of that image.
The second important aspect of this report program is you don't need to provide the path. This code will take care of the current folder of the jar and can save the report there itself.
Thirdly , this will not overwrite the existing report rather it will try to append below. So need further configuration when using this concurrently. Else cope this script in your test and run.

The code for HTML report file


//This is for point number-1 all import section. Do not touch this section
import oracle.oats.scripting.modules.basic.api.*;
import oracle.oats.scripting.modules.utilities.api.*;
import oracle.oats.scripting.modules.utilities.api.sql.*;
import oracle.oats.scripting.modules.utilities.api.xml.*;
import oracle.oats.scripting.modules.utilities.api.file.*;
import java.util.Calendar;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
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.text.SimpleDateFormat;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.security.PrivateKey;
//This is for point number-2 Class deceleration.
public class script extends IteratingVUserScript {
private static final String Test_Name = null;
@ScriptService oracle.oats.scripting.modules.utilities.api.UtilitiesService utilities;
public void initialize() throws Exception {
}

/**
* Add code to be executed each iteration for this virtual user. This is for point number-3 run method.
*/
public void run() throws Exception {
String Test_Name = "Testing Any App";
        String Step = "Put_id";
        String Test_Data = "Animesh";
        String Flag = "Pass";
        String comments="animesh is testing";
        Generate_HTML(Step, Test_Data, Flag,comments);
}
//This is for point number-4 finish method
public void finish() throws Exception {
}
//This is for point number-5 All custom methods
public void  Generate_HTML(String step, String test_Data, String flag, String comments)throws FileNotFoundException,
    IOException {

        String path =getJarFolder()+"\\"+"Result"+"\\";
        System.out.print(path);
        if (verify_create_file(path)) {
            System.out.println(verify_create_file(path));
            Body_table(path, Test_Name);
        }
        Update_table(path, step, test_Data, flag, comments);
}
    private  void Update_table(String path, String Step, String Test_Data,
            String Flag,String comments) throws FileNotFoundException,
                                IOException {

System.out.println("inside update");
File file1 = new File(path + "Ani.html");
String body = "";
String body1 = "";
String finalbody = "";
try {
FileInputStream fstream = new FileInputStream(file1);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str;
while ((str = br.readLine()) != null) {
body = body + str;
}
      body1 =
           body.replaceAll("<html><Head><Title>Test Execution for the Flow PJT08</Title></head><body>","");
      body1 = body1.replaceAll("</table></body></html>", "");
if (Flag == "Pass") {
body1 =body1 + "<tr bgcolor=#CCFF99><td>" + Step + " </td><td>" +
Test_Data + "</td><td>" + Flag + "</td><td>"+comments+"</td><td></td></tr>";
}
else {
body1 =body1 + "<tr bgcolor=#FFCCCC><td>" + Step + " </td><td>" +
     Test_Data + "</td><td>" + Flag + "</td><td>"+comments+"</td><td><a href=" +
     Capture_Screenshot(path) + ">" +
     Capture_Screenshot(path) + "</a></td></tr>";
}
finalbody =
 "<html><Head><Title>Test Execution for the Flow PJT08</Title></head><body>" +
 body1 + "</table></body></html>";
PrintWriter pw;
pw = null;
try {
pw = new PrintWriter(new FileWriter(file1, false));
pw.println(finalbody);
pw.flush();
} catch (Exception e) {
System.err.println(e);
}
in.close();
} catch (Exception e) {
System.err.println(e);
}
}


private boolean verify_create_file(String path) {
//Check the result file is there or not
File file1 = new File(path + "Ani.html");
boolean flag = false;
// this code id to create the folder structure
if (!file1.exists()) {

try {
boolean success = (new File(path)).mkdirs();
file1.createNewFile();
flag = true;

} catch (Exception e) {
System.out.println(e);
flag = false;
}
return flag;
} else {
System.out.println("return false");
return false;
}

}


private void Body_table(String path, String Test_Name) {
String file1 = path + "Ani.html";
PrintWriter pw;
pw = null;
try {
pw = new PrintWriter(new FileWriter(file1, true));
pw.println("<html><Head><Title>Test Execution for the Flow " +
    Test_Name + "</Title></head>");
pw.println("<body><table border=\"1\" cellpadding=\"5\" cellspacing=\"5\" width=\"100%\" style=\"border-collapse:collapse\">" +
"<tr bgcolor=#99CCFF><td>Step performed</td><td>Test Data</td><td>Result-Pass/Fail</td><td>Comments</td><td>Screenshot</td></tr></table></body></html>");
pw.flush();
} catch (Exception e) {
System.out.println(e);
}

}
private String getJarFolder() {
String applicationDir = new java.io.File("").getAbsolutePath();
System.out.print(applicationDir);
if (applicationDir.endsWith(".exe"))
{
   applicationDir = new File(applicationDir).getParent();
}
else
{
   applicationDir += getClass().getName().replace('.', '/');
   applicationDir = new File(applicationDir).getParent();
}
return applicationDir;
  }
public String Capture_Screenshot(String path) {
PrintWriter pw;
String dateNow;
String pathcr = "";
pw = null;
try {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter =
new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
dateNow = formatter.format(currentDate.getTime());
String[] datefinal = dateNow.split(" ");
String changeformat = datefinal[1].replace(":", "_");
pathcr = path + changeformat + ".png";
ImageIO.write(image, "png", new File(pathcr));
} catch (Exception e) {
//Catch exception if any
System.err.println("Error: " + e.getMessage());

}
return pathcr;
}

}  


That's it!!! Enjoy!!!

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply