Tips for executing multiple cucumber scenarios with identical initial steps and varying subsequent steps without redundancy

One problem I encountered is having duplicate steps in my feature file scenarios. The initial four lines are the same, but the succeeding steps differ for each scenario. Initially, I included all steps per scenario, but it resulted in a duplicate error. Here's an example scenario:


    Background: Login to ODB Portal
      Given that the user wanted to login at the <site>
      And user is on the <site> Page
      When user enters Email as <email> and Password as <password>
      And user click on login button
      Then the <site> Dashboard is displayed
      When the user click on Users Menu Item

    @AddNewUser
    Scenario: Add New User
      When the user click on +Add New User button
      Then user can view Add New User window
      When the user enters Account Details
      And the user enters Profile Details
      And the user enters Payment Method
      And click on Add New User button
      Then the user is created
      And has the status of "Pending"
      When user click on logout button
      Then login page is displayed
      And close the browser

    @EditUser
    Scenario: Edit Existing User
      When user click on the email link from the list of users
      Then user can view User window
      When user click on View User Information button
      Then user can view Passenger Profile
      When user click on Update User Information button
      And the user updates Profile Details
      And click on Update User button
      Then Passenger Profile is updated
      And close the browser

    <!-- More scenarios here... -->

I tried separating the repeated first four lines into a new scenario and running them together with other scenarios using tags, but I faced some challenges. Do you have any suggestions on how to avoid duplicate errors in this scenario setup? Any advice would be appreciated.

Below is the modified scenario structure:


    Background: Login to ODB Portal
      Given that the user wanted to login at the <site>
      And user is on the <site> Page
      When user enters Email as <email> and Password as <password>
      And user click on login button
      Then the <site> Dashboard is displayed
      When the user click on Users Menu Item

    <!-- Updated scenarios go here... -->

Answer №1

It seems like you are looking to handle the login process with the option of using different credentials, is that correct? In such a scenario, you can implement a single Step_Definition step within any class (not necessarily related to the specific scenario) and utilize a data table for defining your variables. Here's an example:

Given user is logged in with <Email> and <Password>
| Email   | Password  |
  <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83e2e1e0c3e7ade5">[email protected]</a>   123456

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Deactivate the dropdown menu without triggering any event

I am working on a JSP page and I need to find a way to disable a dropdown list without using an onclick method or event in the dropdown menu itself. <% boolean condition = true; if(condition){ // Here, I need to access 'plc1' ...

Testing with various browser versions using WebDriver

I'm in the process of creating a test scenario using Selenium WebDriver and planning to run it on various versions of Firefox and IE. I have already downloaded GeckoDriver and the Selenium WebDriver. My main query revolves around determining the appro ...

Is there a more efficient method for locating elements on a ReactJS webpage when using Selenium?

In my pursuit of writing web-driven tests using Selenium on a webpage constructed with reactjs, I have faced significant challenges. While I can currently extract elements by xpath, this method is far from ideal and imposes severe limitations. When any m ...

Trouble with Selecting an un-ordered list menu item in Selenium Java's side bar navigation

Exploring a different approach to selecting a menu item in the sidebar navigation within an iframe. After switching to the iframe, I am attempting to choose a specific item from the side menu to display its respective navigational items for selection. Thes ...

Selenium mistakenly chooses the incorrect element by selecting the first sibling element instead of searching within the element itself

I've been trying to loop through a list of elements and display the text, but I've encountered a strange issue with Selenium. When I select an element inside another element, Selenium returns the element inside the first sibling element instead o ...

What is the best way to extract and analyze CSS code from an HTML page using Java programming language

I'm looking to extract and parse the content of HTML pages using the Jsoup API, which can be found at . However, I've encountered a challenge in extracting CSS that is located in a different folder. How can this be achieved? Currently, my code ...

A peculiar notification from Selenium upon logging into Microsoft: "Looks like your browser is a little out of

I am currently exploring UI testing using Selenium and NUnit in C# and facing an issue with logging into Microsoft. After inputting my details through selenium, clicking on Sign-In triggers a popup message: "Your browser seems to be behaving unusuall ...

Are there any available scripts written in Selenium using Python for capturing screen recordings?

Is there a way to record the screen using selenium in Python? I have searched for solutions, but all I find are examples in Java and only for capturing screenshots. If anyone knows of a script or method for recording the screen in Python, please share it ...

experiencing empty JSONObject values

My goal is to create a JSONObject, but I am encountering an issue in Android Studio where it is returning null. Can you help me identify where I have gone wrong? I have attempted two different methods to create the JSONObject. Method 1: String JSONStrin ...

Turn off self links (HAL) in Json for Spring Data Rest

I am new to using spring-data-rest. When I make a REST call in my application, I receive JSON data that includes self-links and links to related tables in the database. However, I would prefer to receive the actual JSON data instead of the links. The code ...

Navigating through automation tests using if-else logic in the Java programming language

When it comes to automating the progression through levels in a game, I have various user groups logging into the game using a method that requires input for both username and password. However, each user group has a different number of unlocked levels ava ...

Unix system yields a blank page source when using Chrome Headless

I'm currently using Chrome Headless to execute my selenium test script on a Unix machine (Cent OS). Interestingly, the same script runs perfectly on my local Windows machine. However, when running the script on the Unix machine, it returns an empty p ...

Turn off images using Selenium Python

In order to speed up the process, I believe that disabling images, CSS, and JavaScript can help since Webdriver waits for the entire page to load before moving on. from selenium import webdriver from selenium.webdriver.firefox.firefox_profile import Firef ...

How to change the CSS style of an element using Selenium with Python

Is there a way to modify the CSS style of an element in Selenium by switching from "visibility: hidden;" to "visibility: visible;" using .execute_script? I'm open to any alternative methods that involve selenium+python. This is what my code looks lik ...

Launching Chrome using Selenium with custom options in Python

Is it possible to open Google Chrome using chromedriver without loading my cookies, passwords, history, and personal settings? I have tried adjusting the options and searching for solutions online, but so far, no luck. Here is the code snippet I attempted ...

Navigating to another webpage without closing the existing VB.Net Selenium Chrome window

Hello fellow forum members. I have a small issue with my code snippet below that uses selenium to open the Chrome web browser and navigate to a specific URL. When I click on Button1, it successfully opens a new Chrome window and directs me to . However, w ...

What is the best method to continuously refresh a page in Ruby/Capybara until a specific element is available?

Task: Currently, I am in the process of writing a script utilizing Ruby and Capybara to verify if a specific date on a calendar is available for booking. Problem: The issue arises when Capybara fails to locate the element, causing an error that brings eve ...

Utilizing a Distinctive Profile in Selenium: A Step-by-

I am currently attempting to utilize a specific Profile within my Chrome Browser. To do so, I am aware that I must utilize the user-data-dir flag and specify the path to my desired Profile. For instance, let's assume I have a Profile named Profile 1 ...

Python Selenium: How to locate elements using xpath in the presence of duplicate elements within the HTML code

Currently, I am utilizing selenium to extract data from a liquor sales website to streamline the process of adding product information to a spreadsheet. My workflow involves logging into the website using selenium and searching for the specific product. Wh ...

Guidance on incorporating static files with Spring MVC and Thymeleaf

I'm seeking guidance on how to properly incorporate static files such as CSS and images in my Spring MVC application using Thymeleaf. Despite researching extensively on this topic, I have not found a solution that works for me. Based on the recommenda ...