I am new to automation, and after some practice with Selenium, I wanted to delve into building an entire automation framework setup: Selenium, Cucumber, Jenkins, and maybe integrate a reporting tool later on.
In my initial days, I applied the Page Object Model to structure my project.
Eventually, I accumulated numerous test case Java files. Upon exploring Cucumber, I transitioned to creating step definition files and feature files. My question is: will the test case files be replaced by the step definitions, and should I run the step definition files instead?
Thank you and I hope you have a beautiful day!
I am not sure what the best practice is here
If answer your question in a nutshell: yes, your test case files will be replaced. But not with step definitions. They will be replaced with .feature files.
When working with BDD, all the tests are represented as human-readable .feature files written in Gherkin. Cucumber, as well as other BDD frameworks, translates the 'steps' from the .feature files to the methods (step definition or step implementation).
Step definitions - it is actually the code that does the described action (runs some SQL queries, navigates over UI, makes API calls, etc). It encapsulates all the 'complex'/'tech' logic that is required to perform an action described by the Given/When/Then step. Here you work with the POM you have mentioned, and so on.
To execute .feature files you should have a Test Runner. You can use e.g. JUnit or TestNG frameworks for this purpose.
So putting it all together:
And, for more clarity, let's review a short example.
Assuming you have a test for login, that looks like the following:
You should translate it to the feature file, e.g.:
Then implement 4 steps:
And create a Test Runner, let's use TestNG:
src/test/resources/features - it is a path to a folder with your .feature file(s)
stepdefs - it is a path to the package that contains step definition classes (src/test/java/stepdefs)
After this you can run the TestRunner class either directly, or via TestNG .XML file to execute your .feature file(s). But keep in mind that you need to setup the webdriver first (I expect that you know how to do it already) and provide it to the step definition class(es). Usually, test hooks and state (in the form of DI) are used for this purpose.