[英]Testing loading spinner selenium
我正在尝试自动化一个关于加载微调器的测试用例,当页面加载时在不同的页面类别之间导航时会显示该测试用例。
微调器有两种状态: 何时不可见 - “显示:无;”
页面加载时的可见状态 - "display: block;"
问题是当我使用一种方法在不同类别之间导航时
选择类别(“画廊”);
然后不确定如何在加载页面时断言微调器可见。 就像 webdriver 在它已经消失并且类别已加载时正在寻找微调器一样。
如果我正确理解了这个问题,有时会显示微调器,有时则不会,在这种情况下,您需要等待它消失。 我正在为其编写测试的网页遇到类似的情况,因此我为此编写了某种解决方案。
首先,我有一个辅助函数 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;
}
}
我只为在此处等待的元素插入了 1 秒,因为我们确信微调器很快就会出现在页面上,因此等待更长的时间是没有意义的。
其次,如果在页面上检测到微调器,我有一种方法可以等待微调器消失:
public static void waitForElementToBeGone(WebElement element, int timeout) {
if (isElementDisplayed(element)) {
new WebDriverWait(driver, timeout).until(ExpectedConditions.not(ExpectedConditions.visibilityOf(element)));
}
}
通过调整超时时间,我能够涵盖我的应用程序中冗长的微调器操作的所有情况。 此外,它还允许您加快测试执行速度,因为如果 Spinner 不存在,您就可以避免花时间等待它。
根据查询,在 selectCategory("Gallery"); 微调器将在加载图库类别之前显示,对。
你可以通过几种方式尝试这个断言,简单地说
如果 loader 有 id="loader" 那么
Assert.assertTrue(driver.findElement(By.id("loader")).isDisplayed());
如果您想按上述查询的属性进行断言,则
Assert.assertEquals(driver.findElement(By.id("loader")).getCssValue("display"), "block or something as per requirement");
此链接可帮助您获取 cssvalue
在硒中处理微调器。 我们必须使用显式等待
1- WebdriverWait
2- FluientWait
正如你提到的 Spinner 有两种状态1-style.display="block和2-style.display="none"
style.display="block" :表示 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 :传递微调器定位器(例如 By.Id、By.css、By.xpath)等。
对于完整的演示如何在 Selenium 中处理 Spinner 访问: 在 Selenium 中处理 Spinner
这就是我在 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) {}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.