第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在 selenium 中循環(huán) try 和 catch 語句

在 selenium 中循環(huán) try 和 catch 語句

犯罪嫌疑人X 2023-09-27 15:16:26
我的移動(dòng)模擬器測(cè)試設(shè)置有問題?;旧衔矣幸粋€(gè)移動(dòng)設(shè)備列表,可以用來在移動(dòng)設(shè)備上運(yùn)行我的硒測(cè)試。這些是一個(gè)設(shè)備池,任何支付服務(wù)費(fèi)用的人都可以使用,因此有時(shí)這些設(shè)備可能會(huì)被使用,這將創(chuàng)建一個(gè)會(huì)話未創(chuàng)建的異常。我遇到的問題是我正在使用嘗試/捕獲以確保如果一個(gè)設(shè)備不可用,則可以使用另一組設(shè)備功能。我遇到的問題是,有時(shí)兩個(gè)設(shè)備都在使用并且測(cè)試被忽略。這是我當(dāng)前的代碼:@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());        }}我已經(jīng)使用以下代碼綁定,但不允許 (!done)boolean done = false;while (!done) {try {    ...    done = true;} catch (...) {}}我嘗試過這樣的循環(huán),但沒有任何反應(yīng)    @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貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊

您的代碼中至少存在兩個(gè)問題:

  1. 你的 if 條件永遠(yuǎn)不會(huì)滿足,因?yàn)槟阍O(shè)置了 did = true。

  2. 您還需要抓住第二個(gè)SessionNotCreatedException才能重試。


代碼

這是固定的例子。正如您所看到的,我創(chuàng)建了一個(gè)單獨(dú)的方法來處理設(shè)備選擇。當(dāng)使用第一個(gè)設(shè)備時(shí),將處理異常并使用第二個(gè)設(shè)備。如果還使用了第二個(gè)設(shè)備,則該錯(cuò)誤SessionNotCreatedException將被拋出,并且必須從調(diào)用者處捕獲。在 catch 塊中,您可以添加等待,因?yàn)樵O(shè)備可能仍會(huì)使用一段時(shí)間。

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())           

        }        

    }


    ...

}

提示

如果您想使用兩個(gè)以上的設(shè)備,您應(yīng)該考慮一種更動(dòng)態(tài)的方式,例如循環(huán)遍歷包含每個(gè)設(shè)備功能的列表。


如果您對(duì) while 或 if 條件感到困惑,您可以嘗試使它們更易于理解(否定布爾值,刪除布爾值,...)。


這是一個(gè)沒有變量的例子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++;

}


查看完整回答
反對(duì) 回復(fù) 2023-09-27
  • 1 回答
  • 0 關(guān)注
  • 153 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)