1 回答

TA贡献1770条经验 获得超3个赞
您的代码中至少存在两个问题:
你的 if 条件永远不会满足,因为你设置了 did = true。
您还需要抓住第二个
SessionNotCreatedException
才能重试。
代码
这是固定的例子。正如您所看到的,我创建了一个单独的方法来处理设备选择。当使用第一个设备时,将处理异常并使用第二个设备。如果还使用了第二个设备,则该错误SessionNotCreatedException
将被抛出,并且必须从调用者处捕获。在 catch 块中,您可以添加等待,因为设备可能仍会使用一段时间。
public class how_to_play_skip_test {
private RemoteWebDriver driver = null;
private static final int MAX_ATTEMPTS = 10;
@BeforeClass
public void setup()throws Exception{
int attempts = 0;
boolean done = false;
while ((MAX_ATTEMPTS > attempts) && !done) {
try {
this.driver = getDriver(config.desiredCapabilitites_galaxyss7());
done = true;
} catch (SessionNotCreatedException e) {
System.out.println("Trying again...");
//Maybe wait here some time?
}
attempts ++;
}
}
private RemoteWebDriver getDriver() throws SessionNotCreatedException {
if(capabilities == null){
throw new IllegalArgumentException("Capabalities must not be null");
}
try {
return new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxyss7());
} catch(SessionNotCreatedException ex){
System.out.println("Secondary device being used");
return new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxys7())
}
}
...
}
提示
如果您想使用两个以上的设备,您应该考虑一种更动态的方式,例如循环遍历包含每个设备功能的列表。
如果您对 while 或 if 条件感到困惑,您可以尝试使它们更易于理解(否定布尔值,删除布尔值,...)。
这是一个没有变量的例子done:
int max_attempts = 10;
int attempts = 0;
while(attempts < MAX_ATTEMPTS){
try{
//do something
attempts += MAX_ATTEMPS; //done
}catch(Exception ex){
//do something
}
attempts++;
}
添加回答
举报