Specflow: Solution to pass many-properties-test-data from Background to Scenario Outline?

Viewed 83

In the below example, is there any way to shorten the When the doctor opens the details page of appointment step? At the moment it has to consist all information of the appointment to find the correct one from the appointment list.

Feature: Open Appointment Details page from Appointment list

Background: 
    Given the doctor has following appointments in appointment list
      | Date      | Time                | Patient | Room | First Examination |
      | 1/20/2022 | 9:00 AM - 10:00 AM  | Allan   | 1    | Yes               |
      | 1/20/2022 | 10:30 AM - 11:00 AM | Bred    | 3    | No                |
      | 1/20/2022 | 1:00 PM - 1:30 PM   | Allan   | 1    | Yes               |
      | 1/20/2022 | 2:00 PM - 3:00 PM   | David   | 4    | No                |
   
Scenario Outline: Open appointment details page
    When the doctor opens the details page of appointment <Date>, <Time>, <Patient>, <Room>, <First Examination>
    Then the appointment details page displays correct <Date>, <Time>, <Patient>, <Room>, <First Examination>
    
    Examples: 
      | Date      | Time                | Patient | Room | First Examination |
      | 1/20/2022 | 9:00 AM - 10:00 AM  | Allan   | 1    | Yes               |
      | 1/20/2022 | 10:30 AM - 11:00 AM | Bred    | 3    | No                |
      | 1/20/2022 | 1:00 PM - 1:30 PM   | Allan   | 1    | Yes               |
      | 1/20/2022 | 2:00 PM - 3:00 PM   | David   | 4    | No                |
1 Answers

When a scenario has multiple records of the same data, the key to making steps shorter is to use the minimum data required to identify one of those records. It appears the patient name and appointment date/time are unique values for a row in the Given statement. The scenario needs to be understandable to a person, so feel free to omit details from a step if they are not valuable to understand the business use case.

I would recommend changing the phrasing of the When step to only mention the patient, date and time:

When the doctor views the appointment with "<Patient>" on "<Date>" at "<Time>"

The patient, date and time should be enough to select the proper appointment with respect to the Given statement in the scenario background. The assertion in your scenario, however, might not get any shorter. It is desirable to list all of the information in the assertion that you care about. Instead, consider a vertical data table:

Then the appointment details should be:
  | Key               | Value               |
  | Date              | <Date>              |
  | Time              | <Time>              |
  | Patient           | <Patient>           |
  | Room              | <Room>              |
  | First Examination | <First Examination> |

The step definition would receive a Table parameter, and then use the table extensions to make your assertion. My example below assumes you are using Selenium to automate the browser, but you can certainly swap the code out for whichever automation framework you are using:

[Then(@"the appointment details should be:")]
public void ThenTheAppointmentDetailsShouldBe(Table table)
{
    // Assume using Selenium to automate browser
    var appointmentDetails = new AppointmentDetailsPage(driver);

    table.CompareToInstance(appointmentDetails)
}

And a stub of the AppointmentDetailsPage class to get you started:

public class AppointmentDetailsPage
{
    private readonly IWebDriver driver;

    public string Date => driver.FindElement(By.Whatever("...")).Text;
    public string Time => driver.FindElement(By.Whatever("...")).Text;
    public string Patient => driver.FindElement(By.Whatever("...")).Text;
    public string Room => driver.FindElement(By.Whatever("...")).Text;
    public string FirstExamination => driver.FindElement(By.Whatever("...")).Text;

    public AppointmentDetailsPage(IWebDriver driver)
    {
        this.driver = driver;
    }
}

The key here is that property names in AppointmentDetailsPage match the Key column in the vertical data table passed to the Then step.

Related