selenium ide loop through array variables

16,739

this did the trick for me, hope it helps others too...

enter image description here

[info] Executing: |storeEval | new Array("postgresql","mysql"); | myarray
[info] script is: new Array("postgresql","mysql");
[info] Executing: |getEval | index=0;
[info] script is: index=0;
[info] Executing: |while | index < storedVars['myarray'].length;
[info] Executing: |storeEval | index | temp
[info] script is: index
[info] Executing: |echo | javascript{storedVars['myarray'][storedVars['temp']]}
[info] echo: postgresql
[info] Executing: |getEval | index++; 
[info] script is: index++;
[info] Executing: |endWhile | | |
[info] Executing: |while | index < storedVars['myarray'].length; | 
[info] Executing: |storeEval | index | temp |
[info] script is: index
[info] Executing: |echo | javascript{storedVars['myarray'][storedVars['temp']]}
[info] echo: mysql
[info] Executing: |getEval | index++; | |
[info] script is: index++;
[info] Executing: |endWhile | | |
[info] Executing: |while | index < storedVars['myarray'].length;

Here is the above in Source HTML for cut and paste:

<tr>
<td>storeEval</td>
<td>new Array(&quot;postgresql&quot;,&quot;mysql&quot;);</td>
<td>myarray</td>
</tr>
<tr>
<td>getEval</td>
<td>index=0;</td>
<td></td>
</tr>
<tr>
<td>while</td>
<td>index &lt; storedVars['myarray'].length</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>index</td>
<td>temp</td>
</tr>
<tr>
<td>echo</td>
<td>javascript{storedVars['myarray'][storedVars['temp']]}</td>
<td></td>
</tr>
<tr>
<td>getEval</td>
<td>index++;</td>
<td></td>
</tr>
<tr>
<td>endWhile</td>
<td></td>
<td></td>
</tr>
Share:
16,739
MAHI
Author by

MAHI

Updated on June 04, 2022

Comments

  • MAHI
    MAHI almost 2 years

    in selenium ide, i have built a test case, in which a array variable stores the values. i have used while loop to print those array variables.

    enter image description here

    here i have used "getEval | myarray[0]" to print the first value which is 'postgresql'. but the value is not listed. no error occurs.

    [info] Executing: |getEval | myarray = new Array('postgresql','mysql'); | |
    [info] script is: myarray = new Array('postgresql','mysql');
    [info] Executing: |getEval | index=0; | |
    [info] script is: index=0;
    [info] Executing: |while | index < myarray.length; | |
    [info] Executing: |getEval | myarray[0] | mynewvalue |
    [info] script is: myarray[0]
    [info] Executing: |echo | ${mynewvalue} | |
    [info] echo: ${mynewvalue}
    [info] Executing: |getEval | index++; | |
    [info] script is: index++;
    [info] Executing: |endWhile | | |
    [info] Executing: |while | index < myarray.length; | |
    [info] Executing: |getEval | myarray[0] | mynewvalue |
    [info] script is: myarray[0]
    [info] Executing: |echo | ${mynewvalue} | |
    [info] echo: ${mynewvalue}
    [info] Executing: |getEval | index++; | |
    [info] script is: index++;
    [info] Executing: |endWhile | | |
    [info] Executing: |while | index < myarray.length; | | 
    

    now again in the same test case i have changed the "getEval | myarray[0]" as "getEval | myarray[${index}] to list the array values by loop index.

    enter image description here

    now i am getting the following error:

    [info] Executing: |getEval | myarray = new Array('postgresql','mysql'); | |
    [info] script is: myarray = new Array('postgresql','mysql');
    [info] Executing: |getEval | index=0; | |
    [info] script is: index=0;
    [info] Executing: |while | index < myarray.length; | |
    [info] Executing: |getEval | myarray[${index}] | mynewvalue |
    [info] script is: myarray[${index}]
    [error] Threw an exception: missing ] in index expression 
    

    all i need is echo to print "postgresql" and "mysql" is separate new lines. i am very new to selenium plz help me to solve this.