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.

Need help with Dropdown list: How to pick values Randoml from dropdown

srinisrini Members
edited November -1 in Sahi - Open Source
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();
Tagged:

Comments

  • dpdp Members
    Try changing the return statement from return selectOptions[randomnumber] to return selectOptions[randomnumber].text

    It is a good idea to check the value of the random number too, if the above does not work
  • srinisrini Members
    edited June 2009
    Hi DP
    It is not working...it is not showing alert with selected random value.......

    modified return selectOptions[randomnumber] to return selectOptions[randomnumber].text
  • dpdp Members
    Execute the below code and let me know what is printed on the console. It prints
    (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();
  • srinisrini Members
    Hi DP,

    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.
  • dpdp Members
    Ooops....I have no clue. But the above code works for me. Only change that I made is the Id of the control!! Are you sure you do not have any Debug output on the console?
Sign In or Register to comment.