selenium ide loop through array variables

跟風遠走 提交于 2019-12-18 17:30:21

问题


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.

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.

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.


回答1:


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

[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>


来源:https://stackoverflow.com/questions/14955000/selenium-ide-loop-through-array-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!