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.
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:
In this section, we will interact with web view and fill the registration form by performing the following steps:
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");
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 }
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.
name
:WebElement firstName=driver.findElement(By.name("fname")); firstName.sendKeys("test");
name
:WebElement lastName=driver.findElement(By.name("lname")); lastName.sendKeys("test");
name
:WebElement age=driver.findElement(By.name("age")); age.sendKeys("26");
name
:WebElement username=driver.findElement(By.name("username")); username.sendKeys("appiumTester");
id
:WebElement password=driver.findElement(By.id("psw")); password.sendKeys("appium@123");
id
:WebElement registerButton=driver.findElement(By.id("register")); registerButton.click();
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(); } }
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:
In this section, we are going to see how to interact with web view, as follows:
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);
className
:WebElement editBox=driver.findElement(By.className("UIATextField")); editBox.sendKeys("www.google.com");
name
:WebElement goButton=driver.findElement(By.name("Go"));
Click on the Go button to open the URL in web view:
goButton.click();
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 }
driver.context("WEBVIEW_com.example.testapp");
You can also use this command alternatively:
driver.context((String) contextNames.toArray()[1]);
linkText
:WebElement images=driver.findElement(By.linkText("Images")); images.click();
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(); } }
18.188.38.142