Appium: Handling the case when users get different main/landing pages | With working code

Anurut Malhan
noobQA
Published in
3 min readJul 4, 2019

While writing my first blog post here at Medium, I picked up Make My Trip(India) android application as an example for OTP verification. And it caught me on the wrong foot as it has four different Main pages which appear randomly. Here’s the story about how I handled it.

Disclaimer: I’m not a pro at Appium or java, my approach might not be the best approach. Reader discretion is advised.

Step 1: Google it. Isn’t it obvious?

And I couldn’t find a good enough solution and I had to stop procrastinating and get to work.

First I gathered one unique element locator from each possible Main page(in the same sequence as in the header image).

//element locators for the four main pages on the MakeMyTrip app
page one id=com.makemytrip:id/inputs_content
page two id=com.makemytrip:id/btn_loginpage three id=com.makemytrip:id/bb_homepage four id=com.makemytrip:id/loginButton

Now what we want is to see if any of these locators is available that identifies the screen and then we can handle it accordingly.

For that I created a method called whichScreen()

public String whichScreen() {
try {
if(driver.findElement(By.id("com.makemytrip:id/inputs_content")).isDisplayed()){System.out.println();System.out.println("Found Screen 1");return "1";}} catch (NoSuchElementException e) {System.out.println();System.out.println("Screen 1 not displayed " + e);}
try {
if(driver.findElement(By.id("com.makemytrip:id/btn_login")).isDisplayed()){System.out.println();System.out.println("Found Screen 2");return "2";}} catch (NoSuchElementException e) {System.out.println();System.out.println("Screen 2 not displayed " + e);}try {if(driver.findElement(By.id("com.makemytrip:id/bb_home")).isDisplayed()){System.out.println();System.out.println("Found Screen 3");return "3";}} catch (NoSuchElementException e) {System.out.println();System.out.println("Screen 3 not displayed " + e);}try {if(driver.findElement(By.id("com.makemytrip:id/loginButton")).isDisplayed()){System.out.println();System.out.println("Found Screen 4");return "4";}} catch (NoSuchElementException e) {System.out.println();System.out.println("Screen 4 not displayed " + e);}return "";}

The method above returns a value when it finds one of the locators and an empty string if none of the locators is found. We can use this value in a switch statement to do operations relating to the specific screen present.

public void handleMultipleMainScreen() {switch (whichScreen()) {case "1":AndroidElement enterMobile = driver.findElement(By.id("com.makemytrip:id/inputs_content"));if (enterMobile.isDisplayed()) {System.out.println("");System.out.println("--------- Going to Enter Mobile Number ---------");enterMobile.click();break;case "2":driver.findElement(By.id("com.makemytrip:id/btn_login")).click();break;case "3":AndroidElement mmtButton = driver.findElement(By.id("com.makemytrip:id/my_profile_icon"));mmtButton.click();List<AndroidElement> loginButton = driver.findElements(By.className("android.widget.TextView"));AndroidElement login = getListElement("LOGIN", loginButton);login.click();break;case "4":driver.findElement(By.id("com.makemytrip:id/loginButton")).click();break;default:System.out.println("None of the expected screens were displayed");break;}}

An another method getListElement is also used to search for a particular element in a List

public AndroidElement getListElement(String elementName, List<AndroidElement> listElement){for(int i=0; i<listElement.size();i++) {if (listElement.get(i).getText().contains(elementName)) {System.out.println("*Got* " + listElement.get(i).getText());return listElement.get(i);}}return null;}

The above method receives a string number and performs actions accordingly. The aim of the actions written here is to take the user to the Login/Registration screen. For the complete process you can have a look at my GitHub project.

Want to know how to Sign In with Phone number and OTP in Appium? I have a blog on it here.

--

--