Grocery pickup and delivery services are increasing in popularity. Global companies such as Aldi, Amazon, Lidl, Target, and Walmart have entered the online grocery pickup and delivery market. To attract and retain customers, they must keep their websites working efficiently and free of bugs. Robot Framework is an ideal way for these companies to test the functionality of their websites at a low cost. This article demonstrates the end-user shopping for grocery items at Target using minimal code (low code).
The International Friendship Business Club (IFBC) recently held a meeting. Members noticed that their friends from Kiribati were quiet and downcast. IFBC members decided to give a feast with native Kiribati food to help take their minds off their homeland’s climate issues. The dinner menu for the feast is:
Kiribati Pumpkin Coconut Soup
Roasted Shrimp with Coconut Curry Dipping Sauce
Palusami
Tomato Coconut Pasta
Mushroom Chicken
Batata Mash
Milk Rice
Tea
Coffee
After checking their pantries, the cooking committee was missing some items. They decided to place an online grocery order at Target.com for pickup.
Knowledge of Selenium
Knowledge of Robot Framework
Computer with macOS, Windows, Linux, or Unix. This article will show the Mac setup.
Python 3.5+
Robot Framework
Selenium Chrome WebDriver for Chrome version installed
PyCharm
Open PyCharm.
Install the plugins – PyCharm -> Preferences -> Python Interpreter ->
Install:
Selenium 3.141.0
robotframework 4.0.0
robotframework-pythonlibcore 2.2.1
robotframework-seleniumlibrary 5.1.3
robotframework-selenium2library 3.0.0
robotframework-dependencylibrary 1.0.0.post1
webdrivermanager 0.10.0
Robot Framework Support
Create a new project named LowCodeAutomationDemo – File -> New Project.
Create five new directories: Libraries, Resources, Results, Tests, and Variables – Right-click on the project name -> New -> Directory
Create one file in the Libraries directory: SelectSuggestion.py
– Right-click on the Libraries directory -> New -> File
Create four files in the Resources directory: ChromeOptions.robot, Common.robot, SetLocation.robot, and Shopping.robot – Right-click on the Resources directory -> New -> File
Create one file in the Test directory: GroceryList.robot
– Right-click on the Test directory -> New -> File
Create one file in the Variables directory: ItemList.py
– Right-click on the Variables directory -> New -> File
The test scripts will use Selenium Library keywords, custom library keywords, and a Python variable file. Because this demo project mimics the action of the end-user, the main test script has a Tasks section instead of a Test Cases section.
The ChromeOptions.robot file has Settings and Keywords sections. The test script configures Chrome by disabling notifications and setting the browser for incognito mode.
Scenario
Define a variable.
Add an option to disable the notification.
Add an option to start the browser in incognito mode.
Create and add options to Chrome web driver.
Following the Don’t Repeat Yourself (DRY) principle, functions used by multiple scripts are in this file. The file has the Settings and Keywords sections. The script calls the configuration script, launches, and closes the browser.
Scenario
Call script to configure and open Chrome – start the test suite.
Go to the specified URL.
Maximize the browser window.
Close the browser – end the test suite.
The SetLocation.robot script contains the keywords to search, select, and set the Target store location. The file has the Settings, Variables, and Keywords sections.
Scenario
Click on the select button.
Wait until the side menu opens.
Enter the zip code.
Click on the ‘Lookup’ button.
Wait until the store location loads.
Click on the ‘Set as my store’ button.
The Shopping.robot script is where most of the work occurs. The test script uses advanced Robot Framework concepts to search, select, and add items to the cart. The file has the Settings, Variables, and Keywords sections. Due to the dynamic nature of the website, the test script waits for the element to load. It is best practice to set an implicit wait instead of using the sleep keyword. However, this website intercepts the Selenium implicit wait keyword.
Scenario
Import SeleniumLibrary, Collections, SelectSuggestion libraries.
Import the Variables Python file.
Get dictionary keys from ItemList.py
.
Get dictionary values from ItemList.py
.
Get the number of items in the list.
Create a loop to:
Get the item from the keys list.
Enter the item in the search box.
Get the search locator from the values list.
Get the item locator from the values list.
Get the item to purchase from the values list.
Search for item.
Select the item from the auto-suggestion list using the custom library keyword.
Select ‘In-store pickup’ if it is the first iteration of the loop.
Scroll the item to purchase into view.
Click the ‘Add for pickup’ button.
Click the ‘Continue shopping’ button.
Clear the search field
View shopping cart.
Checkout.
Check if the sign-in page displays.
The GroceryList.robot contains the keywords to run the tasks. The file has the Settings, Variables, and Keywords sections. The Settings section imports other test scripts and the suite setup and teardown. This project uses suite setup and suite teardown because it mimics the action of the end-user from start to finish.
Scenario
Create variables for the browser and the URL.
Select the location.
Start shopping.
Checkout.
SelectSuggestion.py
is a custom library file. It is important to note that Robot Framework reads import statements and method names as keywords. There are multiple ways to specify keywords. This demo project sets auto keywords to false. The keyword declarator lets Robot Framework know to use the method name as a keyword.
Scenario
Get the current web driver. Please note that declaring a new Selenium web driver will open a new browser instead of using the current browser window.
Select the item from the auto-suggestion dropdown menu. A custom keyword is defined to select an option.
The ItemList.py
is a Python dictionary variable (data) file. There are some Robot Framework keywords available to create lists and dictionaries. However, the keywords do not allow you to create keys with a list of values. Each dictionary key in ItemList.py
has three values.
Open a terminal window in PyCharm. At the command prompt type, robot -d results tests/grocerylist.robot
. The -d instructs the compiler to store the results in the Results directory. After the test suite finishes, the Result directory contains the log.html, output.xml, and report.html files. You can view the log.html or results.html file in the browser by right-click -> Open In -> Browser -> Chrome.
Congratulations on creating a low code Robot Framework project that can do the shopping for you. Advanced concepts allow you to write more robust and efficient tests. Here are the links for the source code and a video demonstration of the project.
Way to go!