Native apps are basically developed for a particular platform and can take advantage of the device's features. One important point to note is that these apps can work even when you are offline. You can install them directly onto the device or through application stores, such as Google Play or the Apple App Store.
In the case of Android automation, the same code that was used in the previous chapter will work when testing with an emulator, but we need to remove the avd
capabilities from the emulator automation code.
Here, we are going to take an example of the Android dialer app. In this section, we will make a call with the Android dialer app, which is shown in the following screenshot:
Let's start with the following steps to automate the dialer app:
caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.dialer"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.dialer.DialtactsActivity");
AccessibityId
, as follows:WebElement dialPad= driver.findElementByAccessibilityId("dial pad"));
dialPad.click();
name
and click on them one by one:for(int n=0;n<10;n++){ driver.findElement(By.name(""+n+"")).click(); }
This loop will execute 10 times and will pass the value inside the locator from the numbers 0 to 9.
AccessibilityId
:WebElement dial= driver.findElementByAccessibilityId("dial"));
dial.click();
public class TestAppIication { AndroidDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android"); caps.setCapability(MobileCapabilityType.DEVICE_NAME,"Moto X");//I am using Moto X as Real Device caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.dialer"); caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.dialer.DailtactsActivity"); driver = new AndroidDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ WebElement dialPad=driver.findElementByAccessibilityId("dial pad"); dialPad.click(); for(int n=0;n<10;n++){ driver.findElement(By.name(""+n+"")).click(); } WebElement dial=driver.findElementByAccessibilityId("dial"); dial.click(); } @AfterClass public void tearDown(){ driver.closeApp(); } }
Here, we are going to take an example of the BMI calculator; you can get it from https://github.com/manojhans/Appium/blob/master/Application/iOS/Real_Device/Native/BMICalc.zip?raw=true.
You can also download the source code from https://github.com/soroushjp/BMICalc_iOS/archive/master.zip and build it for a real device. You can use the same steps to build the BMICalc that we used for the SafariLaucher app earlier in this chapter. Initially, we will get the BMI calculator as shown in following screenshot:
In this section, we will calculate the body mass index as per the height and weight. To do this, we need to perform the following steps:
setup()
method to launch the BMICalc:File app=new File("/Users/mhans/appium/ios/BmiCalc.app");//You can change it with your app address caps.setCapability(MobileCapabilityType.APP,app);//To set the app path
Xpath
:WebElement height=driver.findElement(By.xpath("(//UIATextField)[2]")); WebElement weight=driver.findElement(By.xpath("(//UIATextField)[4]"));
calculate
button; we are going to find it by name
:WebElement calculateBMI=driver.findElement(By.name("Calculate BMI"));
height.sendKeys("1.82");
weight.sendKeys("75");
calculateBMI.click();
public class TestAppIication { IOSDriver driver; @BeforeClass public void setUp() throws MalformedURLException{ File app=new File("/Users/mhans/appium/ios/BmiCalc.app");//You can change it with your app address DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(MobileCapabilityType.APP,app); caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "8.1"); caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS"); caps.setCapability(MobileCapabilityType.DEVICE_NAME,"iPad"); caps.setCapability("udid","Real Device Id "); driver = new IOSDriver (new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testExample(){ WebElement height=driver.findElement(By.xpath("(//UIATextField)[2]")); height.sendKeys("1.82"); WebElement weight=driver.findElement(By.xpath("(//UIATextField)[4]")); weight.sendKeys("75"); WebElement calculateBMI=driver.findElement(By.name("Calculate BMI")); calculateBMI.click(); } @AfterClass public void tearDown(){ driver.closeApp(); } }
13.58.53.238