我正在通过 kobiton 运行我的 selenium 移动测试,我一直发现的一个问题是,当我使用公用电话时,当我尝试运行测试时,它们可能正在使用中,我收到以下消息
org.openqa.selenium.SessionNotCreatedException:没有符合所需功能的设备
我当前的代码设置是
@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());
}
我希望能够尝试
this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone9() )
如果 iphone 8 不可用,所以我认为 if 和 else 可以工作,但我不知道如何针对特定异常执行此操作?
最佳答案
如果我正确理解你的问题,你想要类似于 if-else 但有异常(exception)的东西,
一般来说,异常的“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
}
关于java - 如果发生特定异常,请在 selenium 中重新启动测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57976548/