By: Arif Khan.
In our last session we did following tasks with our robotic script.
- Launch the browser via robot framework
- Open the URL via robot framework
- Close the browser via robot framework
In this session we will add little advancement in our code and try convert our manual testing scenario into robotic process.
What we will try to do in this blog tutorial. See below scenario.
Robot Framework Simple Test Case | ||||
Pre Condition | ||||
1 | Lanunch eBay.com in browser | |||
Steps Seq | Testing Step | Expected Result | Actual Result | Testing Data |
1 | Click on search text box and type some search text | Robot should do the step successfully | Product Name | |
2 | Click on search button | Robot should do the click | ||
3 | Verify the result in search page | page should show the results correctly | ||
4 | Close Browser | |||
Let’s dive into the topic.
In the same pyCharm project under Tests folder create another folder and name it as eBay. Create a file in this folder name it as “ProductSearch.robot”. now add the following code in robot file. (Settings/Variables/Test Cases/Keywords)
In Settings section you can write the documentation about the test case, also you can import required libraries. As you see below.
*** Settings ***
Documentation Search Funcionality
Library SeleniumLibrary
Presently we will not use any variable that is why this section will remain blank, in future blogs we will use this section, I don’t want complicate the things at this level.
*** Variables ***
We will write our test case here, you can also write some help documentation and can add tags here along with your test case, write Open to open the browser then Maximize browser window, to enter our search product name we will use the xpath of the eBay site search item as a selector, you can find the xpath of any web item by right click on page and select inspect from context menu. After getting the correct xpath of input search we use Selenium Shortcut (Input Text), then we will press enter with the help of Selenium Shortcut (Press Keys) [Return]. Now to confirm that the result returned by the eBay contains our search product or not we use another Shortcut (Page Should Contain results for mobile). In the last Close Browser. To understand the Selenium Shortcusts and KeyWords visit Documentation
*** Test Cases ***
Verify basic search functionality of eBay
[Documentation] This test case verifies the basic product search
[Tags] Functional
Open Browser https://www.ebay.com chrome
Maximize Browser Window
Input Text //*[@id="gh-ac"] mobile
Press Keys //*[@id="gh-btn"] [Return]
Page Should Contain results for mobile
Close Browser
*** Keywords ***
We discuss in detail about this Key Section in coming up blogs presently we leave it blank
Full code look like as below.
*** Settings ***
Documentation Search functionality
Library SeleniumLibrary
*** Variables ***
*** Test Cases ***
Verify basic search funcionality of eBay
[Documentation] This test case verifies the basic search
[Tags] Functional
Open Browser https://www.ebay.com chrome
Maximize Browser Window
Input Text //*[@id="gh-ac"] mobile
Press Keys //*[@id="gh-btn"] [Return]
Page Should Contain results for mobile
Close Browser
*** Keywords ***
To execute above robot script go to pycharm terminal and write following as seen below. Hit enter.
(venv) D:\RoboFramework\pyrobotic>robot -d results Tests/eBay/ProductSearch.robot
Need to remember while working with robot framework.
Always put two spaces after every keywords used in the robot framework script, if you correctly add two spaces after the Keyword then your instruction will become italic it means that two spaces applied otherwise it won’t.
After successful execution it will return as.
ProductSearch :: ProductSearch Functionality
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
You will find testing report in Results/report.html.
What if your required result not present on the search page? Test it by yourself by changing the product name in the script like “mobileabcd” then save and re-run the script. What you noticed in the report share in comments below.
If you feel any difficulty understanding this article feel free to share with me.