• Selenium Video Tutorials

Selenium IDE - Control Flow



Selenium IDE commands contain features with which the conditional statements and looping can be added to the tests. This helps to run some steps only if specific conditions are satisfied or to execute the same steps multiple times depending on the particular criteria.

JavaScript Expressions

The conditions in the application under test can be verified with the help of the JavaScript expressions. The commands execute script and execute async script are used to execute a JavaScript code within our test and save the result to a variable for future use. These variables can be used in control flow commands as well. Moreover, the JavaScript expressions can be utilized with the control flow commands.

Read More:Selenium IDE - JavaScript Functions

Basic Control Flow Commands on Conditional Branching

The basic control flow commands along with their companions are listed below −

if

This is the starting command on conditional branching and can be used along with a JavaScript command to be evaluated or with the variables created before a JavaScript command. This is a part of the Target input field of the if command.

If the expression returns a true value, then the test will run the steps after if expression till the next control flow commands like the else, else if, or end is encountered. If the expression returns a false value, it will jump to the following conditional commands like the else, else if, or end.

else if

This command is used inside an if command block. Similar to an if command, the else if command accepts a JavaScript command to be evaluated. This is a part of the Target input field of the else if command.

If the expression returns a true value, then the test will run the steps after if expression till the next control flow commands like the else, else if, or end is encountered. If the expression returns a false value, it will jump to the following conditional commands like the else, or end.

else

This command is used to accommodate the last condition within the if block. If none of the previous conditions are met, the else command is executed.

end

This command is used to end the conditional command block. Without an end command in a conditional block, will give errors while running the Selenium IDE test.

Basic Control Flow Commands on Looping

The basic control flow commands on looping (that allows to repeat a group of command/commands execution) are listed below −

times

This command is used to define the count of iterations of an action/actions that need to be performed. The count of iterations is fed to the Target input field with this command. A times command should be ended with the end command.

do

This command is used to begin a loop. After the do command, the steps to be performed are declared. A do command should be ended with the repeat if command. The repeat if accepts the JavaScript expression that will be executed within the target input field. The commands post the do command will be executed initially followed by the action in the repeat if command.

If the expression returns a true value, then the test execution moves to the next do command and redo the pattern. This goes on till the condition is not satisfied anymore or in an event of infinite loop. The default number of attempts is 1000. This default number can be modified, by setting a number in the Value field of the repeat if command.

while

In this command, the JavaScript expression to be executed is provided in the Target field. If the expression returns a true value, the command block runs till the end command. Again, the control jumps back to the while command to repeat the same pattern(checking each time if a JavaScript expression yields a true or false value). A while command should terminate with the end command.

forEach

This command is used to loop through a collection and refer to individual items within a collection. In the Target field, the variable name that points to the array to be iterated is to be mentioned. Within the Value field, iterator variable name.

Example 1

Let us take an example of the below page, where we would check the text - Welcome, Login In appearing at the top using the conditional branching control flow commands.

Selenium Control Flow 1

The steps to be followed are listed below −

Step 1 − Click on the Selenium IDE extension visible on the browser after its installation.

Step 2 − Selenium IDE should be launched along with it, a welcome message should be displayed with the version of the tool. In the below image, the version installed is 3.17.2.

Along with this, it gives us the option to select what we would like to do with the tool, like Record a new test in a new project, Open an existing project, Create a new project, and Close Selenium IDE.

Also, a link with the text - Selenium IDE project page is provided, which on clicking would land us to the Selenium Integrated Development Environment documentation page.

https://www.selenium.dev/selenium-ide/.

Step 3 − Click on the Create a new project link, then enter a project name, say, Test12 under the Please provide a name for your new project. Finally, we would click on the OK button.

Step 4 − We would have the workspace ready in the Selenium IDE now.

Step 5 − Enter open in the Command field, and selenium in the Target field, to launch the application.

Step 6 − Enter store text in the Command field, xpath=//*[@id="signInForm"]/h1 in the Target field and val in the Value field. Please note, xpath=//*[@id="signInForm"]/h1 is the locator value of the text and val is the variable which we would use to store the text.

Step 7 − Enter if in the Command field and ${val}==="Welcome, Login In" in the Target field.

Step 8 − Enter echo in the Command field and Correct Verification in the Target field.

Step 9 − Enter else if in the Command field and ${val}==="Welcome, Register" in the Target field.

Step 10 − Enter echo in the Command field and InCorrect Verification in the Target field.

Step 11 − Enter else Command field.

Step 12 − Enter echo in the Command field and InValid Test in the Target field.

Step 13 − Enter end in the Command field.

Step 14 − Enter close in the Command field to close the browser window.

Selenium Control Flow 2

Step 15 − Click on the Run all tests from the top, and wait for the test execution to complete. In our example, we would see Runs: 1, Failures: 0, and a green bar denoting the test ran successfully without any failures. Also, the message that the completed successfully appeared under the Log.

Selenium Control Flow 3

In the example above, we had verified the text - Welcome, Login In and printed the message in the console - Correct Verification. Besides, we got a green tick at the top, signifying a passed test.

Example 2

Let us take an example where we would use one of the looping control flow commands called while.

The steps to be followed are listed below −

Step 1 − Follow steps 1 to 4 as mentioned in Example 1.

Step 2 − Enter execute script in the Command field, return 1 in the Target field, and val in the Value field.

Step 3 − Enter while in the Command field, and ${val} < 3 in the Target field.

Step 4 − Enter echo in the Command field, and ${val} in the Target field.

Step 5 − Enter execute script in the Command field, return ${val} + 1 in the Target field, and val in the Value field.

Step 6 − Enter end in the Command field.

Selenium Control Flow 4

Step 7 − Click on the Run all tests from the top, and wait for the test execution to complete. In our example, we would see Runs: 1, Failures: 0, and a green bar denoting the test ran successfully without any failures. Also, the message that the completed successfully appeared under the Log.

Selenium Control Flow 5

Conclusion

This concludes our comprehensive take on the tutorial on Selenium IDE - Control Flow. We’ve started with describing JavaScript Expressions, basic Control Flow Commands on Conditional Branching, Basic Control Flow Commands on Looping, and examples to walk through how to use these commands along with Selenium.

This equips you with in-depth knowledge of the Control Flow in Selenium IDE. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements