硒测试无法单击垫选择元素

时间:2019-12-03 12:03:55

标签: c# selenium angular-material

“表单字段”(mat-select)中的“角材料选择”(mat-form-field)忽略了硒的点击

DOM part

运行测试时,Selenium单击后不会显示下拉列表。如果由我手动完成,则测试将继续并通过。 我也尝试使用SelectElement类,但由于mat-select不使用任何选择元素,因此它不适用

公共类页面定义:

[FindsBy(How = How.XPath, Using = "//*[@id='form']//mat-form-field//*[@class='mat-form-field-flex' and .//*[@formcontrolname='network']]")] // also tried all surrounding elements from DOM including mat-form-field and mat-select (look at attached screenshot)
public IWebElement _formNetworkFormControl;

public bool IsFormNetworkFormControlComponentPresent()
{
    return ExtendedWebElementOperations.IsElementDisplayed(_formNetworkFormControl);
}

// Click dropdown
public void ClickFormNetworkFormControl(IWebDriver webdriver)
{
    // Wait is needed until data is loaded and select is enabled
    WebDriverWait wait = new WebDriverWait(webdriver, timeout: TimeSpan.FromSeconds(15));
    wait.Until(ExpectedConditions.ElementToBeClickable(_formNetworkFormControl));

    _formNetworkFormControl.Click();
}

// To select option after dropdown click
public void SelectByOptionName(IWebDriver webdriver, string optionName)
{
    string xpath = $"//mat-option[span[text()[contains(.,'{optionName}')]]]";
    WebDriverWait wait = new WebDriverWait(webdriver, timeout: TimeSpan.FromSeconds(5));
    wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(xpath)));

    IWebElement optionNameSelect = webdriver.FindElement(By.XPath(xpath));

    optionNameSelect.Click();
}

测试:

var testedPage = new Page(Driver);

testedPage.ClickAddRingTestFormNetworkFormControl(Driver); // click is made but dropdown doesn't appear
testedPage.SelectByOptionName(Driver, networkName); // if select is clicked manually, this works great

1 个答案:

答案 0 :(得分:1)

问题在于,mat-select在装入所需数据之前一直具有aria-disabled='true',因此

wait.Until(ExpectedConditions.ElementToBeClickable(_formNetworkFormControl));

不涉及这种情况。

我通过以下方式更新Page类来修复它:

public IWebElement IsElementHasTrueAriaDisabledAttribute(IWebDriver webdriver, IWebElement element)
{
    if (element.GetAttribute("aria-disabled").Equals("false"))
    {
        return element;
    }

    return null;
}

public void ClickFormNetworkFormControl(IWebDriver webdriver)
{
    WebDriverWait wait = new WebDriverWait(webdriver, timeout: TimeSpan.FromSeconds(15));
    wait.Until<IWebElement>((d) => 
    {
        return IsElementHasTrueAriaDisabledAttribute(d, _formNetworkFormControl);
    });

    _formNetworkFormControl.Click();
}
相关问题