Automating native apps

Native apps are basically developed for a particular platform, and they can take an advantage of the device's features. They can work offline. You can install a native app directly on to the device or through an application store, such as Google Play or Apple App Store.

Native Android apps

Here, we are going to take an example of the Android calculator app, and in this section, we will take a look at the addition of two numbers.

Native Android apps

Perform the following steps to automate the calculator app:

  1. Update the desired capabilities in the setup() method to launch the calculator app:
    caps.setCapability("avd","AVD_Nexus_4");// Mention the created AVD name
    caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculator2");
    caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator");
  2. Now, we need to find the numbers; we are going to find them by name:
    WebElement five=driver.findElement(By.name("5"));
    WebElement four=driver.findElement(By.name("4"));
  3. We need to find the + sign and the = sign; we are going to find them by name and AccessabilityID, respectively:
    WebElement plus=driver.findElement(By.name("+"));
    WebElement equalTo=driver.findElementByAccessibilityId("equals"));
  4. Now, we need to perform click on the element:
    five.click();
    plus.click();
    four.click();
    equalTo.click();
  5. Run your script using TestNG; it should look like the following block of code:
    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, "Android emulator");
      caps.setCapability("avd","AVD_Nexus_4");// Mention the created AVD name
      caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.android.calculator2");
      caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.android.calculator2.Calculator");
      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 five=driver.findElement(By.name("5"));
      five.click();
      WebElement plus=driver.findElement(By.name("+"));
      plus.click();
      WebElement four=driver.findElement(By.name("4"));
      four.click();
      WebElement equalTo=driver.findElementByAccessibilityId("equals"));
      equalTo.click();
    }
    @AfterClass
    public void tearDown(){
      driver.closeApp();
    }
    }

Native iOS apps

Here, we are going to take an example of an iOS TestApp; you can get it from https://github.com/manojhans/Appium/blob/master/Application/iOS/Native/TestApp7.1.app.zip?raw=true. After downloading it, extract it to a local folder (for example, /Users/mhans/appium/ios/TestApp.app). The downloaded app will look like the following screenshot:

Native iOS apps

In this section, we will take a look at the addition of two numbers. To do this, perform the following steps:

  1. Update the desired capabilities in the setup() method to launch the TestApp:
    File app=new File("/Users/mhans/appium/ios/TestApp.app");//You can change it with your app address
    caps.setCapability(MobileCapabilityType.APP,app);
  2. Now, we have to find elements to be typed in; we are going to find them by name:
    WebElement editBox1=driver.findElement(By.name("TextField1"));
    WebElement editBox2=driver.findElement(By.name("TextField2"));
  3. Now, we need to find the compute button; we are going to find it by AccessibilityID:
    WebElement computeSumBtn=driver.findElementByAccessibilityId("Compute Sum"));
  4. Now, type a value in the first box:
    editBox1.sendKeys("10");
  5. Type a value in the second box:
    editBox2.sendKeys("20");
  6. Now, click on the Compute Sum button:
    computeSumBtn.click();
  7. Run your script using TestNG; it should look like the following code:
    public class TestAppIication {
    IOSDriver driver;
    @BeforeClass
    public void setUp() throws MalformedURLException{
    File app=new File("/Users/mhans/appium/ios/TestApp.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, "iPhone 5");
      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 editBox1=driver.findElement(By.name("TextField1"));
      editBox1.sendKeys("10");
      WebElement editBox2=driver.findElement(By.name("TextField2"));
      editBox2.sendKeys("20");
      WebElement computeSumBtn=driver.findElementByAccessibilityId("Compute Sum"));
      computeSumBtn.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
3.15.4.52