18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

Problem setting a javascript variable using SAHI functions

lafranceblafranceb Members
edited November -1 in Sahi - Open Source
Hello,

I looked in most posts and at many places on the website trying to find an answer to my problem before posting but I didn't find anything about it. So here I go :

I'm using SAHI as an automated bot on a gaming site. It's simple work : clicking and parsing informations of the browser's pages and SAHI seems the perfect tool for my deeds. I work on Windows XP and I use Firefox to run SAHI. I didn't change anything in any config file of SAHI, so it's still in its original state since I downloaded and installed it.

I made a few versions of my script with SAHI and everything was fine untill the day I decided to add a little bit of AI to my bot. All I wanted is to keep values of HTML DOM Elements or Javascript Objects gotten by SAHI functions (eg.: _byId("COUNTER"), _getText(_cell(_table(7), 1, 1)), etc.). These functions seem to work properly when I use them in an _alert(). But what I really want to do si to put the result of these functions into Javascript variables. And when I do so, it doesn't work at all...

I tried alternative ways, such as using _set() and _condition() hence eliminating the direct set of my Javascript variables from the result of a SAHI function, but because I run my script in almost an infinite loop it doesn't take time before the browser shows sign of slowness and ultimately (after a few minutes) showing a Firefox pop-up stating that a slow script is running and asking me if I want to stop it or let it go. Of course, this pop-up ruins the rest of my running script. Important note, even if I was already looping in my first revisions of my bot, the pop-up never showed up in hours of relentless running. So my guess is that the functions _set() and _condition() are kind of heavy to process.

Knowing that SAHI shows signs of hard and long internal work using these methods too often, it leads us to my current problem of setting Javascript variables. What I noticed when looking to the parsed script available to view in the SAHI Controller, is that when a code line doesn't start with a SAHI function or some predifined RegExp that I ignore but which I suppose it exists (as "if (_condition(*)"), then the parsed script doesn't seem like it will execute those afore-mentioned lines of code.

Enough blank talking, here's a sample of my script where the problem happens :
var $statParry = new String("100");
var $statFighting = new String("100");

_click(_link("Mission"));
_click(_imageSubmitButton(0));

// this works perfectly!!
_alert(_getText(_cell(_table(7), "Parry:", 1)).toString() + " " + _getText(_cell(_table(7), "Fighting ability:", 1)).toString());

$statFighting = _getText(_cell(_table(7), "Fighting ability:", 1)).toString();
// SAHI fails on the above line never executing the following lines...
$statFighting = $statFighting.substring($statFighting.indexOf("(") + 1, $statFighting.indexOf(")"));
$statParry = _getText(_cell(_table(7), "Parry:", 1)).toString();
$statParry = $statParry.substring($statParry.indexOf("(") + 1, $statParry.indexOf(")"));

// duh! I never get to this point...
_alert($statParry + " " + $statFighting);

if ($statParry <= 50 && $statFighting <= 40 || $statParry <= 45 && $statFighting <= 45) {
  _log("attack!!")
  // some other actions...
}
And now, what the SAHI parsed script of this looks like :
_sahi.scriptScope = function (){
    _sahi.scriptScope.execute = function(s){eval(s);};

//Your code starts

var $statParry = new String("100");
var $statFighting = new String("100");
_sahi.schedule("_sahi._click(_sahi._link(\"Mission\"));", "C:\\solapcore\\sahi\\bin\\..\\scripts\esources\\ExampleForumSahi.sah&n=4");
_sahi.schedule("_sahi._click(_sahi._imageSubmitButton(0));", "C:\\solapcore\\sahi\\bin\\..\\scripts\esources\\ExampleForumSahi.sah&n=5");
// this works perfectly!!
_sahi.schedule("_sahi._alert(_sahi._getText(_sahi._cell(_sahi._table(7), \"Parry:\", 1)).toString() + \" \" + _sahi._getText(_sahi._cell(_sahi._table(7), \"Fighting ability:\", 1)).toString());", "C:\\solapcore\\sahi\\bin\\..\\scripts\esources\\ExampleForumSahi.sah&n=8");
$statFighting = _sahi._getText(_sahi._cell(_sahi._table(7), "Fighting ability:", 1)).toString();
// SAHI fails on the above line...
$statFighting = $statFighting.substring($statFighting.indexOf("(") + 1, $statFighting.indexOf(")"));
$statParry = _sahi._getText(_sahi._cell(_sahi._table(7), "Parry:", 1)).toString();
$statParry = $statParry.substring($statParry.indexOf("(") + 1, $statParry.indexOf(")"));
// duh! I never get to this point...
_sahi.schedule("_sahi._alert("+s_v($statParry)+" + \" \" + "+s_v($statFighting)+");", "C:\\solapcore\\sahi\\bin\\..\\scripts\esources\\ExampleForumSahi.sah&n=17");
if ($statParry <= 50 && $statFighting <= 40 || $statParry <= 45 && $statFighting <= 45) {
_sahi.schedule("_sahi._log(\"attack!!\")", "C:\\solapcore\\sahi\\bin\\..\\scripts\esources\\ExampleForumSahi.sah&n=20");
// some other actions...
}


//Your code ends

}
try{_sahi.scriptScope();}catch(e){_sahi.loadError = e; throw e;}
In the logs, I've got this error message :
Error loading script. Firefox may point to the exact line. TypeError: el has no properties(null)
@http://www.somesite.us/_s_/spr/concat.js:307(null)
@http://www.somesite.us/_s_/spr/concat.js:913()
@http://www.somesite.us/_s_/dyn/Player_script/script.js:12
@http://www.somesite.us/_s_/dyn/Player_script/script.js:28
What was my point about what I think might be the cause of the problem is that on the lines where I'm setting my variables there's no _sahi.schedule(...). I honestly don't know what's the internal difference for SAHI between a _sahi.schedule() call and a simple _sahi._whateverFunction() when it executes a script...

With all that stated, I think I explained everything needed to understand what I'm doing, what are my needs and what problems I encountered and even more informations about all of this. Perhaps I'm a perfect dumb and I'm not pointing at the right source to troubleshoot this issue... In any case, I would like to know what to do to be able to set my variables or which alternate way I should use (one I did not already try and that slowed down my browser, or if it's absolutely one of those ways that I must take, how could I do it without the slowdown issue??).

If anyone needs more precise information about something I wrote or that I forgot, let me know.

Thanks for the help and for the great software SAHI is!!

Comments

  • narayannarayan Administrators
    There IS an overhead to using _condition etc. Your observations are quite right. I will have a look at this in detail and get back to you.
Sign In or Register to comment.