Hybrid App Automation or Mobile Web Automation.

Let us see the procedure for automation of mobile web. Here we are automating the mobile web using a chrome browser on an Android device.

Make sure that the Mobile Browser version and Browser Driver should be in the same version.

If we are automating the mobile web using chrome browser then chrome browser version and chrome driver version should be the same.If not it will show error while executing error.

Two Ways of Identifying locator values on webview.


1st-way :

Launch the url in the desktop browser and inspect the values using chrome Dev tools as shown in the link.


2nd-way :


1. Inspecting the values from the mobile browser itself.Follow the below steps for 2nd way.

2. Connect the device to PC. Now open chrome browser and type "chrome://inspect/#devices"

3. Make sure the required url is launched before providing the above command in chrome.

4. Now this will show a list of devices connected along with a list of urls which are opened in the browser.


Click on Inspect

5. Click on the inspect for required url as shown in the image.

6. This will open a new window and here we can inspect the elements.


Inspect Element
Inspect Element

Below are the steps which are used to automate the hybrid app.

  • Create the Driver object by using the mobile browser app package and activity etc.
  • Create WebDriverWait object
  • Type the URL in address bar and press enter to launch the URL
  • Get the list of Contexts in App
  • Switch to webview from Native view to perform action on Required URL
  • Do the testing on a Webview URL in chrome browser or any.
  • Switch back to native view to perform action on native app
  • Do the testing on native app screen if we want
  • Quit the driver object

HybridAppAutomation.py

from appium import webdriver
import time
from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException, NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.appiumby import AppiumBy

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['automationName'] = 'UiAutomator2'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Pixel'
desired_caps['app'] = ('/Skill2Lead/Appium_Demo_App/Android/Android_Appium_Demo.apk')
desired_caps['appPackage'] = 'com.android.chrome'
desired_caps['appActivity'] = 'org.chromium.chrome.browser.ChromeTabbedActivity'


# 1. Create the Driver object
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# 2. Create WebDriverWait
wait = WebDriverWait(driver,25,poll_frequency=1,ignored_exceptions=[ElementNotVisibleException,ElementNotSelectableException,NoSuchElementException])


# 3. Open the URL in Browser
ele = wait.until(lambda  x: x.find_element(AppiumBy.ID,"com.android.chrome:id/terms_accept"))
ele.click()

ele2 = wait.until(lambda  x: x.find_element(AppiumBy.ID,"com.android.chrome:id/negative_button"))
ele2.click()

ele3 = wait.until(lambda  x: x.find_element(AppiumBy.ID,"com.android.chrome:id/search_box_text"))
ele3.click()

ele4 = wait.until(lambda  x: x.find_element(AppiumBy.ID,"com.android.chrome:id/url_bar"))
ele4.click()
ele4.send_keys("https://www.google.com/")

driver.press_keycode(66)

time.sleep(5)

# 4. Get the list of Contexts in App
appContexts = driver.contexts
print("AppContexts : ", appContexts)

# 5. switch to webview to perform action on Required URL or on WebView
for appType in appContexts:
    if appType == "WEBVIEW_chrome":
        driver.switch_to.context(appType)


# 6. Do testing on Webview screen in chrome browser or any if we want
ele4 = wait.until(lambda  x: x.find_element(AppiumBy.XPATH,"//*[@name='q']"))
ele4.send_keys("Skill2Lead")

# 7. Switch back to native view to perform action
for appType in appContexts:
    if appType == "NATIVE_APP":
        driver.switch_to.context(appType)

# 8. Do testing on native app screen if we want

ele4 = wait.until(lambda  x: x.find_element(AppiumBy.ID,"com.android.chrome:id/url_bar"))
ele4.click()
ele4.send_keys("https://www.skill2lead.com/")
driver.press_keycode(66)

# 9. Quit or close the driver object

time.sleep(5)
driver.quit()