我的移动仿真器测试设置出现问题。
基本上,我有一个可用于在移动设备上运行硒测试的移动设备列表。这些是设备池,可供已付费使用该服务的任何人使用,因此有时可能会使用这些设备,这将导致会话未创建异常。我遇到的问题是我正在使用尝试/捕获以确保一个设备不可用,可以使用另一组设备功能。我遇到的问题是,有时两个设备都在使用中,而测试却被忽略了。这是我当前的代码:
@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());
}
}
已使用以下代码绑定,但不允许(!done)
boolean done = false;
while (!done) {
try {
...
done = true;
} catch (...) {
}
}
我尝试过这样的循环,但是什么也没发生
@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;
}
}
我也尝试过
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 ++;
}
}
全面测试
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();
}
}
您的代码中至少有两个问题:
SessionNotCreatedException
才能重试。这里是固定的示例。如您所见,我创建了一个单独的方法来处理设备选择。当使用第一台设备时,将处理异常并使用第二台设备。如果还使用第二个设备,则将抛出SessionNotCreatedException
,并且必须从呼叫者处将其捕获。在catch块中,您可以添加一个等待时间,因为该设备可能仍会使用一段时间。
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 = 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())
}
}
...
}