Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试加载纺纱机_Java_Selenium Webdriver - Fatal编程技术网

Java 测试加载纺纱机

Java 测试加载纺纱机,java,selenium-webdriver,Java,Selenium Webdriver,我正在尝试自动化一个关于加载微调器的测试用例,该微调器在加载页面时在不同页面类别之间导航时显示 微调器有两种状态: 不可见时-“显示:无 页面加载时的可见状态-“显示:块 问题是当我使用一种方法在不同类别之间导航时 选择类别(“画廊”) 然后不确定在加载页面时如何断言微调器可见。 这就像webdriver正在寻找微调器,而微调器已经消失,并且类别已加载。根据查询,在selectCategory(“Gallery”)上;微调器将在加载库类别之前显示,对 您可以通过两种方式尝试这个断言,简单地说 如

我正在尝试自动化一个关于加载微调器的测试用例,该微调器在加载页面时在不同页面类别之间导航时显示

微调器有两种状态: 不可见时-“显示:无

页面加载时的可见状态-“显示:块

问题是当我使用一种方法在不同类别之间导航时

选择类别(“画廊”)

然后不确定在加载页面时如何断言微调器可见。
这就像webdriver正在寻找微调器,而微调器已经消失,并且类别已加载。

根据查询,在selectCategory(“Gallery”)上;微调器将在加载库类别之前显示,对

您可以通过两种方式尝试这个断言,简单地说

如果loader具有id=“loader”,则

如果您想按照前面提到的查询按属性断言,那么

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

这有助于您获得cssvalue

如果我正确理解了这个问题,有时会显示微调器,有时则不会,在这种情况下,您需要等待它消失。我写测试的网页也有类似的情况,所以我写了一些解决方案

首先,我有一个helper函数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不存在时花费时间等待它

在硒中处理纺纱机。我们必须使用显式等待-
1-WebdriverWait
2-FluientWait

正如您提到的,微调器有两种状态1-style.display=“block2-style.display=“none”
style.display=“block”:表示微调器正在运行。
如果微调器消失,下面的代码将返回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);
}
公共静态布尔waitTillSpinnerDisable(WebDriver驱动程序,By)
{
FluentWait fWait=新的FluentWait(驱动程序);
等待超时(10,时间单位秒);
fWait.pollingEvery(250,时间单位毫秒);
fWait.ignering(NoSuchElementException.class);
函数func=新函数()
{
@凌驾
公共布尔应用(WebDriver驱动程序){
WebElement=driver.findElement(by);
System.out.println(element.getCssValue(“display”);
if(element.getCssValue(“display”).equalsIgnoreCase(“none”)){
返回true;
}
返回false;
}
};
返回fWait.until(函数);
}
By:传递微调器定位器(例如By.Id、By.css、By.xpath)等…

有关如何在Selenium中处理微调器的完整演示,请访问:

这是我在NodeJS和TypeScript中使用的等待所有微调器的方法。它检查alt text和src text属性

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) {}
        }
    }
}
public异步WaitForAllSpinners():Promise{
const elements=wait this.driver.findElements(By.css(“img”));
常量关键字=[“加载”、“加载”、“旋转器”];
for(元素的常量元素){
让altText=“”;
让srcText=“”;
//当我们检查元素时,它可能已过时
试一试{
altText=await元素.getAttribute(“alt”);
}捕获(错误){}
//当我们检查元素时,它可能已过时
试一试{
srcText=await-element.getAttribute(“src”);
}捕获(错误){}
让identifiedByAltText=false;
设identifiedBySrcText=false;
for(关键字中的常量关键字){
if(altText&&altText.toLowerCase().indexOf(关键字)>=0){
identifiedByAltText=true;
打破
}
if(srcText&&srcText.toLocaleLowerCase().indexOf(关键字)>=0){
identifiedBySrcText=true;
打破
}
}
if(由AltText标识| |由RCText标识){
//元素在我们等待它时可能已过时
试一试{
等待this.driver.wait(直到.element不可见(element)、this.\u testConfig.defaultelement超时);
}捕获(错误){}
}
}
}
请阅读。请提供您尝试过的代码和执行结果,包括任何错误消息等。同时提供指向页面和/或相关HTML的链接。
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) {}
        }
    }
}