Restart Test in selenium if a certain exception occurs

tester1986 :

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?

Allan Lago :

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
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164558&siteId=1