在 selenium 中循环 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 ++;

    }


}


犯罪嫌疑人X
浏览 126回答 1
1回答

德玛西亚99

您的代码中至少存在两个问题:你的 if 条件永远不会满足,因为你设置了 did = true。您还需要抓住第二个SessionNotCreatedException才能重试。代码这是固定的例子。正如您所看到的,我创建了一个单独的方法来处理设备选择。当使用第一个设备时,将处理异常并使用第二个设备。如果还使用了第二个设备,则该错误SessionNotCreatedException将被抛出,并且必须从调用者处捕获。在 catch 块中,您可以添加等待,因为设备可能仍会使用一段时间。public class how_to_play_skip_test {&nbsp; &nbsp; private RemoteWebDriver driver = null;&nbsp; &nbsp; private static final int MAX_ATTEMPTS = 10;&nbsp; &nbsp; @BeforeClass&nbsp; &nbsp; public void setup()throws Exception{&nbsp; &nbsp; &nbsp; &nbsp; int attempts = 0;&nbsp; &nbsp; &nbsp; &nbsp; boolean done = false;&nbsp; &nbsp; &nbsp; &nbsp; while ((MAX_ATTEMPTS > attempts) && !done) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.driver = getDriver(config.desiredCapabilitites_galaxyss7());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; done = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (SessionNotCreatedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Trying again...");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Maybe wait here some time?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; attempts ++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private RemoteWebDriver getDriver() throws SessionNotCreatedException {&nbsp; &nbsp; &nbsp; &nbsp; if(capabilities == null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Capabalities must not be null");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxyss7());&nbsp; &nbsp; &nbsp; &nbsp; } catch(SessionNotCreatedException ex){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Secondary device being used");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new RemoteWebDriver(config.kobitonServerUrl(), config.desiredCapabilitites_galaxys7())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; ...}提示如果您想使用两个以上的设备,您应该考虑一种更动态的方式,例如循环遍历包含每个设备功能的列表。如果您对 while 或 if 条件感到困惑,您可以尝试使它们更易于理解(否定布尔值,删除布尔值,...)。这是一个没有变量的例子done:int max_attempts = 10;int attempts = 0;while(attempts < MAX_ATTEMPTS){&nbsp; try{&nbsp; &nbsp; &nbsp;//do something&nbsp; &nbsp; &nbsp;attempts += MAX_ATTEMPS; //done&nbsp; }catch(Exception ex){&nbsp; &nbsp; &nbsp;//do something&nbsp; }&nbsp; attempts++;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java