为了账号安全,请及时绑定邮箱和手机立即绑定

在 selenium 中循环 try 和 catch 语句

在 selenium 中循环 try 和 catch 语句

犯罪嫌疑人X 2023-09-27 15:16:26
我的移动模拟器测试设置有问题。基本上我有一个移动设备列表,可以用来在移动设备上运行我的硒测试。这些是一个设备池,任何支付服务费用的人都可以使用,因此有时这些设备可能会被使用,这将创建一个会话未创建的异常。我遇到的问题是我正在使用尝试/捕获以确保如果一个设备不可用,则可以使用另一组设备功能。我遇到的问题是,有时两个设备都在使用并且测试被忽略。这是我当前的代码:@BeforeClasspublic 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;@BeforeClasspublic 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 ++;    }}
查看完整描述

1 回答

?
德玛西亚99

TA贡献1770条经验 获得超3个赞

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

  1. 你的 if 条件永远不会满足,因为你设置了 did = 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++;

}


查看完整回答
反对 回复 2023-09-27
  • 1 回答
  • 0 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信