在 selenium 中循环一个 try and catch 语句

Loop a try and catch statement in selenium

我的移动模拟器测试设置有问题。

基本上我有一个移动设备列表,我可以用它来 运行 我在移动设备上进行 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());
        }

}

我使用以下代码绑定,但不允许 (!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();

    }
}

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

  1. 你的 if 条件永远不会满足,因为你设置了 done = true。
  2. 您还需要抓住第二个 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())           
        }        
    }

    ...
}

提示

如果您想使用两个以上的设备,您应该考虑一种更动态的方式,例如循环遍历包含每个设备的功能的列表。

如果您对 while 或 if 条件感到困惑,您可以尝试使它们更易于理解(取反布尔值,删除布尔值,...)。

这里是一个没有 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++;
}