如何使用Selenium C#创建包含display:Block display:none的加载微调器方法?

如何使用Selenium C#创建包含display:Block display:none的加载微调器方法?,c#,selenium,kendo-ui,C#,Selenium,Kendo Ui,在我的测试中,我没能抓住加载器。我需要创建一个方法来帮助我查看加载程序何时不再可见。由于此问题,我无法选择任何页面元素 尝试的代码: public static void SeeNoLoaderInQD() { Actor.Wait.UntilElementIsNotDisplayed(Elements.QDLoader); Assert.IsTrue(Elements.SeeException.Count == 0); } 我想试试这样的方法: public sta

在我的测试中,我没能抓住加载器。我需要创建一个方法来帮助我查看加载程序何时不再可见。由于此问题,我无法选择任何页面元素

尝试的代码:

public static void SeeNoLoaderInQD() 
{ 
    Actor.Wait.UntilElementIsNotDisplayed(Elements.QDLoader); 
    Assert.IsTrue(Elements.SeeException.Count == 0); 
} 

我想试试这样的方法:

public static void WaitForElementAttributeValue(this IWebDriver driver, By by, string attribute, string value)
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    wait.Until(d => d.FindElement(by).GetAttribute(attribute).Contains(value));
}
driver.WaitForElementAttributeValue(By.Id("line-scale-loader"), "style", "display: none;");
您可以针对您的场景调用此方法,如下所示:

public static void WaitForElementAttributeValue(this IWebDriver driver, By by, string attribute, string value)
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    wait.Until(d => d.FindElement(by).GetAttribute(attribute).Contains(value));
}
driver.WaitForElementAttributeValue(By.Id("line-scale-loader"), "style", "display: none;");

调用此方法时,您告诉WebDriver等待WebElement的
样式
属性值等于
显示:无表示加载程序不再可见

我想试试这样的东西:

public static void WaitForElementAttributeValue(this IWebDriver driver, By by, string attribute, string value)
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    wait.Until(d => d.FindElement(by).GetAttribute(attribute).Contains(value));
}
driver.WaitForElementAttributeValue(By.Id("line-scale-loader"), "style", "display: none;");
您可以针对您的场景调用此方法,如下所示:

public static void WaitForElementAttributeValue(this IWebDriver driver, By by, string attribute, string value)
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    wait.Until(d => d.FindElement(by).GetAttribute(attribute).Contains(value));
}
driver.WaitForElementAttributeValue(By.Id("line-scale-loader"), "style", "display: none;");

调用此方法时,您告诉WebDriver等待WebElement的
样式
属性值等于
显示:无表示加载程序不再可见

Selenium内置了
ExpectedConditions
,您可以将其与
WebDriverWait
一起使用来处理类似情况。可能的情况是,在加载程序变为可见之前,您的等待不可见正在触发并通过,这可能会导致以后的操作失败,因为加载程序出现得较晚。您可以先添加“等待可见”,然后添加“等待不可见”

By qdLoaderLocator = By.Id("line-scale-loader");
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(qdLoaderLocator));
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(qdLoaderLocator));

旁注。。。不要像这样使用断言

Assert.IsTrue(Elements.SeeException.Count == 0);
因为当他们失败时,你会得到

Expected: true
Actual: false
Verify no exceptions
Expected: 0
Actual: 2
这并没有告诉你任何事情。而是使用以下选项之一

Assert.Zero(Elements.SeeException.Count, "Verify no exceptions");
Assert.AreEqual(0, Elements.SeeException.Count, "Verify no exceptions");
现在你将得到

Expected: true
Actual: false
Verify no exceptions
Expected: 0
Actual: 2

在这种情况下,它可能看起来不太有用,但至少您可以获得比正确/错误更多的信息。当您开始比较字符串、数字等时,它将非常有用。

Selenium内置了
ExpectedConditions
,您可以使用
WebDriverWait
来处理类似情况。可能的情况是,在加载程序变为可见之前,您的等待不可见正在触发并通过,这可能会导致以后的操作失败,因为加载程序出现得较晚。您可以先添加“等待可见”,然后添加“等待不可见”

