Reputation: 105
Im having a issue with the setup of my mobile emulator tests.
Basically i have a list of mobile devices i can use to run my selenium tests on mobile. These are a pool of devices and can be used by anyone who has paid for the service so its possible on occasion these devices will be in use and this will create a session not created exception.The problem I'm having is that i am using a try/catch to ensure if one device is unavailable another set of device capabilities can be used. The problem i am having is that on occasion both devices are in use and the tests get ignored. This is my current code:
@BeforeClass
public void setup()throws Exception{
//Setup a mobile device on Kobiton, if this one is not available, the next one will be used
try {
this.driver = new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxyss7());
} catch (SessionNotCreatedException e) {
System.out.println("Secondary device being used");
this.driver = new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxys7());
}
}
Ive tied using the following code but the (!done) is not allowed
boolean done = false;
while (!done) {
try {
...
done = true;
} catch (...) {
}
}
Ive tried a loop like this, but nothing happens
@BeforeClass
public void setup()throws Exception{
boolean done = false;
while (!done)
try {
this.driver = new RemoteWebDriver (config.kobitonServerUrl(),
config.desiredCapabilitites_galaxyss7());
done = true;
} catch (SessionNotCreatedException e){
System.out.println("Secondary device being used");
this.driver = new RemoteWebDriver (config.kobitonServerUrl(),
config.desiredCapabilitites_galaxys7());
done = true;
}
}
Ive also tried
public class how_to_play_test {
private RemoteWebDriver driver = null;
@BeforeClass
public void setup()throws Exception{
int max_attempts = 10;
int attempts = 0;
boolean done = false;
while (attempts<max_attempts && !done) {
try {
this.driver = new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxyss7());
done = true;
} catch (SessionNotCreatedException e) {
System.out.println("Secondary device being used");
this.driver = new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxys7());
done = true;
}
attempts ++;
}
}
Full test
public class how_to_play_skip_test {
private RemoteWebDriver driver = null;
@BeforeClass
public void setup()throws Exception{
int max_attempts = 10;
int attempts = 0;
boolean done = true;
while ((max_attempts > attempts) && !done) {
try {
this.driver = new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxyss7());
done = true;
} catch (SessionNotCreatedException e) {
System.out.println("Secondary device being used");
this.driver = new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxys7());
done = true;
}
attempts ++;
}
}
@Test(priority=1)
public void how_to_play_skip_test_android() throws Exception {
driver.get("https://baseball-game-stage.com/howtoplay#howtoplay");
Thread.sleep(10000);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement howto = driver.findElement(By.xpath("/html/body/aside/div/div[2]/div/div[1]/div[1]/div/div/div[1]/div/section/h2"));
wait.until(ExpectedConditions.visibilityOf(howto));
System.out.println("How to is displayed");
String how = driver.findElement(By.xpath("/html/body/aside/div/div[2]/div/div[1]/div[1]/div/div/div[1]/div/section/p")).getText();
//String how = how_to_play_text_element.getText();
System.out.println(how);
WebElement next = driver.findElement(By.cssSelector("[data-qa-action-button]"));
next.click();
driver.findElement(By.xpath("/html/body/aside/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div/section/h2")).isDisplayed();
System.out.println("Game Picks is displayed");
String game_picks_text = driver.findElement(By.xpath("/html/body/aside/div/div[2]/div/div[1]/div[1]/div/div/div[2]/div/section/p")).getText();
System.out.println(game_picks_text);
Thread.sleep(3000);
next.click();
String submit_text = driver.findElement(By.xpath("/html/body/aside/div/div[2]/div/div[1]/div[1]/div/div/div[3]/div/section/p")).getText();
Assert.assertEquals("Complete your selections and submit your picks. Follow your progress on the big screen leaderboard.", submit_text);
System.out.println(submit_text);
WebElement finish = driver.findElement(By.cssSelector("[data-qa-action-button='finish']"));
finish.click();
Thread.sleep(3000);
}
@AfterClass
public void tear_down ()throws Exception {
driver.quit();
}
}
Upvotes: 1
Views: 1047
Reputation: 1734
There are at least two issues in your code:
SessionNotCreatedException
as well to be able to retry.Here the fixed example. As you can see I created a separate method to handle the device selection. When the first device is in use the exception will be handled and the second device will be used. In case the second device is used as well the SessionNotCreatedException
will be thrown and must be catched from the caller. In the catch block you could add a wait because the device will probably still be in use for some time.
public class how_to_play_skip_test {
private RemoteWebDriver driver = null;
private static final int MAX_ATTEMPTS = 10;
@BeforeClass
public void setup()throws Exception{
int attempts = 0;
boolean done = false;
while ((MAX_ATTEMPTS > attempts) && !done) {
try {
this.driver = getDriver(config.desiredCapabilitites_galaxyss7());
done = true;
} catch (SessionNotCreatedException e) {
System.out.println("Trying again...");
//Maybe wait here some time?
}
attempts ++;
}
}
private RemoteWebDriver getDriver() throws SessionNotCreatedException {
if(capabilities == null){
throw new IllegalArgumentException("Capabalities must not be null");
}
try {
return new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxyss7());
} catch(SessionNotCreatedException ex){
System.out.println("Secondary device being used");
return new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxys7())
}
}
...
}
If you want to use more than two devices you should think about a more dynamic way e.g Loop through a list that contains capabilities for each device.
If you get confused with a while or if condition you can try to make them simpler to understand (negate the boolean, remove the boolean, ...).
Here an example without the done
variable:
int max_attempts = 10;
int attempts = 0;
while(attempts < MAX_ATTEMPTS){
try{
//do something
attempts += MAX_ATTEMPS; //done
}catch(Exception ex){
//do something
}
attempts++;
}
Upvotes: 1