My Blog List

Friday, August 26, 2011

QTP - Logging with folder structure

'QTP - Logging with folder structure for cases------

Function PushLog(StepName, StepDetail)
Dim dicMetaDescription, intContext
Set dicMetaDescription = CreateObject("Scripting.Dictionary")
dicMetaDescription("Status") = micGeneral
dicMetaDescription("PlainTextNodeName") = StepName
dicMetaDescription("StepHtmlInfo") = "
" + StepDetail +"
"

'Some backdoor settings:
'dicMetaDescription("DllIconIndex") = 206
'dicMetaDescription("DllIconSelIndex") = 206
dicMetaDescription("DllPAth") = "C:\Program Files\HP\QuickTest Professional\bin\ContextManager.dll"

intContext = Reporter.LogEvent("User", dicMetaDescription, Reporter.GetContext)
'Set the new report node as a parent
'From now on, all reports will be added under this node
Reporter.SetContext intContext
End Function

Function PopLog()
'Now return to the parent level:
Reporter.UnSetContext
End Function



'So Calling will be like,
For CaseNo = 1 to CasesCount
Call PushLog(CaseID, CaseName)
For Iteration =1 to DataCount
Call PushLog(CaseID + " - Iteration: "&Iteration, CaseName)
Eval("Case"&CaseNo)
Call PopLog()
Next
Call PopLog()
Next

Wednesday, August 17, 2011

Apache POI - Read Excel - For use of Selenium TestNG DataProvider

Here is the Class designed for reading Excel file...
You can download the JAR files needed to work with Apache POI here.

package Excel;

import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.DocumentException;

public class POI {
int rowIndex = 0, columnIndex = 0;
Workbook wb;
Sheet ws;
Row wr;
String fileName, sheetName;
Hashtable <String, String> [] data = null;

public  void open() throws IOException  {
if (fileName.indexOf("xlsx") < 0) {
wb = new HSSFWorkbook(new FileInputStream(new File(fileName)));
ws = wb.getSheet(sheetName);
} else {
wb = new XSSFWorkbook(fileName);
ws = (XSSFSheet) wb.getSheet(sheetName);
}
}

@SuppressWarnings("unchecked")
public Object[][] getData() throws IOException  {
data = new Hashtable[ws.getPhysicalNumberOfRows()];                            
wr = ws.getRow(0);

for(rowIndex = 1; rowIndex < ws.getPhysicalNumberOfRows(); rowIndex++) {
data[rowIndex - 1] = new Hashtable <String, String>();

for (columnIndex = 0; columnIndex < ws.getRow(rowIndex).getPhysicalNumberOfCells(); columnIndex++) {
data[rowIndex - 1].put(wr.getCell(columnIndex).toString(), ws.getRow(rowIndex).getCell(columnIndex).toString());
}
}

Object[][] obj = new Object[data.length - 1][1];
for(int i = 0; i < data.length - 1; i++) {
obj[i][0] = data[i];
}
return obj;
}


       public  void close() throws IOException  {
            wb = null
            ws = null
            wr = null
            data = null
}


}

TestNG - DataDriven Custom Framework

Here is the sample code for DataDriven TestNG test...

package Excel;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Hashtable;

import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.openqa.selenium.*;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import Excel.POI; //Class POI contains methods to read Excel file

public class Test1 {
SeleniumServer server;
Selenium sel;
POI poi = new POI();
@BeforeClass
public void beforeClass() throws Exception {
server = new SeleniumServer();
server.start();
sel = new DefaultSelenium("localhost", 4444, "*iehta", "http://www.google.com/");
sel.start();
}
@DataProvider
public Object[][] dp() throws IOException {
poi.fileName = "C:\\Users\\shanmugavel.chinnago\\Desktop\\test.xlsx";
poi.sheetName = "Sheet1";
poi.open();
return poi.getData();
}
@Test(dataProvider = "dp")
public void googleTest(Hashtable value) {
sel.open("/");
sel.type("//input[@name='q']", value.get("C1")); //C1 - ColumnName
}

@AfterClass
public void afterClass() {
sel.close();
sel.stop();
server.stop();
}

}