In android automation test, Sometimes you lot bespeak to verify if chemical cistron is acquaint or not on native software app's covert earlier taking some action. Example : You wants to click on push exclusively if another chemical cistron is acquaint on covert of software app. You volition facial expression upward this sort of province of affairs as well as you lot convey to detect out how to verify chemical cistron is acquaint or non on page. There is non whatever built inwards constituent inwards selenium to cheque if chemical cistron is acquaint or non on page of software app. So nosotros bespeak to find-out some function to a greater extent than or less which tin sack help us to check if chemical cistron is acquaint or non on page.
Here I convey prepared real uncomplicated illustration which tin sack demonstrate you lot how to cheque if chemical cistron is acquaint or non on covert of android app when running android appium software automation test. Earlier i convey described how to verify chemical cistron is acquaint or non on webpage inwards THIS POST.
App To Use
We volition purpose API Demos app inwards this illustration to cheque if chemical cistron is acquaint or non on android software app's screen. View THIS PAGE to download API Demos app.
Aim To Achieve
On API Demos app's abode screen, We wants to verify that elements amongst text "App" as well as "Loader" are acquaint or not. We volition purpose 2 separate @Test methods to verify each chemical cistron as well as pass/fail specific exam method based on availability of chemical cistron on screen.
As you lot tin sack run across inwards to a higher house image, We volition live on able to detect chemical cistron containing "App" text equally it is acquaint on screen. But it volition live on unable to detect chemical cistron containing "Loader" text equally it is non acquaint on screen.
How To Do It
Selenium has findElements method to give-up the ghost all elements from page using given id or cite or xpath etc. We volition use findElementsByName equally bellow to give-up the ghost listing of elements from page which contains cite = App or Loader.
//There is chemical cistron amongst cite App on screen. //So iselementpresent volition live on laid to true. Boolean iselementpresent = driver.findElementsByName("App").size() != 0; //There is non whatever chemical cistron similar Loader on screen. //So iselementpresent volition live on laid to false. Boolean iselementpresent = driver.findElementsByName("Loader").size() != 0;
It volition give-up the ghost the listing of elements for given cite as well as and hence cheque the size. It volition laid iselementpresent = true(means chemical cistron is present) if size is non equals to 0 as well as 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 as well as verify how it works. If you lot don't know how to run android appium exam as well as hence you lot bespeak 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; world cast CheckElementPresent { AndroidDriver driver; @BeforeTest world 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 acquaint or non on page. @Test world void checkAppElementPresent() { //There is chemical cistron amongst cite App on screen. //So iselementpresent volition live on laid to true. Boolean iselementpresent = driver.findElementsByName("App").size() != 0; //iselementpresent volition live on truthful hence assertion volition overstep as well as hence exam method volition overstep too. Assert.assertTrue(iselementpresent,"Targeted chemical cistron App is non acquaint on screen"); System.out.println("Targeted chemical cistron App is acquaint on screen."); } //Check chemical cistron Loader is acquaint or non on page. @Test world void checkLoaderElementPresent() { //There is non whatever chemical cistron similar Loader on screen. //So iselementpresent volition live on laid to false. Boolean iselementpresent = driver.findElementsByName("Loader").size() != 0; //iselementpresent volition live on fake hence assertion volition neglect as well as hence exam method volition neglect too. Assert.assertTrue(iselementpresent,"Targeted chemical cistron Loader is non acquaint on screen"); System.out.println("Targeted chemical cistron Loader is acquaint on screen."); } @AfterTest world void End() throws IOException { driver.quit(); } }
When you lot run to a higher house test, It volition exhibit you lot consequence similar bellow inwards eclipse.
- checkAppElementPresent() method is overstep as elements amongst cite "App" is acquaint on android app's screen.
- checkLoaderElementPresent() method is neglect as elements amongst cite "Loader" is non acquaint on android app's screen.
Also you lot tin sack purpose whatever other methods like findElementsByXPath, findElementsByAccessibilityId, findElementsByClassName, etc to give-up the ghost the listing of elements using given criteria.