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 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://a9t9.com/
store true !errorIgnore
click link=helloworld
If !${LastCommandOK}
click link=anotherLink
else
echo The first link was found
endIf

Works in

Kantu for Chrome 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, 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.