使用Appium和RubyGems时如何从NativeApp切换到WebView

时间:2016-03-01 01:31:33

标签: android webview rubygems appium

我正在使用selenium,appium和RubyGems测试Android混合应用程序。当我尝试使用

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

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

driver.switch_to.window("WEBVIEW")

我收到错误说" ...尚未实施..."

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

...加 当我尝试     driver.switch_to.context(" web视图&#34) 我收到错误 未定义的方法`context' #(NoMethodError)

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

enter image description here

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

1 个答案:

答案 0 :(得分:0)

而不是你的阻止:

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