How to loop tests in Selenium IDE?

允我心安 提交于 2019-11-28 20:07:28
Prashant Vadher

Do this:

  1. Download this js file: https://github.com/darrenderidder/sideflow/blob/master/sideflow.js
  2. Launch Selenium IDE from Firefox and open the options menu.
  3. Upload the .js file to the "Selenium Core extensions (user-extensions.js)" field.

The js file provides goto, gotoIf and while loop functionality in Selenium IDE. The example below shows a simple loop:

<tr>
    <td>getEval</td>
    <td>index = 0;</td>
    <td></td>
</tr>
<tr>
    <td>while</td>
    <td>index &lt; 10;</td>
    <td></td>
</tr>
<tr>
    <td>storeEval</td>
    <td>index</td>
    <td>value</td>
</tr>
<tr>
    <td>echo</td>
    <td>${value}</td>
    <td></td>
</tr>
<tr>
    <td>getEval</td>
    <td>index++;</td>
    <td></td>
</tr>
<tr>
    <td>endWhile</td>
    <td></td>
    <td></td>
</tr>

as stated in the answer above, install the user extension, which will add loop functionality to Selenium IDE tests. The example below shows a simple loop:

<tr>
<td>getEval</td>
<td>index = 0;</td>
<td></td>
</tr>
<tr>
<td>while</td>
<td>index &lt; 10;</td>
<td></td>
</tr>
<tr>
<td>storeEval</td>
<td>index</td>
<td>value</td>
</tr>
<tr>
<td>echo</td>
<td>${value}</td>
<td></td>
</tr>
<tr>
<td>getEval</td>
<td>index++;</td>
<td></td>
</tr>
<tr>
<td>endWhile</td>
<td></td>
<td></td>
</tr>

I'm new to Selenium (just started using it a few minutes ago). After a quick Google search for "selenium loop" this stackoverflow.com question came up. I immediately jumped into the extension and started using loops. The accepted answer is very helpful. However, I wanted to point out something else for others that are new to selenium (and stumble on this page).

I created a simple test for a simple web page. I added a loop so that the test would run indefinitely (until I paused/stopped it). However, I noticed that by doing this, the Runs/Failures counters within the Selenium GUI do not increment with each loop (I am guessing because a single test case was never running to completion, it was just looping indefinitely). So I dug a bit further. My goal was to leave the same test running for a long time (a few hours, or possibly overnight) to see if there were any failures (I'm chasing an intermittent bug at the moment).

The simplest way (for me, after a few minutes of searching/experimenting) was to do the following (likely no plugins needed, although the attached plugin is definitely helpful if you want to run a few small loops within a test case):

  • save the test case to a text file
  • save the test suite to a text file
  • open the test suite text file in a text editor
  • copy and paste the test case multiple times within the test suite (for example, a thousand times)
  • then open the test suite in Selenium, and run the test suite

Now I have the same simple test suite running many times, and the Runs/Failures counters are incrementing as expected (without the need for any loops).

Steve HHH

Use the Flow Control plug-in for Firefox. After restarting Firefox, use the label command to mark a point in the script, and the gotolabel command to jump there.

For example:

Or if you'd rather see the source code, this is a label:

<tr>
    <td>label</td>
    <td>start</td>  
    <td></td>
</tr>

And this causes the execution point to jump back to the label:

<tr>
    <td>gotolabel</td>
    <td>start</td>
    <td></td>
</tr>

There are other commands that you can see on the plug-in page, and documented in the Selenium IDE: Flow Control GitHub project.

No need to install/download anything, the built-in times command does this very easily:

  1. Insert a new line at the beginning of your script, select times as its Command and 10 (for instance) as its Target.
  2. Scroll down to the bottom of your script, and add a new line with end as its command
  3. Press the "Run" button as usual.
  4. Your commands are executed 10 times.

In this example I click on a button 2000 times:

To loop forever, just replace 10 with an extremely large number, that will take centuries to execute, which probably is as good as forever if you are running Selenium IDE.

This is a sample for sampcop user in order to automate spam complaints using label and goto Label commands:

1st Login on spamcop.net

2nd use Report Spam option

3rd start this script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://www.spamcop.net/sc" />
<title>testecase</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">testecase</td></tr>
</thead><tbody>

<tr>
    <td>label</td>
    <td>target1</td>
    <td></td>
</tr>
<tr>
    <td>clickAndWait</td>
    <td>link=Report Now</td>
    <td></td>
</tr>
<tr>
    <td>clickAndWait</td>
    <td>//input[@value='Send Spam Report(s) Now']</td>
    <td></td>
</tr>
<tr>
    <td>gotoLabel</td>
    <td>target1</td>
    <td></td>
</tr>
</tbody></table>
</body>
</html>
Alexandre

Your test suite file is just an HTML file, so just do the following:

<tr><td><a href="testCase1.html">testCase1</a></td></tr>
<tr><td><a href="sameStep.html">sameStep</a></td></tr>
<tr><td><a href="testCase2.html">testCase1</a></td></tr>
<tr><td><a href="sameStep.html">sameStep</a></td></tr>

Selenium IDE now has flow control. These Control Flow commands work by specifying opening and closing commands to denote a set (or block) of commands.

Available Commands

Here are each of the available control flow commands accompanied by their companion and/or closing commands.

if, else if, else, end times, end do, repeat if while, end

You can read more about it here:

https://www.seleniumhq.org/selenium-ide/docs/en/introduction/control-flow/

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