1

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 Answers1

0

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.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • Thanks @Greg, such a deep explaination, it has 2 points I can take into consideration: 1. Try to identify property that can make the record unique, then describe the step to be more readable `When user views the appointment on at – Steven Pham Dec 08 '21 at 17:15
  • @StevenPham: thanks. I had to make a small correction to the 'Then' step, so take a look before trying it out. – Greg Burghardt Dec 08 '21 at 17:41
  • Hi @Greg, the solution for `Then` step is trying to use Table inside a Scenario Outline and I think it will not work. I had looked around for this posibility but no luck, it also violates readability of Specflow, I think. Refer: https://stackoverflow.com/questions/5118860/specflow-cucumber-gherkin-using-tables-in-a-scenario-outline – Steven Pham Dec 14 '21 at 09:05
  • @StevenPham: I think you might be misunderstanding that question and its answer. That question was about rewriting a scenario to test combinatorial search criteria, filtering and paging through the search results. That is *way* too much scope for a scenario. Reading both your version and my version of your scenario is easy to read. The scenario you wrote seems appropriately sized for the behavior it tests. Readability is notoriously subjective. It depends on the reader. The readers should be you, developers and end users. Ask them. – Greg Burghardt Dec 14 '21 at 12:28