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.

Selected Boxes

lepierrotlepierrot Members
edited November -1 in Sahi - Open Source
Hi (again),

I've got a Select box in my webpage, and I would like to store all the value in an array...

For example there is a selected box :
<select name="myselectedbox">
<option value="1">Cat</option>
<option value="2">Dog</option>
<option value="3">Bird</option>
<option value="4">Rabbit</option>
<option selected value="5">Cow</option>
<option value="6">Turtle</option>
</select>
I want to store all the values of the options in an array. In fact I would like to have :

array[0]= "Cat";
array[1]= "Dog";
...
array[5]= "Turtle";

I've checked in Sahi's APIs and there is a function _getSelected(...) but it works when you know which value ou want to get (and you have to do that manually).

Here, I need a solution that is able to pick up all the option of the selectbox and to store them in the array...I need this because the number of option can change everyday .....

Any idea ?

:)

Comments

  • Hi,

    Right now I can only give some logic how you can do this. The script below needs to be debugged.
    for (var $i = 0; $i < _select("myselectedbox").options.length ; $i++){
        var $arr = new Array();
        _set($arr[$i], _select("myselectedbox").options[$i].text);
        _alert($arr[$i]);
    }
    
    Regards,
    Pankaj.
  • I have to learn javascript ! it's becoming urgent :)
    I wll test it this morning, thank you for all
  • Yup sure..:)
  • lepierrotlepierrot Members
    edited June 2008
    Hi,

    I've tried your solution and I've searched a little bit around "select" :
    I've found this:
    <html>  
        <form name="myForm">
          <select name="mySelect" onChange='[b]alert(mySelect.options.length)[/b]'>
            <option value=A>AA</option>
            <option value=B>BB</option>
            <option value=C>CC</option>
            <option value=D>DD</option>
          </select>
        </form>
    </html>
    
    So I think that if I type
    _alert(mySelect.options.length);
    
    but replacing mySelect by the name of my select box which is "dev_country",
    It should at least display the length of my select box ... But SAHI says that dev_country has no properties...

    The name of my select box is dev_country" and the name of my form is "simdevice". So I've tried with :

    dev_country.options.length
    document.simdevice.dev_country.length
    form.dev_country.options.length
    ... in vain

    Nothing works !

    Does Sahi knows JavaScript after all ?!

    :)
  • Hi,

    In sahi controller try if this thing works..
    _alert(_select("mySelect").options.length);
    
    Regards,
    Pankaj.
  • Ok I've changed little things and it seems to work now...but...on sahi controller only !
    var $i=0;
    var $arr = new Array();
    for($i=0;$i<_select("dev_country").options.length; $i++){
    $arr[$i]=_select("dev_country").options[$i].text;
    }
    for($i=0;$i<_select("dev_country").options.length; $i++){
    _alert($arr[$i]);
    }
    
    It works perfectly in sahi controller, by running the button "test", but when I playback the script, sahi fails !!!

    _select("dev_country") has no propertie

    or

    el has no propertie !

    My mind is getting twisted...
  • Ya this happens alot many times...:(
    use _set() to set the array elements. If you do it directly it will throw the same error of element has not\ properties. I suppose it has something to do with page dependant variables.

    Regrds,
    Pankaj.
  • lepierrotlepierrot Members
    edited June 2008
    I'm afraid _set does not change anything ...

    Summary :

    If I use $arr[$i]=_select("dev_country").options[$i].text; it works in sahi controller but not as a script launched

    If I use _setValue : [Exception] TypeError: el has no properties

    If I use _set : [Exception] ReferenceError: _set is not defined

    Edit : I thought it was because the value of _set(element,value) musn't be in JavaScript so I've done :

    $temp = _select("dev_country").options[$i].text;
    _setGlobal($arr[$i], $temp);

    But it still don't work ...
  • lepierrotlepierrot Members
    edited June 2008
    var $i=0;
    var $arr = new Array();
    for($i=0;$i< _select("dev_country").options.length; $i++){
    $arr[$i] = _select("dev_country").options[$i].text;
    }
    
    Works perfectly in sahi controller but not when I playback the script.

    I've tried with _set() but it does not work on both ...
  • Help ....
  • I've found the solution the problem was that we cannot use javascript and page dependent variable in for loop :

    So I've tried this , and it works :
    var $arr = new Array();
    var $countries_gsm_d = [];
    
    function getOptions(){
        var ret = [];
        var opt = _select("dev_country").options;
        for (var i=0; i< opt.length; i++){
            ret[i] = opt[i].text;
        }
        return ret;
    }
    
    _set($countries_gsm_d, getOptions());
    
    for( var $i=0; $i < $countries_gsm_d.length; $i++){
        $arr[$i] = _select("dev_country").options[$i].text;
    }
    
  • You are right :)
  • By the way, I'd like to thank you for all you have done until now, teaching me a lot of things that allow me to now sahi better ... :)
Sign In or Register to comment.