Selenium IDE User Guide 3 (Control Flow)

Posted May 27, 20205 min read

The commands included with Selenium IDE allow you to add conditional logic and loops to the test.

This allows you to execute commands(or a set of commands) only when certain conditions in the application are met, or to repeat commands according to predefined criteria.

\ [](# javascript-expressions) JavaScript expression

Check the conditions in the application by using JavaScript expressions.

You can use the execute script or execute async script command to run a piece of JavaScript at any time during the test and store the result in a variable. These variables can be used in control flow commands.

You can also use JavaScript expressions directly in control flow commands.

\ [](# available-commands) available commands

Control flow commands work by specifying open and close commands to represent a set of commands(or blocks).

The following are each available control flow commands and their accompanying commands and/or close commands.

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

Let us introduce the examples one by one.

\ [](# conditional-branching) conditional branch

Conditional branches allow you to change the behavior in the test.

If Example

\ [](# if-selenium-ide-docs-en-api-commands-if) [if](/archives/selenium-ide-commands # if)

This is the open command of the conditional block.

It also provides the JavaScript expression you want to evaluate. This can include variables created from previous JavaScript expressions in the test. All of these are entered into the input field of the if command.

If the expression evaluates to true then the test will execute after it until the next conditional control flow command command(for example, else if, else, or end).

If the expression evaluates to false, it will skip subsequent commands and jump to the next relevant conditional control flow command(for example, else if, else, or end).

\ [](# else-if-selenium-ide-docs-en-api-commands-else-if) [else if](/archives/selenium-ide-commands # else-if)

This command is used in the if command block.

Just like if is evaluated using a JavaScript expression in the target input field, execute the command branch that follows it, or jump to the next related control flow command(such as else or end).

\ [](# else-selenium-ide-docs-en-api-commands-else) [else](/archives/selenium-ide-commands # else)

else is the final condition you can have in an if block. If any prerequisites are not met, this command branch will be executed.

After completion, it will jump to the end command.

\ [](# end-selenium-ide-docs-en-api-commands-end) [end](/archives/selenium-ide-commands # end)

This command terminates the conditional command block. Without it, the command block will be incomplete and you will receive a useful error message to let you know when to try to run the test.

\ [](# looping) loop playback

Looping allows you to traverse a given set of commands.

\ [](# times-selenium-ide-docs-en-api-commands-times) [times](/archives/selenium-ide-commands # times)

With times you can specify a set of commands to be executed for the number of iterations. This number enters the target input field times of the command.

To close the times command block, make sure to use the end command.

Time Example

\ [](# do-selenium-ide-docs-en-api-commands-do) [do](/archives/selenium-ide-commands # do)

You start this loop from the do command, then the command to be executed, and then end the repeat if with the command. repeat if accepts the JavaScript expression you want to evaluate in the target input field.

do will execute the following command first, and then evaluate the expression repeat if. If the expression returns, true then the test will jump back to the do command and repeat the sequence.

As an example

This will continue until the condition returns false or triggers infinite loop protection-the default is 1000 attempts. You can override this default value by specifying a number in the input field of the repeat if command.

\ [](# while-selenium-ide-docs-en-api-commands-while) [while](/archives/selenium-ide-commands # while)

By providing JavaScript expressions in while, you want to evaluate in the target input field. If it evaluates to the true command block, it will continue to execute until the end command is reached.

After completion, the test will jump back to the while command and repeat the same sequence(first check if the condition becomes true or false).

To close the while command block, use the end command.

while-example

The loop will retry until the condition returns false or the infinite loop protection is triggered(default is 1000 attempts). You can override this default value by specifying a number in the input field of the while command.

\ [](# foreach-selenium-ide-docs-en-api-commands-for-each) [forEach](/archives/selenium-ide-commands # for-each)

Trying to do our best, we have the ability to traverse a collection(for example, a JS array) and reference each item in the collection when we perform the process.

In the target field, specify the name of the variable containing the array to be iterated. In the value field, specify the name of the iterator variable to use. For each entry in the array, the following command will be executed. During each iteration, the contents of the current entry will be accessed through iterator variables.

Every example

\ [](# nesting-commands) nested commands

You can nest control flow commands as needed(for example, an if block can be placed inside a while block, and vice versa).

Nesting example

\ [](# syntax-validation) syntax verification

If you are not sure whether the control flow syntax is correct, try running a test to see. The IDE will find errors in the control flow syntax and call up specific commands that are incorrect or missing.

Error example

  1. Selenium IDE User Guide 1(Crawler Script Recorder)
  2. Selenium IDE User Guide 2(Command Line Runner)
  3. Selenium IDE User Guide 3(Control Flow)
  4. Selenium IDE User Guide 4(Code Export)
  5. Selenium IDE User Guide 5(FAQ)
  6. Selenium IDE User Guide 6(Instruction List)
  7. Selenium IDE User Guide 7(Parameter Definition)

Author: distributed programming
Source: https://zthinker.com/
If you like this article, please long press the QR code and follow Distributed Programming
. Distributed programming