Automating native apps

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.

Android native apps

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:

Android native apps

Let's start with the following steps to automate the dialer app:

  1. Set the desired capabilities to launch the dialer app, as follows:
      caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.dialer");
      caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.dialer.DialtactsActivity");
  2. Now, we need to find the dial pad icon; we are going to find it by AccessibityId, as follows:
    WebElement dialPad= driver.findElementByAccessibilityId("dial pad"));
  3. Now, we need to click on the icon using:
    dialPad.click();
  4. We need to find the numbers key in order to dial. Here, we are going to put some logic to find the numbers (0 to 9) by 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.

  5. Here, we are using a wrong phone number so that the device doesn't make a call to anyone; it will end with an invalid number.
  6. Now, we need find the dial icon to make the call; we are going to find it by AccessibilityId:
    WebElement dial= driver.findElementByAccessibilityId("dial"));
  7. Now, click on the icon to make the call:
    dial.click();
  8. Run your script using TestNG; this is how it should look:
    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();
     }
    }

iOS native apps

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:

iOS native apps

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:

  1. Update the desired capabilities in the 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
  2. Now, we have to find the elements to type in the height and weight text box; we will find them by Xpath:
    WebElement height=driver.findElement(By.xpath("(//UIATextField)[2]"));
    WebElement weight=driver.findElement(By.xpath("(//UIATextField)[4]"));
  3. Now, we need to find the calculate button; we are going to find it by name:
    WebElement calculateBMI=driver.findElement(By.name("Calculate BMI"));
  4. Now type a value in the first box:
    height.sendKeys("1.82");
  5. Type a value in the second box:
    weight.sendKeys("75");
  6. Now, click on the Calculate BMI button:
    calculateBMI.click();
  7. Run your script using TestNG; this is how it should look:
    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();
     }
    }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.53.238