How to code generic in cucumber?
Hello everyone,
In this example, I will show you how to code generic in cucumber. For this example, I’ll use one of my project.
Let’s start.
I’ll use one of my projects. You can click and check it for this example.
So let’s start.
Normally, in cucumber, we run our tests from feature files.
For example, this is an example of a feature file. We are using keywords “Given”, “When” and “Then” and sometimes we can use “And” keywords but in this example we don’t have.
So let’s click “ When User go game’s info page”
When we click the keyword “When” we came PageSteps page. In here we defined the sentence. As you know, cucumber helps us about the coding like writing sentences. So let’s click line 23, goGameInfoPage.
So we came here.
Actually you don’t need to understand my code, this is just how I build my project and that’s why I’m showing this. As you can see, this code block is not generic. Generics code can allow you to write code that can work with different data types without sacrificing type safety. So in our example, we will see how to write changeable steps.
So let’s check this example, as you can see “Grand Theft Auto” is marked with blue color. What does it mean? This means that for this example, it will search for whatever we put in quotes. Because this is a search method. So let’s click Given sentence.
So as you can see, it directed us to the appOpen method. This method requires a String as a parameter. If we write {string} in the sentence we created for Cucumber, this will be a variable value for the given clause. If we write “Mario” in the Feature file, this String value will be Mario. So let’s click searchGame method. As you can see, it take us String text value.
So it’s very simple search method. We clicked SEARCH_TB and sent text which we got in feature file and after all, we clicked SEARCH_BUTTON.
So in this way, if we change parameters, it will check other whatever we want.
Let’s check another example.
For this example, we are taking integer value. Let’s click Given clause.
So in same way, we wrote {int} in the Given clause.
So let’s do another example. This example is like next level.
This example looks a little different. As you can see, there is a table below. This table actually tells us, if we talk specifically about the example, at first we will put 3 and 3 to the desired values, in the second scenario we will put 5 and 5, and at the end we will put 3 and 2. In this scenario, it is successful if the horizontal rows of the table are equal. In a different scenario, these scenarios may not be interconnected. This allows us to write code that is written horizontally, is easy to read, and is not long.
When we click on the Given clause, we will see the same thing we did in the example above, but as I mentioned, this is a different example in terms of table structure.
Finally, I will talk about something that has nothing to do with writing generic code for cucumber.
Here you see the Background keyword located on 3 lines.
This is a keyword that allows us to run the app opening step, which is a common step for all 5 scenarios below. Thanks to this keyword, we do not write the same thing over and over again by writing the common steps only once.
Thank you for reading. I hope it was useful.
All the best,
Ozan