我的移动仿真器测试的设置有问题。
基本上,我有一个移动设备列表,我可以使用它在手机上运行我的selenium测试。这些设备是一个设备池,任何为该服务付费的人都可以使用这些设备,因此这些设备有时可能正在使用,这将创建一个会话,而不是创建的exception.The问题是,我正在使用try/catch来确保如果一个设备不可用,则可以使用另一组设备功能。我遇到的问题是,有时这两种设备都在使用,而测试却被忽略了。这是我目前的代码:
@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());
}
}我使用以下代码进行绑定,但不允许(!完成)
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();
}
}发布于 2019-10-08 11:38:05
在您的代码中至少有两个问题:
SessionNotCreatedException才能重试。代码
这是一个固定的例子。如您所见,我创建了一个单独的方法来处理设备选择。当第一设备正在使用时,异常将被处理,第二设备将被使用。如果第二个设备也被使用,SessionNotCreatedException将被抛出,并且必须从调用方捕获。在catch块中,您可以添加等待,因为该设备可能仍在使用一段时间。
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())
}
}
...
}提示
如果您想使用两个以上的设备,您应该考虑一种更动态的方法,例如通过一个包含每个设备功能的列表来循环。
如果您混淆了一段时间或条件,您可以尝试使它们更容易理解(否定布尔值,删除布尔值,.)。
这里有一个没有done变量的示例:
int max_attempts = 10;
int attempts = 0;
while(attempts < MAX_ATTEMPTS){
try{
//do something
attempts += MAX_ATTEMPS; //done
}catch(Exception ex){
//do something
}
attempts++;
}https://stackoverflow.com/questions/58236915
复制相似问题