how should i organise my files in my project based on best practices?

34 Views Asked by At

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

1

There are 1 best solutions below

0
sashkins On

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:

Write feature file(s) -> write code for the steps -> Configure Test Runner to map features with step definitions -> Run the Test Runner to execute the feature files.

And, for more clarity, let's review a short example.

Assuming you have a test for login, that looks like the following:

@Test
public void testLogin() {
    LoginPage loginPage = new LoginPage(driver)
    loginPage.setUsername("user123");
    loginPage.setPassword("password123");
    loginPage.clickLogin();
    Assert.assertTrue(driver.getCurrentUrl().contains("/home"));
}

You should translate it to the feature file, e.g.:

Feature: User Login

  Scenario: Successful login
    Given User is on the login page
    When User enters username "user123" and password "password123"
    And User clicks on login button
    Then User should be redirected to the home page

Then implement 4 steps:

    @Given("^User is on the login page$")
    public void userIsOnTheLoginPage() {
        driver.get("http://yourdomain.com/login");
    }

    @When("^User enters username \"([^\"]*)\" and password \"([^\"]*)\"$")
    public void userEntersUsernameAndPassword(String username, String pass) {
        loginPage.setUsername(username);
        loginPage.setPassword(pass);
    }

    @And("^User clicks on login button$")
    public void userClicksOnLoginButton() {
        loginPage.clickLogin();
    }

    @Then("^User should be redirected to the home page$")
    public void userShouldBeRedirectedToHomePage() {
        Assert.assertTrue(driver.getCurrentUrl().contains("/home"));
    }

And create a Test Runner, let's use TestNG:

@CucumberOptions(
    features = "src/test/resources/features",
    glue = {"stepdefs"}
)
public class TestRunner extends AbstractTestNGCucumberTests {
   
}

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.