Forum Discussion
brhacking
15 years agoNew Contributor
Here is a set of basic web page operations that work at the native level. I am just posting the clickLink and changeTextBox function that I have written as well as the supporting functions. These are written in jScript and work in Firefox and IE. You may want to enhance/remove/change how I get my browser processes.
Using the supporting functions you can write other page operations such as changing a textbox or whatever. I've found that I had to write functions at the native level because of the number of controls on the web pages I have been testing just takes TC too long and causes IE to consume too much memory (silly IE) when using TC's builtin functions.
function clickLink(linkText)
{
var linkObject;
var window = getBrowserWindow();
var page = window.Page("*");
page.Wait()
page.Refresh()
var linkObj = getElement("A","innerText",linkText);
if(linkObj != null){
clickObj(page, linkObj);
Log.Event("Found and Clicked a link with the text of: '" + linkText + "'");
}
else {
Log.Error("I could not find a link with the text of: '" + linkText + "'");
}
}
function changeTextbox(textboxName, text)
{
var inputObject;
var window = getBrowserWindow();
var page = window.Page("*");
//get the textbox by name.
var inputObject = getElement("INPUT","name",textboxName)
/*Only try to modify the texdtbox if the textbox was returned
if it was not returned, then log an error. */
if(inputObject != null){
//set the Textbox value to text
inputObject.value=text
try {
inputObject.onChange()
}
catch (exception){
}
Log.Event("Changed Textbox '" + textboxName + "' to '" + text + "'")
}
else {
Log.Error("Could not find an INPUT named '"+textboxName+ "'")
}
}
function getBrowserType()
{
var process, window;
process = Sys.WaitProcess("iexplore", 500);
if (process.Exists){
window = process.Window("IEFrame", "*");
if (window.WaitWindow("Shell DocObject View").Exists)
return "IE6";
else
if (window.WaitWindow("TabWindowClass").Exists)
return "IE7";
else
return "";
}
process = Sys.WaitProcess("firefox", 500);
if (process.Exists){
return "FireFox";
}
return "";
}
function getBrowserWindow()
{
var browserName;
browserName = getBrowserType();
switch(browserName)
{
case "IE6":
return Sys.Process("IEXPLORE").Window("IEFrame", "*", 1).Window("Shell DocObject View", "", 1).Window("Internet Explorer_Server", "", 1);
case "IE7":
return Sys.Process("iexplore").Window("IEFrame", "*", 1).Window("TabWindowClass","*", 1).Window("Shell DocObject View", "", 1).Window("Internet Explorer_Server","", 1);
case "FireFox":
return Sys.Process("firefox").Window("MozillaUIWindowClass", "*", 1).Window("MozillaWindowClass","", 1).Window("MozillaWindowClass", "", 3).Window("MozillaWindowClass", "",1).Window("MozillaContentWindowClass", "", 1).Window("MozillaWindowClass","", 1);
case "":
return Utils.CreateStubObject();
}
}
function getElement(itemTag, itemAttribute, itemText, itemNumber)
{
var i = 0;
var i2 = 0;
var foundIt = false;
var window = getBrowserWindow();
var page = window.Page("*");
var regx = new RegExp(itemText);
var noFound = 0;
var frames = window.document.frames.length
if (itemNumber == undefined){
itemNumber = 1;
}
//first try the main document body
var objA = window.document.body.getElementsByTagName(itemTag);
//loop through each item returned stopping at the specified itemNumber.
while(i<objA.length && (foundIt==false)){
if (regx.exec(trim(objA.getAttribute(itemAttribute).replace(/(<([^>]+)>)/ig," ")))){
noFound++;
if (noFound == itemNumber){
foundIt = true;
break;
}
}
i++;
}
if (!foundIt){
i=0; //reset the item iterator
while(i2<frames && (foundIt == false)){
//loop through each frame until you find the item or are out of frames.
var objA = window.document.frames[i2].document.getElementsByTagName(itemTag);
//loop through each item returned
while(i<objA.length && (foundIt==false)){
if (regx.exec(trim(objA.getAttribute(itemAttribute)))){
foundIt = true;
break;
}
i++;
}
i2++;
}
}
if(foundIt){
objA.scrollIntoView(true);
return objA;
}
else {
return null;
}
}
function trim(value)
// Removes leading and ending whitespaces
{
if(value==""){
return ""
}
else {
return LTrim(RTrim(value));
}
}
function LTrim(value)
//Removes leading whitespaces
{
var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");
}
function RTrim(value)
// Removes ending whitespaces
{
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}
function clickObj(page, itemObj)
{
//Clicks on the center of the object that was passed in.
itemObj.scrollIntoView(false)
var objRect = itemObj.getBoundingClientRect()
var x = page.ScreenLeft + objRect.left + (objRect.right - objRect.left)/2
var y = page.ScreenTop + objRect.top + (objRect.bottom - objRect.top)/2
Sys.Desktop.MouseDown(VK_LBUTTON,x,y)
Sys.Desktop.MouseUp(VK_LBUTTON,x,y)
}
Related Content
- 3 years ago
- 5 years ago
Recent Discussions
- 7 hours ago