& # 39; dropdownlist # 39;有一个无效的SelectedValue,因为它不存在于项目列表中
本文关键字:于项目 项目 列表 不存在 无效 有一个 dropdownlist SelectedValue 因为 | 更新日期: 2023-09-27 18:17:23
我目前正在测试一个web应用程序的下拉列表的功能。我是新的测试,所以我不太确定如何避免得到这个错误。在做了一些研究之后,我知道我必须包含一个try catch块来处理异常。到目前为止,当我运行测试时,我能够测试下拉列表中的第一个选项。最后,我试着测试下拉列表中的每个选项,考虑到每个选项将用户引导到不同的页面。下面是我的代码:
public void GoToProductSelection(string choice)
{
this.GoToFacilitySelection();
IWebElement productsDdl = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementExists(By.Id("ctl00_ContentPlaceHolder1_ddlProducts")));
SelectElement options = new SelectElement(productsDdl);
options.SelectByText(choice);
IWebElement product = options.SelectedOption;
IWebElement contBtn;
IWebElement selCheckBox;
string val = (product.GetAttribute("value"));
IWebElement addToOrderBtn = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_btnAddItem"));
addToOrderBtn.Click();
try
{
}
catch (NoAlertPresentException)
{
}
}
我不确定您的问题是关于try/catch,一般测试还是实际抛出的错误(无效选定值)。
try/catch的结构应该如下所示。
public void GoToProductSelection(string choice)
{
try
{
this.GoToFacilitySelection();
IWebElement productsDdl = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementExists(By.Id("ctl00_ContentPlaceHolder1_ddlProducts")));
SelectElement options = new SelectElement(productsDdl);
options.SelectByText(choice);
IWebElement product = options.SelectedOption;
IWebElement contBtn;
IWebElement selCheckBox;
string val = (product.GetAttribute("value"));
IWebElement addToOrderBtn = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_btnAddItem"));
addToOrderBtn.Click();
}
catch (NoAlertPresentException)
{
//code to handle NoAlertPresentException
}
}
当在try块中抛出异常时,CLR查找处理此异常的catch语句。如果try块中没有任何内容,则在该块中不会抛出异常,也不会执行catch。