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

In android automation test, Sometimes yous involve to verify if chemical cistron is introduce or not on native  software app's enshroud earlier taking some action. Example : You wants to click on push exclusively if another chemical cistron is introduce on enshroud of software app. You volition confront this sort of province of affairs together with yous convey to detect out how to verify chemical cistron is introduce or non on page. There is non whatsoever built inwards component subdivision inwards selenium to cheque if chemical cistron is introduce or non on page of software app. So nosotros involve to find-out some move or thence which tin give the sack aid us to check if chemical cistron is introduce or non on page.

Here I convey prepared real elementary illustration which tin give the sack demonstrate yous how to cheque if chemical cistron is introduce or non on enshroud of android app when running android appium software automation test. Earlier i convey described how to verify chemical cistron is introduce or non on webpage inwards THIS POST.

App To Use
We volition role API Demos app inwards this illustration to cheque if chemical cistron is introduce or non on android software app's screen. View THIS PAGE to download API Demos app.

Aim To Achieve
On API Demos app's domicile screen, We wants to verify that elements amongst text "App" together with "Loader" are introduce or not. We volition role 2 separate @Test methods to verify each chemical cistron together with pass/fail specific exam method based on availability of chemical cistron on screen.

Verify chemical cistron introduce inwards appium test

As yous tin give the sack run into inwards to a higher house image, We volition last able to detect chemical cistron containing "App" text equally it is introduce on screen. But it volition last unable to detect chemical cistron containing "Loader" text equally it is non introduce on screen.

How To Do It
Selenium has findElements method to become all elements from page using given id or refer or xpath etc. We volition use findElementsByName equally bellow to become listing of elements from page which contains refer = App or Loader.

//There is chemical cistron amongst refer App on screen. //So iselementpresent volition last laid to true. Boolean iselementpresent = driver.findElementsByName("App").size() != 0;    //There is non whatsoever chemical cistron similar Loader on screen. //So iselementpresent volition last laid to false. Boolean iselementpresent = driver.findElementsByName("Loader").size() != 0;

It volition become the listing of elements for given refer together with thence cheque the size. It volition laid iselementpresent = true(means chemical cistron is present) if size is non equals to 0 together with laid iselementpresent = false(means chemical cistron is non present) if size is equals to 0.

Create And Run Test
Run bellow given software automation exam inwards your eclipse together with verify how it works. If yous don't know how to run android appium exam thence yous involve 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;  populace flat CheckElementPresent {  AndroidDriver driver;   @BeforeTest  populace void setUp() throws MalformedURLException {   DesiredCapabilities capabilities = novel 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 = novel AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);  }   //Check chemical cistron App is introduce or non on page.  @Test  populace void checkAppElementPresent() {   //There is chemical cistron amongst refer App on screen.   //So iselementpresent volition last laid to true.   Boolean iselementpresent = driver.findElementsByName("App").size() != 0;   //iselementpresent volition last truthful thence assertion volition croak together with thence exam method volition croak too.   Assert.assertTrue(iselementpresent,"Targeted chemical cistron App is non introduce on screen");   System.out.println("Targeted chemical cistron App is introduce on screen.");  }   //Check chemical cistron Loader is introduce or non on page.  @Test  populace void checkLoaderElementPresent() {   //There is non whatsoever chemical cistron similar Loader on screen.   //So iselementpresent volition last laid to false.   Boolean iselementpresent = driver.findElementsByName("Loader").size() != 0;   //iselementpresent volition last imitation thence assertion volition neglect together with thence exam method volition neglect too.   Assert.assertTrue(iselementpresent,"Targeted chemical cistron Loader is non introduce on screen");   System.out.println("Targeted chemical cistron Loader is introduce on screen.");  }   @AfterTest  populace void End() throws IOException {   driver.quit();    } }

When yous run to a higher house test, It volition exhibit yous resultant similar bellow inwards eclipse.
Verify chemical cistron introduce inwards android automation test
  • checkAppElementPresent() method is croak as elements amongst refer "App" is introduce on android app's screen.
  • checkLoaderElementPresent() method is neglect as elements amongst refer "Loader" is non introduce on android app's screen.
Also yous tin give the sack role whatsoever other methods like findElementsByXPath, findElementsByAccessibilityId, findElementsByClassName, etc to become the listing of elements using given criteria.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post