java 测试加载微调硒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43001723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing loading spinner selenium
提问by sylvann
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 在它已经消失并且类别已加载时正在寻找微调器一样。
回答by bukva-ziu
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 不存在,您就可以避免花时间等待它。
回答by murali selenium
As per query, on selectCategory("Gallery"); spinner will display before Gallery category is loaded, right.
根据查询,在 selectCategory("Gallery"); 微调器将在加载图库类别之前显示,对。
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 linkhelps you to get cssvalue
此链接可帮助您获取 cssvalue
回答by Avinash Pande
To Handle Spinner in Selenium. We have to use Explicit wait-
1- WebdriverWait
2- FluientWait
在硒中处理微调器。我们必须使用显式等待
- 1- WebdriverWait
2- FluientWait
As you mention Spinner has two states 1- style.display="blockand 2-style.display="none"
style.display="block": Indicates Spinner is running.
Below code will return true if spinner is disappear else timeout exception
正如你提到的 Spinner 有两种状态1-style.display="block和2-style.display="none"
style.display="block":表示 Spinner 正在运行。
如果 Spinner 消失,下面的代码将返回 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 (e.g. By.Id, By.css, By.xpath) etc..
For Complete demo How handle Spinner in Selenium visit on : Handling Spinner in Selenium
By:传递微调器定位器(例如 By.Id、By.css、By.xpath)等。
完整演示如何在 Selenium 中处理 Spinner访问:Handling Spinner in Selenium
回答by Greg Finzer
This is what I am using in NodeJS and TypeScript to wait for all spinners. It checks the alt text and src text attributes.
这就是我在 NodeJS 和 TypeScript 中用来等待所有微调器的东西。它检查 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) {}
}
}
}