简体   繁体   English

测试加载微调硒

[英]Testing loading spinner selenium

I'm trying to automate a test case about a loading spinner which is displayed when navigating between the different page categories while the page is loading.我正在尝试自动化一个关于加载微调器的测试用例,当页面加载时在不同的页面类别之间导航时会显示该测试用例。

The spinner has two states: When is not visible - "display: none;"微调器有两种状态: 何时不可见 - “显示:无;”

Visible state while the page is loading - "display: block;"页面加载时的可见状态 - "display: block;"

The problem is when navigate between the different categories I'm using a method问题是当我使用一种方法在不同类别之间导航时

selectCategory("Gallery");选择类别(“画廊”);

and then not sure how to assert that the spinner is visible while the page is being load.然后不确定如何在加载页面时断言微调器可见。 It's like webdriver is looking for the spinner when it's already gone and the category is load.就像 webdriver 在它已经消失并且类别已加载时正在寻找微调器一样。

If I understand the question correctly, sometimes spinner is shown and sometimes it's not, and in that case you need to wait for it to be gone.如果我正确理解了这个问题,有时会显示微调器,有时则不会,在这种情况下,您需要等待它消失。 I have similar situation with webpages I'm writing tests for, so I wrote some kind of solution for that.我正在为其编写测试的网页遇到类似的情况,因此我为此编写了某种解决方案。

First, I have a helper function isElementDisplayed which wraps isDisplayed method and does some exception handling so it only returns true of false for convenience:首先,我有一个辅助函数 isElementDisplayed,它封装了 isDisplayed 方法并进行了一些异常处理,因此为了方便起见,它只返回 true 或 false:

public static boolean isElementDisplayed(WebElement element) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, 1);
        wait.until(ExpectedConditions.visibilityOf(element));
        return element.isDisplayed();
    } catch (org.openqa.selenium.NoSuchElementException
            | org.openqa.selenium.StaleElementReferenceException
            | org.openqa.selenium.TimeoutException e) {
        return false;
    }
}

I inserted only 1 second for element waiting here because we know for sure that spinner will appear shortly on page and there is no point for waiting longer than that.我只为在此处等待的元素插入了 1 秒,因为我们确信微调器很快就会出现在页面上,因此等待更长的时间是没有意义的。

Second, I have a method that waits for spinner to be gone if it is detected on page:其次,如果在页面上检测到微调器,我有一种方法可以等待微调器消失:

public static void waitForElementToBeGone(WebElement element, int timeout) {
    if (isElementDisplayed(element)) {
        new WebDriverWait(driver, timeout).until(ExpectedConditions.not(ExpectedConditions.visibilityOf(element)));
    }
}

By tweaking timeout time I was able to cover all cases of lengthy spinner actions in my application.通过调整超时时间,我能够涵盖我的应用程序中冗长的微调器操作的所有情况。 Also it allows you to speed up tests execution because you avoid spending time waiting for spinner if it's not there.此外,它还允许您加快测试执行速度,因为如果 Spinner 不存在,您就可以避免花时间等待它。

As per query, on selectCategory("Gallery");根据查询,在 selectCategory("Gallery"); spinner will display before Gallery category is loaded, right.微调器将在加载图库类别之前显示,对。

You can try this assertion in couple of ways, say simply你可以通过几种方式尝试这个断言,简单地说

if loader has id="loader" then如果 loader 有 id="loader" 那么

Assert.assertTrue(driver.findElement(By.id("loader")).isDisplayed());

If you want to assert by attribute as mentioned query then如果您想按上述查询的属性进行断言,则

Assert.assertEquals(driver.findElement(By.id("loader")).getCssValue("display"), "block or something as per requirement");

this link helps you to get cssvalue链接可帮助您获取 cssvalue

To Handle Spinner in Selenium.在硒中处理微调器。 We have to use Explicit wait-我们必须使用显式等待
1- WebdriverWait 1- WebdriverWait
2- FluientWait 2- FluientWait

As you mention Spinner has two states 1- style.display="block and 2-style.display="none"正如你提到的 Spinner 有两种状态1-style.display="block2-style.display="none"
style.display="block" : Indicates Spinner is running. style.display="block" :表示 Spinner 正在运行。
Below code will return true if spinner is disappear else timeout exception如果微调器消失,则下面的代码将返回 true 否则超时异常

public static boolean waitTillSpinnerDisable(WebDriver driver, By by)
{
  FluentWait<WebDriver> fWait = new FluentWait<WebDriver>(driver);
  fWait.withTimeout(10, TimeUnit.SECONDS);
  fWait.pollingEvery(250, TimeUnit.MILLISECONDS);
  fWait.ignoring(NoSuchElementException.class);

  Function<WebDriver, Boolean> func = new Function<WebDriver, Boolean>() 
   {
     @Override
     public Boolean apply(WebDriver driver) {
    WebElement element = driver.findElement(by);
    System.out.println(element.getCssValue("display"));         
    if(element.getCssValue("display").equalsIgnoreCase("none")){
    return true;
       }
        return false;
     }
   };

   return fWait.until(func);
}

By : pass the spinner locator (eg By.Id, By.css, By.xpath) etc.. By :传递微调器定位器(例如 By.Id、By.css、By.xpath)等。
For Complete demo How handle Spinner in Selenium visit on : Handling Spinner in Selenium对于完整的演示如何在 Selenium 中处理 Spinner 访问: 在 Selenium 中处理 Spinner

This is what I am using in NodeJS and TypeScript to wait for all spinners.这就是我在 NodeJS 和 TypeScript 中用来等待所有微调器的东西。 It checks the alt text and src text attributes.它检查 alt 文本和 src 文本属性。

public async WaitForAllSpinners(): Promise<void> {
    const elements = await this.driver.findElements(By.css("img"));
    const keywords = ["loading", "loader", "spinner"];

    for (const element of elements) {
        let altText = "";
        let srcText = "";

        //Element may be stale by the time we check it
        try {
            altText = await element.getAttribute("alt");
        } catch (error) {}

        //Element may be stale by the time we check it
        try {
            srcText = await element.getAttribute("src");
        } catch (error) {}

        let identifiedByAltText = false;
        let identifiedBySrcText = false;

        for (const keyword of keywords) {
            if (altText && altText.toLowerCase().indexOf(keyword) >= 0) {
                identifiedByAltText = true;
                break;
            }
            if (srcText && srcText.toLocaleLowerCase().indexOf(keyword) >= 0) {
                identifiedBySrcText = true;
                break;
            }
        }

        if (identifiedByAltText || identifiedBySrcText) {
            //Element may be stale by the time we wait for it
            try {
                await this.driver.wait(until.elementIsNotVisible(element), this._testConfig.DefaultElementTimeout);
            } catch (error) {}
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM