18 January 2026:
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
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.
Need help with Dropdown list: How to pick values Randoml from dropdown
Hi Narayan,
I have to select dropdown values (Number or String) Randomly. i have written one function but it is not working it is showing null. can you please let me know if any idea.
<browser>
function getRandomLeaveAppId(control)
{
var selectOptions = control.options;
var randomnumber = Math.floor(Math.random() * selectOptions.length);
if(selectOptions.length > randomnumber)
return selectOptions[randomnumber];
else
return -1;
}
</browser>
//_alert(getRandomLeaveAppId(_select("ctl00\$phWorkArea\$ddlStatus")));
function UserLeaveApplication()
{
_alert("test start");
_alert(getRandomLeaveAppId(_select("ctl00\$phWorkArea\$ddlStatus")));
_alert("test end");
}
UserLeaveApplication();
I have to select dropdown values (Number or String) Randomly. i have written one function but it is not working it is showing null. can you please let me know if any idea.
<browser>
function getRandomLeaveAppId(control)
{
var selectOptions = control.options;
var randomnumber = Math.floor(Math.random() * selectOptions.length);
if(selectOptions.length > randomnumber)
return selectOptions[randomnumber];
else
return -1;
}
</browser>
//_alert(getRandomLeaveAppId(_select("ctl00\$phWorkArea\$ddlStatus")));
function UserLeaveApplication()
{
_alert("test start");
_alert(getRandomLeaveAppId(_select("ctl00\$phWorkArea\$ddlStatus")));
_alert("test end");
}
UserLeaveApplication();
Tagged:
Comments
It is a good idea to check the value of the random number too, if the above does not work
It is not working...it is not showing alert with selected random value.......
modified return selectOptions[randomnumber] to return selectOptions[randomnumber].text
(1) the number of items in the control
(2) the generated random number
(3) the return value of getRandomLeaveAppId
<browser>
function getRandomLeaveAppId(control)
{
var selectOptions = control.options;
_debug(selectOptions.length); //print length
var randomnumber = Math.floor(Math.random() * selectOptions.length);
_debug(randomnumber); //print value of random number
if(selectOptions.length > randomnumber)
return selectOptions[randomnumber].text;
else
return -1;
}
</browser>
//_alert(getRandomLeaveAppId(_select("ctl00\$phWorkArea\$ddlStatus")));
function UserLeaveApplication()
{
// _alert("test start");
_debug(getRandomLeaveAppId(_select("ctl00\$phWorkArea\$ddlStatus")));
// _alert("test end");
}
UserLeaveApplication();
i am getting below error in the console and it is not printing any dropdown values....
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Metho
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Sourc
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown S
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sf.sahi.command.CommandExecuter.execute(CommandExecut
at net.sf.sahi.LocalRequestProcessor.getLocalResponse(LocalR
sor.java:42)
at net.sf.sahi.ProxyProcessor.processLocally(ProxyProcessor.
at net.sf.sahi.ProxyProcessor.run(ProxyProcessor.java:79)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at net.sf.sahi.playback.ScriptFactory.getScript(ScriptFactor
at net.sf.sahi.playback.ScriptFactory.getScript(ScriptFactor
at net.sf.sahi.command.Player.setScript(Player.java:94)
at net.sf.sahi.command.Player.resetScript(Player.java:91)
... 9 more
Note:
_select("ctl00\$phWorkArea\$ddlStatus") --> in this dropdown list we have "Approved","Cancel" and "Rejected" values.