首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果发生某种异常,请在selenium中重新启动测试

如果发生某种异常,请在selenium中重新启动测试
EN

Stack Overflow用户
提问于 2019-09-17 14:31:36
回答 2查看 289关注 0票数 3

我正在通过kobiton运行我的selenium移动测试,我一直发现的一个问题是,当我使用公共电话时,当我尝试运行测试时,它们可能在使用,我得到了以下消息

org.openqa.selenium.SessionNotCreatedException:没有匹配所需功能的设备

我当前的代码设置是

代码语言:javascript
复制
@BeforeClass
public void setup()throws Exception{

    String kobitonServerUrl = "https://f:a15e3b93-a1dd3c-4736-bdfb- 
006221ezz8c2a2cz@api.kobiton.com/wd/hub";

    this.driver = new RemoteWebDriver (config.kobitonServerUrl(), 
config.desireCapabilitites_iphone8());

}

我想要能够尝试

代码语言:javascript
复制
    this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone9() )

如果iphone 8不可用,所以我认为if和else可以工作,但我不知道如何处理特定的例外情况?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-17 15:04:54

如果我正确地理解了你的问题,你想要的东西类似于如果

通常情况下,异常的“如果-其他”是“尝试-捕获”。也就是说,下面的代码片段

代码语言:javascript
复制
try{
   this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone8());
} catch(Exception e){
   // Do something if any exception is thrown
}

将执行try中的内容,如果抛出任何异常(在try中),则在catch中执行代码。

对于特定的异常,您也可以指定异常,假设您已经导入了异常,如下所示

代码语言:javascript
复制
try{
   this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone8());
} catch(SessionNotCreatedException e){
   // Do something if SessionNotCreatedException is thrown
}
票数 1
EN

Stack Overflow用户

发布于 2019-09-17 15:11:48

单独捕获异常

代码语言:javascript
复制
@BeforeClass
public void setup()throws Exception{

   try {
    String kobitonServerUrl = "https://f:a15e3b93-a1dd3c-4736-bdfb- 
006221ezz8c2a2cz@api.kobiton.com/wd/hub";

    this.driver = new RemoteWebDriver (config.kobitonServerUrl(), 
config.desireCapabilitites_iphone8());
}

catch (SessionNotCreatedException e){
    this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone9() )
}

   // if you want to use if else
 catch (Exception other){
      if ( other.getMessage().contains("SessionNotCreatedException ") )
    { 
       // do something
    }

 }

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57976548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档