C#Webdriver FindsBy-用于剑道元素

时间:2019-01-02 11:07:58

标签: c# selenium selenium-webdriver pageobjects

我正在尝试在测试项目中引入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();
        }
    }
}

(...)

我该如何管理?

预先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我不能做我自己,但是你可以尝试以下方法:

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)