我列出了所有存放的乘客。方法和步骤定义如下;
interactions.cs
public List<string> GetPassengerNames()
{
List<string> titleList = new List<string>();
List<string> forenameList = new List<string>();
List<string> surnameList = new List<string>();
var passengerTitles = _driver.FindElements(PassengerDetailsElements.TitleField);
var passengerForenames =_driver.FindElements(PassengerDetailsElements.ForenameField);
var passengerSurnames = _driver.FindElements(PassengerDetailsElements.SurnameField);
foreach (var passengerTitle in passengerTitles)
{
SelectElement passengerTitleSelected = new SelectElement(passengerTitle);
string passengerTitleText = passengerTitleSelected.SelectedOption.Text;
titleList.Add(passengerTitleText);
}
foreach (var passengerForename in passengerForenames)
{
forenameList.Add(passengerForename.GetAttribute("value"));
}
foreach (var passengerSurname in passengerSurnames)
{
surnameList.Add(passengerSurname.GetAttribute("value"));
}
List<string> fullNameList = titleList
.Zip(forenameList.Zip(surnameList, (firstname, lastname) => firstname + " " + lastname),
(firstname, lastname) => firstname + " " + lastname).ToList();
return fullNameList;
}
stepdefinition.cs
[When(@"all passenger details are captured")]
public void WhenAllPassengerDetailsAreCaptured()
{
var passengersList = _passengerDetails.GetPassengerNames();
ScenarioContext.Current.Add("PassengersList", passengersList);
}
这给了我以下乘客:
Mr Test Tester
Mr Testering Tester
现在在另一个specflow步骤声明上面步骤中的名称与预订联系人下拉选项匹配,html如下:
<select name="booking-contact" id="booking-contact" class="js-booking-contact-select has-value" data-vv-id="_dv78hd1js" aria-required="true" aria-invalid="false"><option disabled="disabled" hidden="hidden" value="">Choose from list</option> <option value="1">Mr Test Tester</option> <option value="2">Mrs Testering Tester</option> <option value="other">Someone else</option></select>
<option disabled="disabled" hidden="hidden" value="">Choose from list</option>
<option value="1">Mr Test Tester</option>
<option value="2">Mrs Testering Tester</option>
<option value="other">Someone else</option>
</select>
我想知道如何执行断言,该断言将检查上面的两个乘客姓名是否显示在预订联系人下拉列表中,以及第一个选项&#39;从列表中选择&#39;和其他人一起。
我该怎么做呢?
需要执行断言的步骤定义步骤将在此处进行:
[Then(@"the booking contact drop down displays the correct passengers")]
public void ThenTheBookingContactDropDownDisplaysTheCorrectPassengers()
{
var passengers = ScenarioContext.Current["PassengersList"].ToString().ToCharArray().Select(c => c.ToString()).ToList();
}
答案 0 :(得分:0)
在您的全名列表的开头添加“从列表中选择”,例如
fullNameList.Insert(0, item);
还在列表末尾添加“其他人”(考虑到它将是最后一个下拉选项)
fullNameList.add("Someone else");
现在您可以将下拉值验证为:
IWebElement comboBox = new WebDriverWait(_browserWindow, TimeSpan.FromSeconds(30)).Until(d => _browserWindow.FindElement(By.Id(id)));
SelectElement dropdownList = new SelectElement(comboBox);
int j = 0;
for (int i = 0; i < fullNameList.Length; i++)
{
if (dropdownList.Options[i].Text == passengersList[j])
{
j++;
Console.WriteLine("Value Matched");
}
else
{
throw new Exception("Data not found");
}
}