I'm evaluation RFT (6.1, 7.0.1) for testing a Web application with customized HTML components. I would like to know how we can access HTML DOM in RFT and use native Java Script methods like getElementById etc.?
Please find some functions which is same like DOM concept:
package Libraries;
import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.SAP.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;
import com.rational.test.util.regex.internal.*;
import java.util.*;
public class libCommon extends RationalTestScript {
public GuiTestObject getElementByName(String propertyName,
String className, String objName)
{
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
boolean foundFlag = false;
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++)
{
System.out.println(to.getProperty(".name").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objName);
System.out.println(to.getProperty(".name").toString().indexOf(
objName.trim()));
int fndIndex = to.getProperty(".name").toString().indexOf(
objName.trim());
if (fndIndex >= 0)
{
System.out.println("Object found: " + objName);
foundFlag = true;
break;
}
// System.out.println (to.getProperties());
}
if (!foundFlag) {
System.out.println("Object not found:= " + objName + ";classname:="
+ className + ";propertyName:= " + propertyName);
return ((GuiTestObject) null);
} else {
return ((GuiTestObject) to);
}
}
******************************************************************
public GuiTestObject getElementByValue(String propertyName,
String className, String objValue) {
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
boolean foundFlag = false;
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
System.out.println(to.getProperty(".value").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objValue);
System.out.println(to.getProperty(".value").toString().indexOf(
objValue.trim()));
int fndIndex = to.getProperty(".value").toString().indexOf(
objValue.trim());
if (fndIndex >= 0) {
System.out.println("Object found: " + objValue);
foundFlag = true;
break;
}
// System.out.println (to.getProperties());
}
if (!foundFlag) {
System.out.println("Object not found:= " + objValue
+ ";classname:=" + className + ";propertyName:= "
+ propertyName);
return ((GuiTestObject) null);
} else {
return ((GuiTestObject) to);
}
}
***********************************************************************
public boolean findStringInHTMLTable(GuiTestObject HTML_TBL, String strSearch)
{
String tblCellValue=null;
boolean returnValue=false;
//begin test
HTML_TBL.waitForExistence();
//ITestDataTable htmlTble = (ITestDataTable) selAFlight.htmlTable_TourInformation();
// Get the data for the table
ITestDataTable tblInfo=(ITestDataTable) HTML_TBL.getTestData("contents");
// Display the available data types for the grid, total rows and
// columns.
System.out.println ("Available Data Types: " +
HTML_TBL.getTestDataTypes());
System.out.println ("Total Rows in table : " +
tblInfo.getRowCount());
System.out.println ("Total Cols in table : " +
tblInfo.getColumnCount());
int findIndex=0;
// Cycle through all rows
for (int row=0; row < tblInfo.getRowCount();++row)
{
// Cycle through all columns
for (int col=0; col < tblInfo.getColumnCount();++col)
{
//if(!(tblInfo.getCell(row, col).equals(null))){
try {
tblCellValue=tblInfo.getCell(row, col).toString();
}
catch(Exception e) {
continue;
}
//findIndex=1;
findIndex = tblCellValue.indexOf(strSearch.trim());
// Print out values of cells at (row,col) coordinates
System.out.println ("Table Cell Value: " +tblCellValue);
System.out.println ("Search String: " + strSearch);
System.out.println("Index of search:" + findIndex);
System.out.println("Row Num:"+ row);
System.out.println("Col Num:"+ col);
if(findIndex != -1) {
returnValue= true;
break; }
else
returnValue= false;
//}
}//for inside col
if(returnValue) break;
}
System.out.println(returnValue);
if(returnValue)
System.out.println("Tour Information Found");
else
System.out.println("Tour Information Not Found");
return returnValue;
}
******************************************************************************
public String[] getComboBoxValues(String propertyName, String className, String objName)
{
String arrString[];
String arrString1;
TestObject obj;
//obj = lc.getElementByName(".class", "Html.SELECT", "passCount");
obj = getElementByName(propertyName, className, objName);
arrString1 = obj.getProperty(".text").toString();
System.out.println("arrString1: " + arrString1);
arrString = arrString1.split(" ");
System.out.println(arrString.length);
int j;
for(j=0; j< arrString.length; j++)
{
System.out.println(arrString[j]);
}
return arrString;
}
******************************************************************************************
public String getSelectedValueOfObject(String propertyName, String className, String objName)
{
/*
Example to user
String s;
libCommon lc = new libCommon();
s = lc.getSelectedValueOfObject(".class", "Html.INPUT.text","userName");
System.out.println("User Name: " + s);
s = lc.getSelectedValueOfObject(".class", "Html.INPUT.password","password");
System.out.println("Password: " + s);
*/
TestObject obj;
obj = getElementByName(propertyName, className, objName);
GuiTestObject Gobj;
String gvalue;
Gobj = (GuiTestObject)obj;
gvalue = Gobj.getProperty(".value").toString();
System.out.println("Value:" + gvalue);
return gvalue;
}
***********************************************************************************************************
public int getObjectCount(String propertyName, String className, String objName)
{
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
int intCount=0;
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
System.out.println(to.getProperty(".name").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objName);
System.out.println(to.getProperty(".name").toString().indexOf(
objName.trim()));
int fndIndex = to.getProperty(".name").toString().indexOf(
objName.trim());
if (fndIndex >= 0) {
System.out.println("Object found: " + objName);
intCount++;
}
// System.out.println (to.getProperties());
}
return intCount;
}
************************************************************************
public GuiTestObject getElementByName(String propertyName,
String className, String objName, boolean Exact) {
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
boolean foundFlag = false;
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
System.out.println(to.getProperty(".name").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objName);
System.out.println(to.getProperty(".name").toString().indexOf(
objName.trim()));
int fndIndex;
if (!Exact) {
fndIndex = to.getProperty(".name").toString().indexOf(objName.trim());
if (fndIndex >= 0) {
System.out.println("Object found: " + objName);
foundFlag = true;
break;
}
}
else
{
foundFlag=false;
foundFlag = to.getProperty(".name").toString().equalsIgnoreCase(objName.trim());
if (foundFlag) break;
}
// System.out.println (to.getProperties());
}
if (!foundFlag) {
System.out.println("Object not found:= " + objName + ";classname:="
+ className + ";propertyName:= " + propertyName);
return ((GuiTestObject) null);
} else {
return ((GuiTestObject) to);
}
}
*******************************************************************************
public GuiTestObject getElements(String propertyName,
String className, String objName) {
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
boolean foundFlag = false;
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
System.out.println(to.getProperty(".name").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objName);
System.out.println(to.getProperty(".name").toString().indexOf(
objName.trim()));
int fndIndex = to.getProperty(".name").toString().indexOf(
objName.trim());
if (fndIndex >= 0) {
System.out.println("Object found: " + objName);
foundFlag = true;
printProperties(to);
//break;
}
// System.out.println (to.getProperties());
}
if (!foundFlag) {
System.out.println("Object not found:= " + objName + ";classname:="
+ className + ";propertyName:= " + propertyName);
return ((GuiTestObject) null);
} else {
return ((GuiTestObject) to);
}
}
*****************************************************************************
public static void printProperties(TestObject to) {
Hashtable htable = to.getProperties();
int i = 1;
Enumeration eKeys = htable.keys();
Enumeration eElems = htable.elements();
while (eKeys.hasMoreElements()) {
System.out.println("Property " + i + ": name="
+ eKeys.nextElement() + "; value="
+ eElems.nextElement());
i++;
}
}
*********************************************************************************
public TestObject getTableObject(String propertyName,
String className, int index) {
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
int clsIndex=0;
String strClsIndex;
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
strClsIndex = to.getProperty(".classIndex").toString();
clsIndex = Integer.parseInt(strClsIndex);
if(clsIndex == index)
break;
}
return to;
}
} // class end
I think this will help you.
Thanks,
Kanth
Please find some functions which is same like DOM concept:
package Libraries;
import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.SAP.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;
import com.rational.test.util.regex.internal.*;
import java.util.*;
public class libCommon extends RationalTestScript {
public GuiTestObject getElementByName(String propertyName,
String className, String objName)
{
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
boolean foundFlag = false;
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++)
{
System.out.println(to.getProperty(".name").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objName);
System.out.println(to.getProperty(".name").toString().indexOf(
objName.trim()));
int fndIndex = to.getProperty(".name").toString().indexOf(
objName.trim());
if (fndIndex >= 0)
{
System.out.println("Object found: " + objName);
foundFlag = true;
break;
}
// System.out.println (to.getProperties());
}
if (!foundFlag) {
System.out.println("Object not found:= " + objName + ";classname:="
+ className + ";propertyName:= " + propertyName);
return ((GuiTestObject) null);
} else {
return ((GuiTestObject) to);
}
}
******************************************************************
public GuiTestObject getElementByValue(String propertyName,
String className, String objValue) {
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
boolean foundFlag = false;
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
System.out.println(to.getProperty(".value").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objValue);
System.out.println(to.getProperty(".value").toString().indexOf(
objValue.trim()));
int fndIndex = to.getProperty(".value").toString().indexOf(
objValue.trim());
if (fndIndex >= 0) {
System.out.println("Object found: " + objValue);
foundFlag = true;
break;
}
// System.out.println (to.getProperties());
}
if (!foundFlag) {
System.out.println("Object not found:= " + objValue
+ ";classname:=" + className + ";propertyName:= "
+ propertyName);
return ((GuiTestObject) null);
} else {
return ((GuiTestObject) to);
}
}
***********************************************************************
public boolean findStringInHTMLTable(GuiTestObject HTML_TBL, String strSearch)
{
String tblCellValue=null;
boolean returnValue=false;
//begin test
HTML_TBL.waitForExistence();
//ITestDataTable htmlTble = (ITestDataTable) selAFlight.htmlTable_TourInformation();
// Get the data for the table
ITestDataTable tblInfo=(ITestDataTable) HTML_TBL.getTestData("contents");
// Display the available data types for the grid, total rows and
// columns.
System.out.println ("Available Data Types: " +
HTML_TBL.getTestDataTypes());
System.out.println ("Total Rows in table : " +
tblInfo.getRowCount());
System.out.println ("Total Cols in table : " +
tblInfo.getColumnCount());
int findIndex=0;
// Cycle through all rows
for (int row=0; row < tblInfo.getRowCount();++row)
{
// Cycle through all columns
for (int col=0; col < tblInfo.getColumnCount();++col)
{
//if(!(tblInfo.getCell(row, col).equals(null))){
try {
tblCellValue=tblInfo.getCell(row, col).toString();
}
catch(Exception e) {
continue;
}
//findIndex=1;
findIndex = tblCellValue.indexOf(strSearch.trim());
// Print out values of cells at (row,col) coordinates
System.out.println ("Table Cell Value: " +tblCellValue);
System.out.println ("Search String: " + strSearch);
System.out.println("Index of search:" + findIndex);
System.out.println("Row Num:"+ row);
System.out.println("Col Num:"+ col);
if(findIndex != -1) {
returnValue= true;
break; }
else
returnValue= false;
//}
}//for inside col
if(returnValue) break;
}
System.out.println(returnValue);
if(returnValue)
System.out.println("Tour Information Found");
else
System.out.println("Tour Information Not Found");
return returnValue;
}
******************************************************************************
public String[] getComboBoxValues(String propertyName, String className, String objName)
{
String arrString[];
String arrString1;
TestObject obj;
//obj = lc.getElementByName(".class", "Html.SELECT", "passCount");
obj = getElementByName(propertyName, className, objName);
arrString1 = obj.getProperty(".text").toString();
System.out.println("arrString1: " + arrString1);
arrString = arrString1.split(" ");
System.out.println(arrString.length);
int j;
for(j=0; j< arrString.length; j++)
{
System.out.println(arrString[j]);
}
return arrString;
}
******************************************************************************************
public String getSelectedValueOfObject(String propertyName, String className, String objName)
{
/*
Example to user
String s;
libCommon lc = new libCommon();
s = lc.getSelectedValueOfObject(".class", "Html.INPUT.text","userName");
System.out.println("User Name: " + s);
s = lc.getSelectedValueOfObject(".class", "Html.INPUT.password","password");
System.out.println("Password: " + s);
*/
TestObject obj;
obj = getElementByName(propertyName, className, objName);
GuiTestObject Gobj;
String gvalue;
Gobj = (GuiTestObject)obj;
gvalue = Gobj.getProperty(".value").toString();
System.out.println("Value:" + gvalue);
return gvalue;
}
***********************************************************************************************************
public int getObjectCount(String propertyName, String className, String objName)
{
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
int intCount=0;
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
System.out.println(to.getProperty(".name").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objName);
System.out.println(to.getProperty(".name").toString().indexOf(
objName.trim()));
int fndIndex = to.getProperty(".name").toString().indexOf(
objName.trim());
if (fndIndex >= 0) {
System.out.println("Object found: " + objName);
intCount++;
}
// System.out.println (to.getProperties());
}
return intCount;
}
************************************************************************
public GuiTestObject getElementByName(String propertyName,
String className, String objName, boolean Exact) {
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
boolean foundFlag = false;
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
System.out.println(to.getProperty(".name").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objName);
System.out.println(to.getProperty(".name").toString().indexOf(
objName.trim()));
int fndIndex;
if (!Exact) {
fndIndex = to.getProperty(".name").toString().indexOf(objName.trim());
if (fndIndex >= 0) {
System.out.println("Object found: " + objName);
foundFlag = true;
break;
}
}
else
{
foundFlag=false;
foundFlag = to.getProperty(".name").toString().equalsIgnoreCase(objName.trim());
if (foundFlag) break;
}
// System.out.println (to.getProperties());
}
if (!foundFlag) {
System.out.println("Object not found:= " + objName + ";classname:="
+ className + ";propertyName:= " + propertyName);
return ((GuiTestObject) null);
} else {
return ((GuiTestObject) to);
}
}
*******************************************************************************
public GuiTestObject getElements(String propertyName,
String className, String objName) {
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
boolean foundFlag = false;
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
System.out.println(to.getProperty(".name").toString());
System.out.println("ClassName: " + className);
System.out.println("objName:" + objName);
System.out.println(to.getProperty(".name").toString().indexOf(
objName.trim()));
int fndIndex = to.getProperty(".name").toString().indexOf(
objName.trim());
if (fndIndex >= 0) {
System.out.println("Object found: " + objName);
foundFlag = true;
printProperties(to);
//break;
}
// System.out.println (to.getProperties());
}
if (!foundFlag) {
System.out.println("Object not found:= " + objName + ";classname:="
+ className + ";propertyName:= " + propertyName);
return ((GuiTestObject) null);
} else {
return ((GuiTestObject) to);
}
}
*****************************************************************************
public static void printProperties(TestObject to) {
Hashtable htable = to.getProperties();
int i = 1;
Enumeration eKeys = htable.keys();
Enumeration eElems = htable.elements();
while (eKeys.hasMoreElements()) {
System.out.println("Property " + i + ": name="
+ eKeys.nextElement() + "; value="
+ eElems.nextElement());
i++;
}
}
*********************************************************************************
public TestObject getTableObject(String propertyName,
String className, int index) {
RootTestObject root = getRootTestObject();
System.out.println("Got the Root Object and Exists =");
System.out.print(root.exists());
/*
* example propertyName := .class className := Html.INPUT.radio objName =
* <objectname in the aut>
*/
int clsIndex=0;
String strClsIndex;
TestObject[] to = root.find(atDescendant(propertyName, className));
int i;
for (i = 0; i < to.length; i++) {
strClsIndex = to.getProperty(".classIndex").toString();
clsIndex = Integer.parseInt(strClsIndex);
if(clsIndex == index)
break;
}
return to;
}
} // class end
I think this will help you.
Thanks,
Kanth