簡體   English   中英

if-else語句不適用於Appium Java代碼

[英]if-else statements are not working properly for Appium Java code

我已經用if-else語句編寫了Java代碼,並且用戶將通過4條替代路線中的任何一條從“ Screen1”移到“ Screen2”,並且我為所有附加圖像 在此處輸入圖片說明 可能的應用程序流程由開發人員編寫的代碼隨時隨地決定。 只是增加使用的工具是Appium。

    driver.findElement(By.id("----")).click(); //this click will take from  'screen1' to next screen.
          if(driver.findElement(By.id("com.abc.rbanking:id/WhatsNew")).isDisplayed())
    { //case if screen A is displayed just after screen 1
        MobileElement cross = driver.findElement(By.xpath("//*[@class = 'android.widget.ImageView']"));
        cross.click();

        Thread.sleep(3000);

    }

    if(driver.findElement(By.xpath("//android.widget.TextView[@resource-id='com.abc.rbanking:id/text_logo'][text()='Security Question']")).isDisplayed())
    { //case when screen B is displayed Just after screen 1

    MobileElement mfaQ = driver.findElement(By.id("com.abc.rbanking:id/MfaQuestionText"));
    String question = mfaQ.getText();

    String lastword = question.replaceAll("^.*?(\\w+)\\W*$", "$1");

    System.out.println(lastword);

    MobileElement answer = driver.findElement(By.id("com.abc.rbanking:id/MfaAnswerTextBox"));

    answer.sendKeys(lastword);

    MobileElement checkbox = driver.findElement(By.id("com.abc.rbanking:id/ShowChallengeAnswerCheckbox"));
    checkbox.click();

    Thread.sleep(3000);

    MobileElement nextb = driver.findElement(By.id("com.abc.rbanking:id/PrimaryButton"));
    nextb.click();

    Thread.sleep(8000);
    }

    if(driver.findElement(By.id("com.abc.rbanking:id/WhatsNew")).isDisplayed())
    { //case when screen A is displayed after screen B
        MobileElement cross = driver.findElement(By.xpath("//*[@class = 'android.widget.ImageView']"));
        cross.click();
        Thread.sleep(3000);
    }


         driver.findElement(By.id("----")); //this is code for 'Screen 2'

發生的情況是在腳本執行期間,首先檢查“ If”,然后跳過所有代碼。 我無法解決問題。 請幫忙。

在這里聽起來好像(實際上不是雙關語)您實際上不需要 if else構造。 因此,嘗試僅使用單獨的if語句:

// see if element on 'screen A' is displayed
if (driver.findElement(By.id("abc")).isDisplayed()) {
    //execute a few statements

}

// see if element on 'Screen B' is displayed
if (driver.findElement(By.id("xyz")).isDisplayed()) {

   // execute a few statements
}

// see if element on 'screen A' is displayed
if (driver.findElement(By.id("abc")).isDisplayed()) {

}
driver.findElement(By.id("----")); //this is code for 'Screen 2'

你的描述,即與第一行為if被擊中,沒有別的執行,恰恰是你的代碼應該如何表現。 如果您打算允許每個代碼塊都可以執行,那么我上面給出的是一個選擇。

最后,我能夠找到該問題的解決方案,並且工作正常。 我將if語句放在try-catch塊中,如下面的代碼所示,它對於應用程序對最終用戶提出的每個替代方案都非常適用。

try {
            if (driver.findElement(By.xpath("//android.widget.TextView[@resource-id='com.abc.rbanking:id/text_logo'][text()='Security Question']")).isDisplayed()) { //case when screen B is displayed Just after screen 1
                MobileElement mfaQ = driver.findElement(By.id("com.abc.rbanking:id/MfaQuestionText"));
                String question = mfaQ.getText();
                String lastword = question.replaceAll("^.*?(\\w+)\\W*$", "$1");
                System.out.println(lastword);
                MobileElement answer = driver.findElement(By.id("com.abc.rbanking:id/MfaAnswerTextBox"));
                answer.sendKeys(lastword);
                MobileElement checkbox = driver.findElement(By.id("com.abc.rbanking:id/ShowChallengeAnswerCheckbox"));
                checkbox.click();
                Thread.sleep(3000);
                MobileElement nextb = driver.findElement(By.id("com.abc.rbanking:id/PrimaryButton"));
                nextb.click();
                Thread.sleep(8000);
            }
        } catch (
                Exception e) {
            e.printStackTrace();
        }
        try

        {
            if (driver.findElement(By.id("com.abc.rbanking:id/WhatsNew")).isDisplayed()) { //case when screen A is displayed after screen B
                MobileElement cross = driver.findElement(By.xpath("//*[@class = 'android.widget.ImageView']"));
                cross.click();
                Thread.sleep(3000);
            }
        } catch (
                Exception e)

        {
            e.printStackTrace();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM