为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕森王 2023-08-04 17:41:33
我正在通过 kobiton 运行我的 selenium 移动测试,我不断发现的一个问题是,当我使用公用电话时,当我尝试运行测试时,它们可能正在使用中,我收到以下消息org.openqa.selenium.SessionNotCreatedException:没有与所需功能匹配的设备我当前的代码设置是@BeforeClasspublic 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());}我希望能够尝试    this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone9() )如果 iphone 8 不可用,所以我认为 if 和 else 可以工作,但我不知道如何针对特定异常执行此操作?
查看完整描述

2 回答

?
撒科打诨

TA贡献1934条经验 获得超2个赞

如果我正确理解你的问题,你想要的东西类似于 if-else 但有例外,


一般来说,异常的“if-else”是“try-catch”。也就是下面的代码片段


try{

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

} catch(Exception e){

   // Do something if any exception is thrown

}

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


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


try{

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

} catch(SessionNotCreatedException e){

   // Do something if SessionNotCreatedException is thrown

}


查看完整回答
反对 回复 2023-08-04
?
侃侃无极

TA贡献2051条经验 获得超10个赞

单独捕获异常


@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

    }


 }

}


查看完整回答
反对 回复 2023-08-04
  • 2 回答
  • 0 关注
  • 119 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信