Search code examples
appiumkarateappium-android

How to switch driver context from Android to webview in karate


I'm working on a project to automate a mobile application using Karate and appium. The application that I'm working upon seems to be a hybrid one. When I launch the app it redirects me to a web URL to sign-in(web browser) and my karate tests are not able to locate elements present under the sign-in page.

Feature: android test

  Background: App Preset

    * configure driver = { type: 'android', webDriverUrl : 'xxxxx', start: false, httpConfig : { readTimeout: 120000 } }

  Scenario: android mobile app UI tests
  Given driver { webDriverSession: { desiredCapabilities : "#(android.desiredConfig)"} }
  And driver.click('//android.widget.Button[@text="Get Started"]')
  # Sign details
  And click('#signInName')
  And input('#signInName', 'someone@gmail.com')
  And input('#password', '123456')

Solution

  • karate by default keeps you in the native app context. you can check all the context available by invoking a mobile command,

    def contexts = driver.script("mobile: getContexts")
    

    or

    json contexts = driver.http.path("contexts").get()
    

    and print contexts

    you should be able to switch context to web-view as below (instead of WEBVIEW_1 use your respective web-view which you got from the previous step),

    driver.setContext("WEBVIEW_1")
    

    and then switch back to native app by

    driver.setContext("NATIVE_APP")
    

    try with different locator strategies if facing any issue