5 Important Tips to be Successful in Cucumber

I have been developing automation solutions for a few years. Two years ago, Cucumber came into our daily talks even though it has been around for a while. Many people talk about how Cucumber can change automation. I have asked many questions like “In case you develop your Gherkin test cases without structured sentences, how do you keep your code base updated? Don’t you need to write Java code accordingly?” or “You talk about minimizing your maintenance effort but you maintain Java part and Gherkin part together? What kind of minimizing is that?”.

I have searched for cucumber library from various sources but never liked it because I have never seen an implementation example that suits my need which is “I WON’T change my code base in case a new web component is added to my AUT

Then one of my clients decided to implement Cucumber. There you have a challenge, you have to deal with a library that you don’t like. But that’s also an opportunity as I am now able to implement a solution aligned with my motto.

We succeeded in Cucumber in two weeks. Right now, we don’t change our Java code base, we don’t add any step definition. We just write Gherkin test cases. I am going to explain how we succeeded.

Step 1: Create Identifier For All Elements

As a best practice, we want dev team to add identifiers for web element that will be interacted in automation. Best practices say that ID’s should be the first choice, then class’ should be used. Dev team suggested us to create unique data attributes to elements that we will interact because ID and class are linked to CSS and they don’t want to mess with them.

So we have elements like. I am going to use those example elements below steps.

<a href=“/login.php” class=“login” data-automation=“login-button>Giriş Yap</a>

<select class=“profile-type-class-ui” data-automation=“profile-type></select>

<option value=”0″>Profile Type</option>

<option value=”1″>Admin</option>

<option value=”2″>Manager</option>

In case no data attribute, don’t implement test case, wait for dev team to create those attribute.

Step 2: Smart Waits

Many Selenium based tests have the dependency on Fluent, Implicit or Explicit waits. As a matter of facts, while developing Gherkin test cases, you only write test steps. Test automation engineers define elements to have waited. Why would you for an object to move on to another step? Because you don’t know what you are waiting for!!! I don’t rely on that approach.

You should properly wait for DOM Ready, Ajax, JQuery or Angular calls.

Thus, your automation code will wait till any of those requests finish its execution. So learn what DOM Ready is, learn how JQuery can be detected via javascript.

Step 3: Create a Proper Test Case Template Aligned with Your Web Framework

You web app have different objects like a combobox, list boxes, text box, etc. They all behave differently. Implement a test case template aligned with your automation code structure

So every user uses the same sentences while interacting same element type.

Example scenario:

Scenario: Changing Profile Type

Given Main Page“ is loaded

When User fill form with below

 | E-mail    | [email protected] |

 | Password  | 123456           |

And Login button“ is clicked

Then Profile Page“ is loaded

When “Admin” is selected on “Profile Type” combobox

As you see, I have Profile Type combobox and Login button on my page, I am using their visible name in my test case. So how do my automation code running below Gherkin uses those variable to interact with AUT? The answer is in below step.

Step 4: Map Your Gherkin Variable with Your Data Attribute

In that step, you need to create a Mapping structure which links Gerking variable and data attributes defined in AUT. I store my variable and data-attributes in a property file. That code reads from the property file. For this step, you need to stick steps 1 and 3 strictly. You shouldn’t mess with them.

My property file:

Login Button=login-button

Profile Page=profile-image

Profile Type=profile-type

Step 5: Create Your Code Base with Minimum Viable Functions

Here are few example methods that I use.

@When ("^\"(.+)\" is selected on \"(.+)\" combobox$")

public void selectObject(String keyword,String val) {

AUTElement aut_element = getAUTElement(attrValue);

WebElement element = aut_element.getElement();

scrolltoElement(element);

Select select = new Select(element);

select.selectByVisibleText(val);

}

@And("^\"(.+)\" is clicked$")

public void clickObject(String keyword) throws Exception {

AUTElement aut_element = getAUTElement(attrValue);

WebElement element = aut_element.getElement();

scrolltoElement(element);

element.Click();

}

Run Your Cucumber Tests like a Charm

By following those steps, we don’t write any Java code, we only update the property file for new elements to interact.

1 thought on “5 Important Tips to be Successful in Cucumber”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.