简体   繁体   English

if-else语句不适用于Appium Java代码

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

I have written a Java code with if-else statements and user would move from 'Screen1' to 'Screen2' through any of 4 alternate routes and I have attached an image for all 我已经用if-else语句编写了Java代码,并且用户将通过4条替代路线中的任何一条从“ Screen1”移到“ Screen2”,并且我为所有附加图像 在此处输入图片说明 possible application flows which are decided on the go by of course code written by developer. 可能的应用程序流程由开发人员编写的代码随时随地决定。 Just to add tool used is Appium. 只是增加使用的工具是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'

What happens is during execution of script, first 'If' is checked and rest all code skipped. 发生的情况是在脚本执行期间,首先检查“ If”,然后跳过所有代码。 I am not able to figure out the issue. 我无法解决问题。 Please help. 请帮忙。

It sounds here as if (no pun intended) you actually don't want an if else construct. 在这里听起来好像(实际上不是双关语)您实际上不需要 if else构造。 So try just using separate if statements: 因此,尝试仅使用单独的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'

The behavior your describe, namely with the first if being hit and nothing else executing, is precisely how your code should behave. 你的描述,即与第一行为if被击中,没有别的执行,恰恰是你的代码应该如何表现。 If you intend to allow for each block of code to possibly execute, then what I gave above is one option. 如果您打算允许每个代码块都可以执行,那么我上面给出的是一个选择。

Finally I am able to find a solution for the problem and it is working fine. 最后,我能够找到该问题的解决方案,并且工作正常。 I put if statement in try-catch block like in below code and it works perfectly fine for every alternative posed by application to the end user. 我将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