【问题标题】:Restart Test in selenium if a certain exception occurs如果发生某个异常,请在 selenium 中重新启动测试
【发布时间】:2020-01-18 10:53:23
【问题描述】:

我正在通过 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 可以工作,但我不知道如何针对特定异常执行此操作?

【问题讨论】:

    标签: java selenium testing automated-tests testng


    【解决方案1】:

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

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

    try{
       this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone8());
    } catch(Exception e){
       // Do something if any exception is thrown
    }
    

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

    对于特定的异常,你也可以指定异常,假设你已经导入了它,像这样

    try{
       this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone8());
    } catch(SessionNotCreatedException e){
       // Do something if SessionNotCreatedException is thrown
    }
    

    【讨论】:

      【解决方案2】:

      单独捕获异常

      @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-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-23
        相关资源
        最近更新 更多