SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Appium Android : Verify Element Present or Not On App's Screen

In android automation test, Sometimes you need to verify if element is present or not on native  software app's screen before taking some action. Example : You wants to click on button only if some other element is present on screen of software app. You will face this kind of situation and you have to find out how to verify element is present or not on page. There is not any built in function in selenium to check if element is present or not on page of software app. So we need to find-out some work around which can help us to check if element is present or not on page.

Here I have prepared very simple example which can demonstrate you how to check if element is present or not on screen of android app when running android appium software automation test. Earlier i have described how to verify element is present or not on webpage in THIS POST.

App To Use
We will use API Demos app in this example to check if element is present or not on android software app's screen. View THIS PAGE to download API Demos app.

Aim To Achieve
On API Demos app's home screen, We wants to verify that elements with text "App" and "Loader" are present or not. We will use two separate @Test methods to verify each element and pass/fail specific test method based on availability of element on screen.

Verify element present in appium test

As you can see in above image, We will be able to find element containing "App" text as it is present on screen. But it will be unable to find element containing "Loader" text as it is not present on screen.

How To Do It
Selenium has findElements method to get all elements from page using given id or name or xpath etc. We will use findElementsByName as bellow to get list of elements from page which contains name = App or Loader.

//There is element with name App on screen.
//So iselementpresent will be set to true.
Boolean iselementpresent = driver.findElementsByName("App").size() != 0;
  
//There is not any element like Loader on screen.
//So iselementpresent will be set to false.
Boolean iselementpresent = driver.findElementsByName("Loader").size() != 0;

It will get the list of elements for given name and then check the size. It will set iselementpresent = true(means element is present) if size is not equals to 0 and set iselementpresent = false(means element is not present) if size is equals to 0.

Create And Run Test
Run bellow given software automation test in your eclipse and verify how it works. If you don't know how to run android appium test then you need to refer appium tutorials listed on THIS PAGE.

CheckElementPresent.java
package Android;

import io.appium.java_client.android.AndroidDriver;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class CheckElementPresent {
 AndroidDriver driver;

 @BeforeTest
 public void setUp() throws MalformedURLException {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "io.appium.android.apis");
  capabilities.setCapability("appActivity", "io.appium.android.apis.ApiDemos");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 //Check element App is present or not on page.
 @Test
 public void checkAppElementPresent() {
  //There is element with name App on screen.
  //So iselementpresent will be set to true.
  Boolean iselementpresent = driver.findElementsByName("App").size() != 0;
  //iselementpresent will be true so assertion will pass and so test method will pass too.
  Assert.assertTrue(iselementpresent,"Targeted element App is not present on screen");
  System.out.println("Targeted element App is present on screen.");
 }

 //Check element Loader is present or not on page.
 @Test
 public void checkLoaderElementPresent() {
  //There is not any element like Loader on screen.
  //So iselementpresent will be set to false.
  Boolean iselementpresent = driver.findElementsByName("Loader").size() != 0;
  //iselementpresent will be false so assertion will fail and so test method will fail too.
  Assert.assertTrue(iselementpresent,"Targeted element Loader is not present on screen");
  System.out.println("Targeted element Loader is present on screen.");
 }

 @AfterTest
 public void End() throws IOException {
  driver.quit();  
 }
}

When you run above test, It will show you result like bellow in eclipse.
Verify element present in android automation test
  • checkAppElementPresent() method is pass as elements with name "App" is present on android app's screen.
  • checkLoaderElementPresent() method is fail as elements with name "Loader" is not present on android app's screen.
Also you can use any other methods like findElementsByXPath, findElementsByAccessibilityId, findElementsByClassName, etc to get the list of elements using given criteria.

4 comments:

  1. No reconoce el .size() != 0 dice que el metodo no se encuentra

    ReplyDelete
  2. Hi, Boolean iselementpresent = driver.findElementsByName("App").size() != 0;
    I got problem with this code. The method findElementsByName(string) is undefined for the type WebDriver

    ReplyDelete
  3. Hi, Boolean iselementpresent = driver.findElementsByName("App").size() != 0;
    I got problem with this code. The method findElementsByName(string) is undefined for the type WebDriver

    ReplyDelete
  4. Hi, Boolean iselementpresent = driver.findElementsByName("App").size() != 0;
    I got problem with this code. The method findElementsByName(string) is undefined for the type WebDriver

    ReplyDelete