亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號