Appium 如何在空手道中将驱动程序上下文从Android切换到webview

Appium 如何在空手道中将驱动程序上下文从Android切换到webview,appium,karate,appium-android,Appium,Karate,Appium Android,我正在做一个项目,使用空手道和appium自动化移动应用程序。我正在开发的应用程序似乎是一个混合应用程序。当我启动应用程序时,它会将我重定向到要登录的web URL(web浏览器),我的空手道测试无法找到登录页面下的元素 Feature: android test Background: App Preset * configure driver = { type: 'android', webDriverUrl : 'xxxxx', start: false, httpConf

我正在做一个项目,使用空手道和appium自动化移动应用程序。我正在开发的应用程序似乎是一个混合应用程序。当我启动应用程序时,它会将我重定向到要登录的web URL(web浏览器),我的空手道测试无法找到登录页面下的元素

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')

默认情况下,空手道让您处于本机应用程序上下文中。您可以通过调用mobile命令来检查所有可用的上下文

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

打印上下文

您应该能够如下所示将上下文切换到web视图(而不是使用从上一步获得的相应web视图)

然后通过

driver.setContext("NATIVE_APP")

如果遇到任何问题,请尝试使用不同的定位策略

空手道开发-我不知道:|您是否可以启动浏览器,假设浏览器处于“调试模式”,以便它在端口9222上侦听?还有一些选择。让我请那些使用空手道+android的人在这里发表评论。我尝试了上面的方法,在它重定向到登录页面之前设置上下文,并尝试通过XPath、id和CSS选择器来定位元素,但对我来说不起作用。仅供参考,在将驱动程序设置为webview后,我还打印了驱动程序,并打印了以下内容-
com.intuit.karate.driver.appium。AndroidDriver@68002219
我怀疑即使在添加了
driver.setContext(“webview_1”)
之后,它也不能将上下文切换到webview。您是否收到任何错误?因为我正在远程设备上运行测试(MobileLabs)因此我没有收到任何错误,代码也没有中断,但我可以在日志中看到一条消息,上面写着
“message”:“使用给定的搜索参数无法在页面上找到元素。”
可能您有不同的web视图上下文,更新了我的答案以获取应用程序的所有可用web视图,以决定切换内容。建议我打印了上下文,这是我得到的结果-
[print]getContexts->{“message”:“未知移动命令\”getContexts\“。仅支持shell、scrollBackTo、viewportScreenshot、deepLink、StartLogsRoadcast、StopLogsRoadcast、acceptAlert、dismissAlert、batteryInfo、deviceInfo、changePermissions、getPermissions、PerformMeditorAction、StartScreenStreading、StopScreenStreading、getNotifications、listSms和type命令。”}
我看不到任何与
WEBVIEW\u 1相关的上下文
driver.setContext("WEBVIEW_1")
driver.setContext("NATIVE_APP")