Specflow:将许多属性测试数据从后台传递到场景大纲的解决方案?
Specflow: Solution to pass many-properties-test-data from Background to Scenario Outline?
在下面的示例中,有什么方法可以缩短 When the doctor opens the details page of appointment
步骤?目前它必须包含约会的所有信息才能从约会列表中找到正确的人。
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 |
当一个场景有多个相同数据的记录时,缩短步骤的关键是使用识别其中一个记录所需的最少数据。患者姓名和预约 date/time 似乎是 Given
语句中一行的唯一值。该场景需要一个人可以理解,所以如果它们对理解业务用例没有价值,请随意省略步骤中的细节。
我建议更改 When
步骤的措辞以仅提及患者、日期和时间:
When the doctor views the appointment with "<Patient>" on "<Date>" at "<Time>"
患者、日期和时间应该足以select根据场景背景中的Given
语句进行适当的预约。但是,您场景中的断言可能不会变得更短。最好列出您关心的断言中的所有信息。相反,考虑垂直数据 table:
Then the appointment details should be:
| Key | Value |
| Date | <Date> |
| Time | <Time> |
| Patient | <Patient> |
| Room | <Room> |
| First Examination | <First Examination> |
步骤定义将接收一个 Table 参数,然后使用 table extensions to make your assertion。我下面的示例假设您正在使用 Selenium 来自动化浏览器,但您当然可以将代码换成您正在使用的任何自动化框架:
[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)
}
以及 AppointmentDetailsPage
class 的存根以帮助您入门:
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;
}
}
此处的关键是 AppointmentDetailsPage
中的 属性 名称与传递给 Then
步骤的 vertical data table 中的 Key
列相匹配。
在下面的示例中,有什么方法可以缩短 When the doctor opens the details page of appointment
步骤?目前它必须包含约会的所有信息才能从约会列表中找到正确的人。
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 |
当一个场景有多个相同数据的记录时,缩短步骤的关键是使用识别其中一个记录所需的最少数据。患者姓名和预约 date/time 似乎是 Given
语句中一行的唯一值。该场景需要一个人可以理解,所以如果它们对理解业务用例没有价值,请随意省略步骤中的细节。
我建议更改 When
步骤的措辞以仅提及患者、日期和时间:
When the doctor views the appointment with "<Patient>" on "<Date>" at "<Time>"
患者、日期和时间应该足以select根据场景背景中的Given
语句进行适当的预约。但是,您场景中的断言可能不会变得更短。最好列出您关心的断言中的所有信息。相反,考虑垂直数据 table:
Then the appointment details should be:
| Key | Value |
| Date | <Date> |
| Time | <Time> |
| Patient | <Patient> |
| Room | <Room> |
| First Examination | <First Examination> |
步骤定义将接收一个 Table 参数,然后使用 table extensions to make your assertion。我下面的示例假设您正在使用 Selenium 来自动化浏览器,但您当然可以将代码换成您正在使用的任何自动化框架:
[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)
}
以及 AppointmentDetailsPage
class 的存根以帮助您入门:
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;
}
}
此处的关键是 AppointmentDetailsPage
中的 属性 名称与传递给 Then
步骤的 vertical data table 中的 Key
列相匹配。