Cucumber (Gherkin) Implementing Step Definitions Estimated reading: 4 minutes 236 views Now is the time to create and implement our Step Definition Class in Cucumber. This critical step will ensure our test automation framework is robust and efficient. In this guide, we will walk you through the process of setting up your Step Definition Class, specifically focusing on creating a file to store your HomeStepDefs file within your project.Setting Up Your Step Definition ClassTo begin, navigate to the appropriate location in your project directory where you want to store your Step Definition files. It’s essential to keep your project well-organized, making it easier to maintain and scale as needed. In this case we’ll be storing our files in this location – src/test/java/com/kicksapp/stepDefs Cucumber Step Definitions Directory IDE: Launch Eclipse and open your project to get started on your Gherkin test definitions. Directory: Navigate to the src/test/java/com/kicksapp/stepDefs/ directory, where your step definitions will be organized. Create a New File: In the stepDefs directory, right-click and choose New File to create a new file named HomeStepDefs.java, which will house your Gherkin step definitions for the Home feature.HomeStepDefssrc/test/java/kicksapp/stepDefs/HomeStepDefs.javaConvert Project to A Cucumber ProjectRight-click on your project and select Configure Convert to Cucumber Project . This set is necessary so we can generate our gherkin snippets. Converting Project To A Cucumber ProjectGenerate Gherkin Method SnippetsTo generate the necessary Gherkin method snippets, run the home-screen.feature file: Run Feature File: Right-click on the home-screen.feature file and select Run As Cucumber Feature . Review Snippets: Eclipse will execute the feature file, and if any step definitions are missing, it will generate method snippets for you. Copy these snippets into your HomeStepDefs.java class. Gherkin Generated Sample SnippetsGherkin Method Snippets@Given("the user is on the home screen") public void the_user_is_on_the_home_screen() { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @When("the user clicks the {string} icon") public void the_user_clicks_the_icon(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @Then("the {string} screen is displayed") public void the_screen_is_displayed(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); } @When("the user clicks the {string} link") public void the_user_clicks_the_link(String string) { // Write code here that turns the phrase above into concrete actions throw new io.cucumber.java.PendingException(); }Now that we have our snippets, it’s time to add the necessary logic to each method to complete our HomeStepDefs class. This step is crucial as it bridges the gap between our feature files written in Gherkin and the actual automation code that interacts with our web application.In the HomeStepDefs class, we initialize a WebDriver instance through the Hooks class, ensuring a consistent browser session. The testUrl variable holds the URL of our web application, setting the stage for user interaction scenarios.HomeStepDefs – WebDriverpublic class HomeStepDefs { public WebDriver driver = Hooks.driver; private final String testUrl = "https://jahmalrichard.github.io/kicks-flutter-web-app/";Navigating to the Home ScreenThe first step is to navigate the user to the home screen. Using the @Given annotation, we define the method the_user_is_on_the_home_screen. This method directs the browser to the specified URL and pauses for 2 seconds to ensure the page is fully loaded. Kicks App | Home Screen@Given@Given("the user is on the home screen") public void the_user_is_on_the_home_screen() throws InterruptedException { driver.get(testUrl); Utils.wait(2); }Interacting with IconsNext, we manage user interactions with icons on the home screen. The @When annotation is used to define the method the_user_clicks_the_icon. This method receives an icon name as a parameter, locates the corresponding icon using a CSS selector, and performs a click action. Find & Click The Login Icon@When@When("the user clicks the {string} icon") public void the_user_clicks_the_icon(String iconName) { try { switch(iconName.toLowerCase()) { case "login": { WebElement loginIcon = driver.findElement( By.cssSelector("flt-semantics[aria-label=\"login-icon\"]")); loginIcon.click(); Utils.wait(2); break; } default: System.err.println("The icon called '" + iconName + "' was not found. Try again."); } } catch (Exception e) { e.printStackTrace(); } }Verifying Screen DisplaysAfter interacting with icons, it’s essential to verify that the correct screen is displayed. The @Then annotation defines the method the_screen_is_displayed, which checks for the presence of the Sign In button on the login screen to confirm successful navigation. Verify That The Sign In Button Exist Verifying That The Product Page Is Displayed@Then@Then("the {string} screen is displayed") public void the_screen_is_displayed(String screenName) { try { switch(screenName.toLowerCase()) { case "login": { WebElement loginSignInButton = driver.findElement(By.cssSelector("flt-semantics[aria-label=\"Sign In\"]")); assert loginSignInButton.isDisplayed(); Utils.wait(2); break; } case "products": { WebElement filterMostPopular = driver.findElement(By.cssSelector("flt-semantics[aria-label=\"Most popular\"]")); assert filterMostPopular.isDisplayed(); Utils.wait(2); break; } default: System.err.println("The screen named '" + screenName + "' was not found. Try again."); } } catch (Exception e) { e.printStackTrace(); } }Navigating via LinksLastly, we enable users to click on various links within the application. The @When annotation is used again to define the method the_user_clicks_the_link, which handles click actions for specified links. Tip: Navigating LinksThis method is related to the second scenario, where we click on the Products link and verify that we’re on he Products screen. User Clicks The Products Link@When@When("the user clicks the {string} link") public void the_user_clicks_the_link(String linkName) { try { switch(linkName.toLowerCase()) { case "products": { WebElement productsNavLink = driver.findElement( By.cssSelector("flt-semantics[aria-label=\"Products\"]")); productsNavLink.click(); Utils.wait(2); break; } default: System.err.println("The link named '" + linkName + "' was not found. Try again."); } } catch (Exception e) { e.printStackTrace(); } } } Tip: HomeStepDefsBy integrating these methods into our HomeStepDefs class, each method is crafted to handle specific interactions and verifications, making our tests both comprehensive and easy to understand.ConclusionNow that we have a solid grasp of creating and managing step definitions, it’s time to elevate our Cucumber skills further. In next guide, we’ll delve into Understanding Cucumber Tags, a powerful feature that can enhance the flexibility and efficiency of your testing framework.Tagged:Cucumber Cucumber (Gherkin) - Previous Scenario Outlines and Examples Next - Cucumber (Gherkin) Understanding Cucumber Tags