Automating Toggles With Python’s Robot Framework

Advait_Next
Software QE
Published in
3 min readJan 28, 2021
Photo by Brett Jordan on Unsplash

A few months back, one of my friends asked for some help with a solution as he was stuck on a specific scenario in his automation test script development. He was automating user permission scenarios. It was a web application and permissions were defined by an admin. There were different permissions and those were enabled or disabled using toggle keys.

There were 7 unique permissions to test. He was trying to automate the scenarios as per the requirements. It was clear that it would be complex and more importantly he would face various combinations of permutations. He tried some solutions, but was facing many issues starting from identifying the buttons, their status, and finally validating results.

My stack for this test automation solution is Python’s Robot Framework using Selenium.

In the application there was a toggle key as shown below:

Toggle button

There were three scenarios I needed to validate:

1. To check whether it’s enabled or disabled

2. If it’s disabled, then need to make it enabled

3. If it’s enabled, then need to proceed to next step

Initially, I checked all possible locators for the toggle’s status. I found that there was no change in the status of any of the locators before or after the toggle’s status changed. There were no pseudo elements (before-after) present. This was a unique case.

Digging a little deeper and using different logic, I came up with a creative solution:

I used the color element from the CSS. As you can see in the image, there is a change in the background color of the toggle button. When it is disabled it is grey and upon enabling, it changes to a light green background color.

My script was checking the CSS property background color and using that as a conditional basis for the if-else logic.

Below is the snippet which could be used for this type of scenario:

#Check the status of toggle button whether its enabled or disabled.

${BG} Get WebElement xpath=(//span[@class=”slider round”])[3]

${bg color} Call Method ${BG} value_of_css_property background-color

Run Keyword If ‘${bg color}’ == ‘rgba(204, 204, 204, 1)’

… Click Element xpath=(//span[@class=”slider round”])[3]

… ELSE

… Click Element xpath=//*[@id=”app”]/div[2]/div[4]/div[1]/div[1]/div/div/ul/li[2]/a

I used the rgba color model but if required, you could use another color model too. I used the above snippet to automate around nine scenarios and it worked very well. This is a creative approach if you come across a similar scenario and need a quick solution.

--

--