【问题标题】:How to Switch from NativeApp to WebView when using Appium and RubyGems使用 Appium 和 RubyGems 时如何从 NativeApp 切换到 WebView
【发布时间】:2016-06-13 07:00:18
【问题描述】:

我正在测试一个带有 selenium、appium 和 RubyGems 的 Android 混合应用程序。当我尝试使用

单击页面上的图像时
element = driver.find_element(:id => "image0")
element.click

我收到一条错误消息,说它找不到对象。然后我了解到我需要从 Native App 切换到 WebView。当我尝试切换到 Webview 时

driver.switch_to.window("WEBVIEW")

我收到一条错误消息“...尚未实施...”

那么如何切换到 Web 以便我可以单击 web 元素,然后使用 RubyGems 切换回 Native_App?

添加... 当我尝试 driver.switch_to.context("WebView") 我收到错误 # (NoMethodError) 的未定义方法 `context'

知道为什么我会收到上下文错误吗?

require 'rubygems'
require 'selenium-webdriver'
require 'uri'
require 'appium_lib'
require_relative 'SDK_Navigation'

mySampleApp = SampleApp.new
myNavigation = Navigation.new
myProducts = Products.new
myProductEditor = ProductEditor.new

caps = Selenium::WebDriver::Remote::Capabilities.android
caps['deviceName'] = 'fegero'
caps['platformName'] = 'Android'
caps['app'] = 'C:\Users\ScottFeger\Downloads\SampleApp_1105.apk'

driver = Selenium::WebDriver.for(
  :remote,
  :url => "http://127.0.0.1:4723/wd/hub",
  :desired_capabilities => caps)

mySampleApp.PickImagebtn(driver)
mySampleApp.SelectAlbum(driver, "All Photos")
mySampleApp.SelectImage(driver,"bob")
myNavigation.SelectParent(driver, "Home & Office")
myNavigation.SelectChild(driver, "Home Decor")
myProducts.SelectProduct(driver,"Coasters")
myProductEditor.AddPhoto(driver)


#================================================================
#WEBVIEW - Where my problem begins
#driver.execute_script 'mobile: tap', x: 150 , y: 300 // WORKS

driver.available_context
driver.switch_to.context("WebView")

#Click on an image
element = driver.find_element(:id => "image0")
element.click

【问题讨论】:

  • 我确实尝试将 .window 更改为 .context,但我最终得到一个错误 undefined method `context' for #<:webdriver::targetlocator:0x000000050f8d40> (NoMethodError)

标签: android webview rubygems appium


【解决方案1】:

而不是你的块:

driver.available_context
driver.switch_to.context("WebView")

docs 建议的正确方法可能是:

Given(/^I switch to webview$/) do
    webview = @driver.contexts.last
    @driver.switch_to.context(webview)
end

还要确保在尝试访问其上的任何元素之前切换到 webview,并在尝试访问其中的元素之前切换出它。

Given(/^I switch out of webview$/) do
    @driver.switch_to.context(@driver.contexts.first)
end

【讨论】:

  • 我添加了应用程序的屏幕截图,因为我再次收到未定义的方法“上下文”错误。我一定是错过了什么。
  • 我终于有时间审查您的 cmets 了,但我一定没有得到它。我在上面添加了我的代码,希望你能帮助我。一切正常,直到我到达页面底部。有什么我需要声明的,或者我缺少其他一些代码行吗?谢谢。
  • @user3531858 :已更新我的答案,将上下文切换到 webview 并退出它
猜你喜欢
  • 2021-01-10
  • 1970-01-01
  • 2015-09-24
  • 2020-08-17
  • 2016-03-28
  • 2014-07-11
  • 2016-11-14
  • 1970-01-01
  • 2015-09-16
相关资源
最近更新 更多