If - else - endIf (expression, label) - Selenium IDE command

If is the granddaddy of all conditionals and is available in the Kantu Selenium IDE. It allows the macro, for example, to react to errors and changes on the website.

How to use if/else/endif: Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. With if/else/endif you use if to specify a block of code to be executed, if a specified condition is true and use else to specify a block of code to be executed, if the same condition is false. The expression is technically a Javascript EVAL, exactly like in storeEval.

Related user questions:

1. When I run the macro sometimes I get [error] time out when looking for element 'css=button.btn.btn-primary....'. .Is there a way to ignore these errors and go to next step? Like "if (notFound) go to next step".

2. Hi, Sometimes when a macro is playing it encounters an error and then it will not be completed (for example the image will not be found). Is there a way to override such errors? Like for example: 1. Start over. 2. Go to the next line. 3. Go to a specified line.

Solution: The solution is to use if or GotoIf together with the internal variable !LastCommandOK. You also need to set !errorIgnore to true, so the macro execution does not stop.
Example: If | ${!LastCommandOK}...else....endIf or gotoIf | ${!LastCommandOK} | Label.

The above code goes into if...endif if the last command was successful. But if you want to do something if the last command was NOT successful (element NOT found etc), then you can simply add the "!" (Javasacript not operator) in front of the internal variable.
Example: If | !${!LastCommandOK} .
The extra ! in front of the internal variable is the Javascript symbol for NOT as in "!(x == y) is true".

If_V2 uses executeScript_Sandbox

UI Vision Selenium IDE++ (Kantu) V5.0 uses executeScript_Sandbox to evaluate the expression. The old "If" continues to use storeEval. Please read here about the difference.

In the macro source code the new version of IF is "Command": "if_v2". The old version continues to use "Command": "if" for backward-compatibility.

And the new version of "If" uses end instead of endIf.

If Example

The example macro tries a second link if the first one is not found. Best if you see the flow logic in action by running the DemoIfElse macro that installs with the IDE.

Command Target Pattern/Text
open https://ui.vision/
store true !errorIgnore
click link=helloworld
If ${!LastCommandOK}
click link=anotherLink
else
echo The first link was found
end

Works in

Kantu for Chrome Selenium IDE, Kantu for Firefox Selenium IDE, Firefox IDE Classic

Related Demo Macros

DemoGotoIf, DemocsvSave

The ready-to-import-and-run source code of all demo macros can be found in the Github repository testmacros folder.

See also

gotoIf, gotoLabel, Label, while, endWhile, !LastCommandOK, !errorIgnore, Web Automation Extension User Manual, Selenium IDE commands, Selenium IDE Flow Control (SelBlocks), Classic Firefox Selenium IDE.

Anything wrong or missing on this page? Suggestions?

...then please contact us.

Kantu Selenium IDE for Chrome and Firefox - Web Test Automation
Subscribe to the a9t9 automation software newsletter . We'll send you updates on new releases that we're working on.