Home » appium-tutorial » Automating Hybrid Apps with Appium

Newly Updated Posts

Automating Hybrid Apps with Appium

Previous Topic, In the tutorial we are going to learn Automating Hybrid Apps with Appium. Before going further lets understand with is Hybrid Apps.

What are Hybrid App?

Hybrid apps are the apps that are coded in same technology as web application and mobile web application and pushed in a native container on mobile device. Its basically a sandwich approach web technology and native app.

In automation testing point of view, while automation the hybrid app, we come up with two approaches, native app and web view. When ever we have to switch from native to web platform. We need to change the context and continue with scripting or execution.

How to change context from Native to Web view in Hybrid app?

During the automation creation or execution, whenever there is a need to change the context, it can be performed getContextHandles() method, within the driver instance which returns a Set contains all the context within the hybrid app.

If we want to switch to any context, we can iterate and get in to webview and perform the action that is done with in web application.

Automating Hybrid Apps with Appium

Automating Hybrid Apps with Appium

To Automate the Hybrid apps in mobile application, download app “WebView Test” from playstore and consider the scenario below:

  • Launch the hybrid application “WebView Test” on your device.
  • Click to website icon on the top right.
  • Enter the website name “facebook.com”
  • Input the User name in the user text box
  • Enter the password under password textbox.
  • Close the application.

Code for the above scenario.

public static DesiredCapabilities capabilities=null;
public AndroidDriver<AndroidElement> adriver=null;

File app =new File(System.getProperty("user.dir")+"\\apks\\WEBVIEWTEST.apk");
capabilities =new DesiredCapabilities();
capabilities.setCapability("deviceName","Redmi");			//capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
capabilities.setCapability("platformVersion","9 PKQ1.181203.001");			capabilities.setCapability("platformName","Android");			capabilities.setCapability("chromedriverExecutable","C:\\Users\\XXXXX\\Desktop\\Eclipse_Installer\\GeckoDriver\\chromedriver.exe");
capabilities.setCapability("fullReset","false");						capabilities.setCapability("appium:chromeOptions",ImmutableMap.of("w3c",false));	
capabilities.setCapability("app", app.getAbsolutePath());
try {
System.out.println("Opening Hybrid Application");
adriver = new AndroidDriver<AndroidElement>(new URL(""), capabilities); 
AndroidElement browsrIcon =adriver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.snc.test.webview2:id/action_go_website\")"));
AndroidElement inputUrl=adriver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"com.snc.test.webview2:id/input_url\")"));	
AndroidElement goButton=adriver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().resourceId(\"android:id/button1\")"));				
Set<String> contextNames =adriver.getContextHandles();				
Iterator<String> itr=contextNames.iterator();			
adriver.context((String) contextNames.toArray()[1]); 				adriver.findElement(By.id("email")).sendKeys("code2test.com");				adriver.findElement(By.id("pass")).sendKeys("TestingSite");
} catch (Exception e) {

Code Description of above code:

Step 1) Add the below capabilities on the Appium Automation script:

//Create the object of Desired Capability
capabilities =new DesiredCapabilities();
//Define the Device name
//Define the platform version
capabilities.setCapability(“platformVersion”,”9 PKQ1.181203.001″);
//Define Platform name
//provide the chrome driver exe path saved in local system with same version as the browser version in mobile device
capabilities.setCapability(“chromedriverExecutable”,”C:\Users\Girvar Singh Negi\Desktop\Eclipse_Installer\GeckoDriver\chromedriver.exe”);
//Define Browser name
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, “Chrome”);
//Define the string for chrome option

Step 2) Create the object the android Driver passing host, port number along with capabilities as a parameter

adriver = new AndroidDriver(new URL(“”), capabilities)

Step 3) Inspect the element of browserIcon on the top right and click to the element using AndroiduiAutomator.

AndroidElement browsrIcon =adriver.findElement(MobileBy.AndroidUIAutomator(“new UiSelector().resourceId(\”com.snc.test.webview2:id/action_go_website\”)”));browsrIcon.click();

Step 4) Now inspect the text box under browsericon window and enter the website name as “facebook.com” using sendKeys method

AndroidElement inputUrl=adriver.findElement(MobileBy.AndroidUIAutomator(“new UiSelector().resourceId(\”com.snc.test.webview2:id/input_url\”)”)); inputUrl.sendKeys(“facebook.com”);

Step 5) Now call the method getContextHandles() from driver instance which returns Set, capture the set in an instance variable contextNames.

Set<String> contextNames =adriver.getContextHandles();

Step 6) Iterate the set using Iterator and move to next element by next() using while loop.

Iterator itr=contextNames.iterator();
while(itr.hasNext()){ System.out.println(itr.next());

Step 7) Convert the contextNames in to Array and move get the 1 st index from context Names(which is web view)

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

Step 8) Inspect the email text box and password text box and insert the value.

  • adriver.findElement(By.id(“email”)).sendKeys(“code2test.com”); adriver.findElement(By.id(“pass”)).sendKeys(“TestingSite”);

Step 9) Close the browser.