In this article, we will look in detail at how we can do conditional testing(or If-Else) in the Robot framework. Let’s write a simple program to deep dive.
Steps:
1. Go to Wikipedia.org
2. Search for Wikivoyage, if found, verify the title, End test
3. Search for Wikivoyage, if not found, click on Wiktionary, verify the title, End test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | *** Settings *** Documentation Wikipedia Conditional Testing Library SeleniumLibrary *** Variables *** *** Test Cases *** Verify that if you find Wikivoyage on the page, then click on it and validate (Go to If) [documentation] This test case verifies if we find Wikivoyage on the page, then we click on it. [tags] Regression Open Browser https://www.wikipedia.org/ Chrome ${count}= Get Element Count css:[data-jsl10n="wikivoyage.name"] Run Keyword If ${count} > 0 Click Wikivoyage # If the element is present we will get the value of count as 1 ... ELSE Click Wiktionary Title Should Be Wikivoyage timeout=5 Close Browser Verify that if you don't find Wikivoyage on the page, then click on Wiktionary and validate (Go to Else) [documentation] This test case verifies if we don't find Wikivoyage on the page, then we click on Wiktionary. [tags] Regression Open Browser https://www.wikipedia.org/ Chrome ${count}= Get Element Count css:wrong locator # Intentionally given wrong locator to make sure control goes to Else Run Keyword If ${count} > 0 Click Wikivoyage # If the element is not present we will get the value of count as 0 ... ELSE Click Wiktionary Title Should Be Wiktionary timeout=5 Close Browser *** Keywords *** Click Wikivoyage Click Element css:[data-jsl10n="wikivoyage.name"] Click Wiktionary Click Element css:[data-jsl10n="wiktionary.name"] |
Let’s understand the above code –
1 2 3 | *** Settings *** Documentation Wikipedia Conditional Testing Library SeleniumLibrary |
Test Suite title is Wikipedia Conditional Testing. SelenumLibrary is imported into the test suite, so that we can use Selenium commands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | *** Test Cases *** Verify that if you find Wikivoyage on the page, then click on it and validate (Go to If) [documentation] This test case verifies if we find Wikivoyage on the page, then we click on it. [tags] Regression Open Browser https://www.wikipedia.org/ Chrome ${count}= Get Element Count css:[data-jsl10n="wikivoyage.name"] Run Keyword If ${count} > 0 Click Wikivoyage # If the element is present we will get the value of count as 1 ... ELSE Click Wiktionary Title Should Be Wikivoyage timeout=5 Close Browser Verify that if you don't find Wikivoyage on the page, then click on Wiktionary and validate (Go to Else) [documentation] This test case verifies if we don't find Wikivoyage on the page, then we click on Wiktionary. [tags] Regression Open Browser https://www.wikipedia.org/ Chrome ${count}= Get Element Count css:wrong locator # Intentionally given wrong locator to make sure control goes to Else Run Keyword If ${count} > 0 Click Wikivoyage # If the element is not present we will get the value of count as 0 ... ELSE Click Wiktionary Title Should Be Wiktionary timeout=5 Close Browser *** Keywords *** Click Wikivoyage Click Element css:[data-jsl10n="wikivoyage.name"] Click Wiktionary Click Element css:[data-jsl10n="wiktionary.name"] |
In the first test case, we are checking the presence of Wikivoyage link on the webpage. To do that, we are saving the count of the element using ${count}= Get Element Count css:[data-jsl10n=”wikivoyage.name”]. Now, If the element is present then the count value will be 1, if not then 0. Then we are using an If condition to check that if the value of count is greater than 0, then we click the Wikivoyage link Run Keyword If ${count} > 0 Click Wikivoyage. And in the else condition we are checking that if the count value is less than 0, then we click the Wiktionary link. Since we know that the element is present on the page and the count value is 1, we are checking the Wikivoyage page title using Title Should Be Wikivoyage timeout=5. And then finally we are ending the test using Close Browser.
In the second test case, we are intentionally giving a wrong locator value so the count value is 0 using ${count}= Get Element Count css:wrong locator. Now we are using the same If and Else condition as test case 1. But here since the count value is 0, the else condition will be executed, which will click the Wiktionary link … ELSE Click Wiktionary. Then we are checking the Wiktionary page title using Title Should Be Wiktionary timeout=5. And then finally we are ending the test using Close Browser.
After execution, we should see that the test cases are passed.
Do check out 🙂
Github:Â https://github.com/alapanme/Robot-Framework
All Robot Framework Articles: https://testersdock.com/robot-framework-tutorial/