Spice Up Your 'CI/CD Process' With Automation Using Cucumber, Selenium, and Kotlin
This article shows how to use Cucumber, Selenium, and Kotlin in your CI/CD process in four easy steps.
Join the DZone community and get the full member experience.Join For Free
Continuous Integration & Continuous Deployment
Imagine you are working on a two weeks sprint, and your agile teams have different assignments that can only be tested through Integration Tests, which may take numerous builds in shorter periods. I still remember that a Build Engineer yelled at me for asking for three builds within an hour. With the power of CI/CD, 60% of this problem is solved with Rapid releases with just a click of the mouse or build hooks.
App Developer has fixed one issue that has something to do with user-login sessions or some complex logic that renders a huge list of items. It worked just fine in his local machine, but he wanted a Quality Engineer to do the regression testing.
Also, the Web Analytics team wanted to add tags and test the trends in a repeated manner, and they wanted a Quality Engineer to do the regression testing.
I hope you got my point now! These teams want to do these changes repeatedly (needless to mention the number of the builds), and they all want to perform regression tests on a Dev or SIT environment. I see this as almost a Six Months Project based on the team's availability and other dependencies.
Adding Browser Automation to your CI/CD is the best complementing solution, but how you do that is something that really matters!
I choose Cucumber, Gherkin, Kotlin, Selenium, JUnit 5, and Spring Boot framework. And IntelliJ Idea & Selenium IDE (Chrome Browser Add-On), and Chrome Webdriver (if you are running local) or Selenium Grid.
Gherkin + Cucumber
We do not go into detailed discussion about these topics as many blogs are available to explain what they are; I have added a couple of them at the end of this blog. On a high level, Gherkin is a plain English test, a well-known standard representation of Behavior Driven Development (BDD in short form) scripts. Cucumber is a testing tool that supports BDD frameworks to run your test cases written in Java, Ruby, Node, and many other languages. That association is represented as Glue in Cucumber.
You will bridge the ".feature" files written in the Gherkin language and associate them to the steps with the choice of your favorite programming language.
Cucumber + Selenium IDE
Browser Automation is often a tedious process if you do not use the correct tools. In the preliminary step to capture browser elements, if you right-click on your web browser and then select the web component by 'inspect element,' it takes forever to capture these elements in a complex flow. And the quickest possible way to capture these is by using Selenium IDE ( A Chrome Browser Add-On). Add the plugin, click the 'REC'ord button, and navigate to your complete flow. When you are done with the flow, Selenium will provide you with a wide variety of options for every single component that you interact with in your flow (CSS Selector, XPath, Relative XPath, Id and Name, XPath is always my safe bet if you ask me).
Kotlin + Cucumber
As I mentioned in my previous blog post, Kotlin eliminates most of the boilerplate code and gives you many powerful features like Null Safety, Safe Calls, Lazy Properties, Scope Functions, and last but not least, Coroutines.
If you are familiar with writing Cucumber test scripts, we often write these using the Page Object Model. It makes it much easier when using Kotlin to do this.
Now Let's Write Some Code (In Just Four Steps)
1. Write a Cucumber feature file as shown below. (Remember, the Gherkin language is just plain English.)
2. Write a Page Object file with web elements that you have captured with Selenium IDE.
3. Glue Your "search-my-name.feature" file with "SearchSteps.kt" using "SearchPage.kt" elements.
You are All Set!
4. You can do either of these to run this test.
Right click on Test Class and Run As JUnit Class
Right Click on the search_my_name.feature and run
Run the "mvn clean test" command
5. Now the Bonus Part!
Check project-root/target/cucumber-reports/cucumber.html and open it in any browser.
That's all for now, Happy Learning!
Opinions expressed by DZone contributors are their own.