简体   繁体   中英

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 在此处输入图片说明 possible application flows which are decided on the go by of course code written by developer. Just to add tool used is 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. 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. So try just using separate if statements:

// 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 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM