Automating hybrid apps

A hybrid app is a combination of native and web apps. Similar to native apps, you can get hybrid apps through an application store. Nowadays, hybrid apps are very popular because they provide a cross-platform solution and display the content that they get from the Web rather than the contents installed on the device only.

Android hybrid apps

Here, we will take an example of the Hybridtestapp; you can get it from https://github.com/manojhans/Appium/blob/master/Application/Android/Hybrid/Hybridtestapp.zip?raw=true.

If you are working with an Android version higher than 4.4, then you have to use Selendroid (in that case, you have to set the capability as (MobileCapabilityType.AUTOMATION_NAME,"Selendroid")); otherwise, Appium has built-in support through the ChromeDriver.

While working with hybrid apps, you need to make the changes defined at https://developer.chrome.com/devtools/docs/remote-debugging#configure-webview. We have already made these changes in Hybridtestapp. Initially, we will get the Hybridtestapp as shown in following screenshot:

Android hybrid apps

In this section, we will interact with web view and fill the registration form by performing the following steps:

  1. Set the desired capabilities to launch the hybrid app:
    File app=new File("C:\Appium_test\HybridtestApp.apk");// (On window platform)
    caps.setCapability(MobileCapabilityType.APP,app);
    caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.example.hybridtestapp");
    caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.example.hybridtestapp.MainActivity");
  2. We need to switch on the web view to take action on the registration form. We can have the list of contexts using the following command:
    Set<String> contexts = driver.getContextHandles();
    for (String context : contexts) {
    System.out.println(context); //it will print the list of contexts like NATIVE_APP 
     WEBVIEW_com.example.hybridtestapp
    }
  3. Now, switch on the web view using this command:
    driver.context("WEBVIEW_com.example.hybridtestapp");

    Alternatively, you can use the following command:

    driver.context((String) contextNames.toArray()[1]);

    Make sure that all the other apps are closed before you execute the scripts.

  4. Once we are inside web view, we need to find the First Name box to type in; here, we are going to find an element by name:
    WebElement firstName=driver.findElement(By.name("fname"));
    firstName.sendKeys("test");
  5. We need to find the Last Name box to type in; here, we are going to find an element by name:
    WebElement lastName=driver.findElement(By.name("lname"));
    lastName.sendKeys("test");
  6. We need to find the Age box to type in; here, we are going to find an element by name:
    WebElement age=driver.findElement(By.name("age"));
    age.sendKeys("26");
  7. We need to find the User Name box to type in; here, we are going to find an element by name:
    WebElement username=driver.findElement(By.name("username"));
    username.sendKeys("appiumTester");
  8. We need to find the Password box to type in; here, we are going to find an element by id:
    WebElement password=driver.findElement(By.id("psw"));
    password.sendKeys("appium@123");
  9. We need to find the Register button to submit the form; we are going to find an element by id:
    WebElement registerButton=driver.findElement(By.id("register"));
    registerButton.click();
  10. Run your script using TestNG; this is how it should look:
    public class TestAppIication {
      AndroidDriver driver;
     @BeforeClass
     public void setUp() throws MalformedURLException{
      File app=new File("C:\Appium_test\HybridtestApp.apk");// 
      (On window platform)
    
      caps.setCapability(MobileCapabilityType.APP,app);
      DesiredCapabilities caps = new DesiredCapabilities();
      caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4");
      caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
      caps.setCapability(MobileCapabilityType.DEVICE_NAME, "Moto X");
      caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");//Use Selendroid in case of <4.4 android version
      caps.setCapability(MobileCapabilityType.APP_PACKAGE, "com.example.hybridtestapp");
      caps.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.example.hybridtestapp.MainActivity");
      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(){
      Set<String> contexts = driver.getContextHandles();
      for (String context : contexts) {
        System.out.println(context); //it will print NATIVE_APP 
     WEBVIEW_com.example.hybridtestapp
      }
      driver.context((String) contexts.toArray()[1]);
      WebElement firstName=driver.findElement(By.name("fname"));
      firstName.sendKeys("test");
      WebElement lastName=driver.findElement(By.name("lname"));
      lastName.sendKeys("test");
      WebElement age=driver.findElement(By.name("age"));
      age.sendKeys("26");
      WebElement username=driver.findElement(By.name("username"));
      username.sendKeys("appiumTester");
      WebElement password=driver.findElement(By.id("psw"));
      password.sendKeys("appium@123");
      WebElement registerButton=driver.findElement(By.id("register"));
      registerButton.click();
    }
    @AfterClass
     public void tearDown(){
      driver.closeApp();
    }
    }

iOS hybrid apps

Here, we are going to use the example of a WebViewApp. You can get it from https://github.com/manojhans/Appium/blob/master/Application/iOS/Real_Device/Hybrid/WebViewApp.zip?raw=true or download the source code from https://github.com/appium/sample-code/archive/master.zip and build WebViewApp for real devices.

You can perform the same steps to build WebViewApp that you took for the SafariLaucher app earlier in this chapter. Initially, we will get the WebViewApp as shown in following screenshot:

iOS hybrid apps

In this section, we are going to see how to interact with web view, as follows:

  1. Update the desired capabilities in the setup() method to launch the hybrid app:
    File app=new File("/Users/mhans/appium/ios/WebViewApp.app");//Change it with your app address
    caps.setCapability(MobileCapabilityType.APP,app);
  2. We need to find the edit box in order to type in the desired URL(www.google.com) to open; here, we are going to find an element by className:
    WebElement editBox=driver.findElement(By.className("UIATextField"));
    editBox.sendKeys("www.google.com");
  3. Now, we need to find the Go button; we are going to find an element by name:
    WebElement goButton=driver.findElement(By.name("Go"));

    Click on the Go button to open the URL in web view:

    goButton.click();
  4. Now, we need to switch on the web view to take further action; we can have the list of contexts using the following command:
    Set<String> contexts = driver.getContextHandles();
    for (String context : contexts) {
    System.out.println(context); //it will print the list of contexts like NATIVE_APP 
     WEBVIEW_1
    }
  5. Now, switch on the web view using this command:
    driver.context("WEBVIEW_com.example.testapp");

    You can also use this command alternatively:

    driver.context((String) contextNames.toArray()[1]);
  6. Now, you can interact with the Google page. Here, we are going to click on the Images tab; let's find an element by linkText:
    WebElement images=driver.findElement(By.linkText("Images"));
    images.click();
  7. Run your script using TestNG. Before running the test script, you need to start the proxy using the ios_webkit_debug_proxy -c 2e5n6f615z66e98c1d07d22ee09658130d345443:27753 –d command. Replace the UDID with the attached device and make sure that the port is set to 27753.

    Make sure that the web inspector is turned ON on the real device (Settings | Safari | Advanced) and the SafariLauncher app is installed. This is how the test script should look:

    public class TestAppIication {
     IOSDriver driver;
     @BeforeClass
      public void setUp() throws MalformedURLException{
      DesiredCapabilities caps = new DesiredCapabilities();
      File app=new File("/Users/mhans/appium/ios/WebViewApp.app");
      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 Identifier");
      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 editBox=driver.findElement(By.className("UIATextField"));
      editBox.sendKeys("https://www.google.com");
      WebElement goButton=driver.findElement(By.name("Go"));
      goButton.click();
      Set<String> contexts = driver.getContextHandles();
      for (String context : contexts) {
      System.out.println(context); //it will print NATIVE_APP 
     WEBVIEW_com.example.testapp
    }
      driver.context((String) contexts.toArray()[1]);
      WebElement images=driver.findElement(By.linkText("Images"));
      images.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
18.188.38.142