首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在selenium中循环一个try和catch语句

在selenium中循环一个try和catch语句
EN

Stack Overflow用户
提问于 2019-10-04 12:56:59
回答 1查看 451关注 0票数 1

我的移动仿真器测试的设置有问题。

基本上,我有一个移动设备列表,我可以使用它在手机上运行我的selenium测试。这些设备是一个设备池,任何为该服务付费的人都可以使用这些设备,因此这些设备有时可能正在使用,这将创建一个会话,而不是创建的exception.The问题是,我正在使用try/catch来确保如果一个设备不可用,则可以使用另一组设备功能。我遇到的问题是,有时这两种设备都在使用,而测试却被忽略了。这是我目前的代码:

代码语言:javascript
复制
@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());
        }

}

我使用以下代码进行绑定,但不允许(!完成)

代码语言:javascript
复制
boolean done = false;
while (!done) {
try {
    ...
    done = true;
} catch (...) {
}
}

我试过这样的循环,但什么也没发生

代码语言:javascript
复制
    @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;

    }

}

我也试过

代码语言:javascript
复制
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 ++;
    }

}

全试验

代码语言:javascript
复制
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();

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-08 11:38:05

在您的代码中至少有两个问题:

  1. 您的if条件永远不会满足,因为您设置了done = true。
  2. 您还需要捕获第二个SessionNotCreatedException才能重试。

代码

这是一个固定的例子。如您所见,我创建了一个单独的方法来处理设备选择。当第一设备正在使用时,异常将被处理,第二设备将被使用。如果第二个设备也被使用,SessionNotCreatedException将被抛出,并且必须从调用方捕获。在catch块中,您可以添加等待,因为该设备可能仍在使用一段时间。

代码语言:javascript
复制
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变量的示例:

代码语言:javascript
复制
int max_attempts = 10;
int attempts = 0;

while(attempts < MAX_ATTEMPTS){
  try{
     //do something
     attempts += MAX_ATTEMPS; //done
  }catch(Exception ex){
     //do something
  }
  attempts++;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58236915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档