By qdLoaderLocator = By.Id("line-scale-loader");
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(qdLoaderLocator));
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(qdLoaderLocator));

旁注。。。不要像这样使用断言

Assert.IsTrue(Elements.SeeException.Count == 0);
因为当他们失败时,你会得到

Expected: true
Actual: false
Verify no exceptions
Expected: 0
Actual: 2
这并没有告诉你任何事情。而是使用以下选项之一

Assert.Zero(Elements.SeeException.Count, "Verify no exceptions");
Assert.AreEqual(0, Elements.SeeException.Count, "Verify no exceptions");
现在你将得到

Expected: true
Actual: false
Verify no exceptions
Expected: 0
Actual: 2

在这种情况下,它可能看起来不太有用,但至少您可以获得比正确/错误更多的信息。当您开始比较字符串、数字等时,这将非常有用。

要检查页面是否已加载,我通常会检查3个条件,它们是:-

  • 页面准备好了吗
  • ajax调用完成了吗
  • 旋转器/加载器是否不可见
示例代码

    public static void WaitForPageToLoad(IWebDriver driver)
    {           
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
        wait.Until(driver =>
        {               
            bool isPageReady = (bool)((IJavaScriptExecutor)driver).
                ExecuteScript("return document.readyState == 'complete'");               
            bool isAjaxFinished = (bool)((IJavaScriptExecutor)driver).
            ExecuteScript("return jQuery.active == 0");
            bool isLoaderInvisible = (bool)((IJavaScriptExecutor)driver).
                ExecuteScript("return $('#line-scale-loader').is(':visible') == false");
            return isPageReady && isAjaxFinished && isLoaderInvisible;
        });          
    }

希望这有帮助。

要检查页面是否已加载,我通常会检查3个条件,它们是:-

  • 页面准备好了吗
  • ajax调用完成了吗
  • 旋转器/加载器是否不可见
示例代码

    public static void WaitForPageToLoad(IWebDriver driver)
    {           
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
        wait.Until(driver =>
        {               
            bool isPageReady = (bool)((IJavaScriptExecutor)driver).
                ExecuteScript("return document.readyState == 'complete'");               
            bool isAjaxFinished = (bool)((IJavaScriptExecutor)driver).
            ExecuteScript("return jQuery.active == 0");
            bool isLoaderInvisible = (bool)((IJavaScriptExecutor)driver).
                ExecuteScript("return $('#line-scale-loader').is(':visible') == false");
            return isPageReady && isAjaxFinished && isLoaderInvisible;
        });          
    }

希望这能有所帮助。

您能添加您尝试过的代码吗?公共静态无效请参见NoLoaderinQD(){Actor.Wait.UntileLementsNotDisplayed(Elements.QDLoader);Assert.IsTrue(Elements.SeeException.Count==0);}您能添加您尝试过的代码吗?公共静态无效请参见NoLoaderinQD(){Actor.Wait.UntilElementIsNotDisplayed(Elements.QDLoader);Assert.IsTrue(Elements.seeeexception.Count==0)}这仅在所定位的特定元素包含此属性时有效。如果父元素之一具有“display:none”会怎样这会导致所需元素不显示?为什么不使用内置的
ExpectedConditions
并处理这两种/所有情况?根据提供的HTML示例,加载微调器似乎出现在DOM的顶部,因此不存在隐藏父元素的风险。
ExpectedConditions
在.NET bin中被弃用dings并要求现在使用
DotNetSeleniumExtras.WaitHelpers
,因此我尝试提供一个示例,如果可能,它不需要添加任何其他包。这仅在所定位的特定元素包含此属性时才起作用。如果父元素之一具有“display:none”该怎么办这会导致所需元素不显示?为什么不使用内置的
ExpectedConditions
并处理这两种/所有情况?根据提供的HTML示例,加载微调器似乎出现在DOM的顶部,因此不存在隐藏父元素的风险。
ExpectedConditions
在.NET bin中被弃用dings并要求现在使用
DotNetSeleniumExtras.WaitHelpers
,因此我尝试提供一个不需要添加任何其他包(如果可能)的示例。