tester1986
tester1986

Reputation: 105

Restart Test in selenium if a certain exception occurs

I am running my selenium mobile tests through kobiton and an issue i keep finding is that as im using a public phones, they may be in use when i try to run the tests Im getting the following message

org.openqa.selenium.SessionNotCreatedException: No device matching the desired capabilities

My current code setup is

@BeforeClass
public void setup()throws Exception{

    String kobitonServerUrl = "https://f:a15e3b93-a1dd3c-4736-bdfb- 
[email protected]/wd/hub";

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

}

Id like to be able to be able to try

    this.driver = new RemoteWebDriver (config.kobitonServerUrl(), config.desireCapabilitites_iphone9() )

if the iphone 8 is unavailable, so i thought a if and else could work but i dont know how to do this for a specific exception?

Upvotes: 3

Views: 395

Answers (2)

Allan Lago
Allan Lago

Reputation: 309

If I understand your question correctly, you want something that is analogous to if-else but for exceptions,

In general the 'if-else' of exceptions is 'try-catch'. That is, the following code snippet

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

would execute what's inside the try and if any exception is thrown (within the try) would execute the code within catch.

For a specific exception, you may also specify the exception, given you already imported it, like this

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

Upvotes: 1

user1207289
user1207289

Reputation: 3253

Catch exception separately

@BeforeClass
public void setup()throws Exception{

   try {
    String kobitonServerUrl = "https://f:a15e3b93-a1dd3c-4736-bdfb- 
[email protected]/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
    }

 }

}

Upvotes: 0

Related Questions