Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 - Fatal编程技术网

Java 失败后继续执行,如果至少有一个失败,则整个测试失败

Java 失败后继续执行,如果至少有一个失败,则整个测试失败,java,selenium,Java,Selenium,目前,我有一个循环,总共通过16个网站URL,每次检查每个主页中的特定文本。有时会出现这样的情况:一些网站的加载时间超过了指定的时间,从而导致执行失败和停止。我希望循环继续到完成,然后如果在执行过程中至少发生了一个失败,则整个测试失败,如果没有失败,则通过整个测试 下面是用于设置和检查加载时间的实际代码。请建议如何修改下面的代码,以便我可以得到上面想要的结果 public static Boolean isTextPresentAfterWaitOnServer(final String str

目前,我有一个循环,总共通过16个网站URL,每次检查每个主页中的特定文本。有时会出现这样的情况:一些网站的加载时间超过了指定的时间,从而导致执行失败和停止。我希望循环继续到完成,然后如果在执行过程中至少发生了一个失败,则整个测试失败,如果没有失败,则通过整个测试

下面是用于设置和检查加载时间的实际代码。请建议如何修改下面的代码,以便我可以得到上面想要的结果

public static Boolean isTextPresentAfterWaitOnServer(final String strStringToAppear, RemoteWebDriver rwd, String strLoc){
        try{
              Wait<WebDriver> wait = new FluentWait<WebDriver>(rwd)
                      .withTimeout(30, TimeUnit.SECONDS)
                      .pollingEvery(2, TimeUnit.SECONDS)
                      .ignoring(NoSuchElementException.class);
              Boolean foo = wait.until(new ExpectedCondition<Boolean>() {
                  public Boolean apply(final WebDriver webDriver) {
                  return (webDriver.getPageSource().indexOf(strStringToAppear) >= 0);
                  }
             });
                return  foo;              
    }
    catch(TimeoutException e){
        throw new RuntimeException("Could not find text " + strStringToAppear +" on server "+strLoc +" - " + e);
    }
    };
公共静态布尔值isTextPresentAfterWaitOnServer(最终字符串strStringToAppear、RemoteWebDriver rwd、字符串strLoc){
试一试{
等待等待=新建FluentWait(rwd)
.带超时(30,时间单位。秒)
.轮询间隔(2,时间单位。秒)
.忽略(NoSuchElementException.class);
布尔foo=wait.until(新的ExpectedCondition(){
公共布尔应用(最终WebDriver WebDriver){
返回(webDriver.getPageSource().indexOf(strStringToAppear)>=0);
}
});
返回foo;
}
捕获(超时异常e){
抛出新的运行时异常(“在服务器“+strLoc+”-“+e”上找不到文本“+strStringToAppear+”);
}
};

我没有使用FluentWait,因此不确定是否可以像对待NoTouchElementException一样忽略超时异常。如果有,我想你也可以忽略它。或者,不要引发runtimeexception,而是创建一个errorCounter,在catch块中不断递增它,在finally块中根据它的值引发一个异常。

我认为在您的情况下simple非常有用。请尝试以下代码:

public static Boolean isTextPresentAfterWaitOnServer(final String strStringToAppear, RemoteWebDriver rwd, String strLoc){
       (...)            
    }
    catch(TimeoutException e){
        throw new RuntimeException("Could not find text " + strStringToAppear +" on server "+strLoc +" - " + e);
    }
    };
并在测试方法中捕获此RuntimeExecution:

@Test
public void someTestMethod(){
try{
  (...)
  isTextPresentAfterWaitOnServer("strStringToAppear", rwd, "strLoc");
}catch(RuntimeException re){
  Assert.fail(re.getMessage());
} //you can specify another exceptions
}

我在JUnit中使用fail,工作正常(所有测试用例都在运行)。可能在testNG中有相同的行为。

我没有使用FluentWait,因此不确定是否可以像对待NoTouchElementException一样忽略超时异常。如果有,我想你也可以忽略它。或者,不要引发runtimeexception,而是创建一个errorCounter,在catch块中不断递增它,在finally块中根据它的值引发一个异常。感谢您的响应。我更喜欢使用后者。但是我忽略了提到我正在使用testNG。我看到一个errorCollector选项,但是我不确定如何在catch语句之后包含它。不确定在errorCollector上。我会将评论作为答案发布,供您接受或投票。谢谢herry,我会尝试一下。不幸的是,它不起作用。但是,删除“抛出”和返回错误;作品现在将发生的是,对于已经通过和失败的迭代,将分别返回一系列True或False。我现在面临的问题是能否将全部16个结果(真/假)存储在一个数组中。@Ashenchowe修改了我的答案。请试一试!