0

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 enter image description herepossible 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.

2
  • Is the first condition true? Is an exception thrown? Commented May 31, 2018 at 13:40
  • jhamon, I have added lot of details. Can you please provide some inputs. Commented Jun 5, 2018 at 12:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Tim, for your response. I have applied your approach in my code. During run of my script, today 'Screen A' came all the time on the go, so first 'if' block executed successfully. Then user moved to 'Screen 2' but as per code we waited for 2nd if block and since 'Screen B' was not present then it didn't execute the 2nd if block. Now ideally as per my understanding and as per code control should look into 3rd 'if' block and acted according but it never happened. Would be happy to know the reason. Also since 2nd 'if' block never executed though I was getting NoSuchElementException.
@Deepak I don't know your code/logic at all, because I can't see it and you haven't shared that with us. If my answer didn't help you at all, let me know and I will delete it.
Tim, you are right but I can't show the actual code because it is client app and I am not suppose to share details. Your answer really helped me run the code but facing some hiccups that's why I was curious to know.
Tim, I have added almost complete code in my original question. Request you to have a look. Today I faced exact the same issue which I was facing before posting the question.
Tim, any insight based on analysis of actual code would be helpful. Thanks.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.