![](/img/trans.png)
[英]C# Webdriver FindsBy - Combining FindsBy Statements with similar text in ID variable in a Page Object Framework Class
[英]C# Webdriver FindsBy - for Kendo elements
我正在尝试在测试项目中介绍FindsBy
public class BoaRegistrationPage
{
public IWebDriver Driver;
public BoaRegistrationPage(IWebDriver driver)
{
this.Driver = driver;
PageFactory.InitElements(driver, this);
}
[FindsBy(How = How.Id, Using = "ReportingPeriodName")]
public SeleniumKendoDropDownList ReportingPeriodDropDown { get; set; }
[FindsBy(How=How.Id, Using ="BranchCode")]
public IWebElement BranchCode { get; set; }
}
(......)
不幸的是,如您所见,我还需要在返回错误的IWebElement以外的类型上使用它(SeleniuKendoDropDownList)。
namespace CompanyReviewSeleniumTests.Wrappers
{
public class SeleniumKendoDropDownList : KendoDropDownList
{
public SeleniumKendoDropDownList(IWebElement webElement) : base(webElement)
{
this.CopyInternalId(webElement);
}
public void SelectByDataItemProperty(string propertyName, string text)
{
Driver.JavaScripts()
.ExecuteScript(
string.Format(
CultureInfo.InvariantCulture,
"$('{0}').data('{1}').select(function(dataItem) {{return dataItem.{3} === '{2}';}});",
ElementCssSelector,
SelectType,
text,
propertyName));
}
public new void SelectByText(string text)
{
WaitUntilOptionsLoaded();
Open();
var listBoxElement = Driver.FindElement(By.CssSelector($"{this.ElementCssSelector}_listbox"));
listBoxElement.FindElement(By.XPath($".//*[contains(text(),'{text}')]"))?.JavaScriptClick();
}
}
}
(......)
我该如何管理?
预先感谢您的帮助
我不能自己,但您可以尝试以下方法:
public class BoaRegistrationPage
{
public IWebDriver Driver;
private SeleniumKendoDropDownList _dropDownList;
public BoaRegistrationPage(IWebDriver driver)
{
this.Driver = driver;
this._dropDownList = null;
PageFactory.InitElements(driver, this);
}
[FindsBy(How = How.Id, Using = "ReportingPeriodName")]
public IWebElement ReportingPeriodDropDown
{
get { return _dropDownList; }
set { this._dropDownList = new SeleniumKendoDropDownList(value); }
}
[FindsBy(How=How.Id, Using ="BranchCode")]
public IWebElement BranchCode { get; set; }
}
由于KendoDropDownList
继承自RemoteWebElement
类,而后者又实现了IWebElement
接口,因此应该可以使用。 但是,如果需要从外部访问SeleniumKendoDropDownList的属性和方法,则必须进行强制转换,例如:
((SeleniumKendoDropDownList)boaRegPage.ReportingPeriodDropDown).SelectByDataItemProperty(property, text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.