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

问题描述 投票:0回答:1

我正在使用 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("WebView") 我收到错误 # (NoMethodError) 的未定义方法“上下文”

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

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
android webview rubygems appium
1个回答
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 上的任何元素之前切换到 webview,并在尝试访问其中的元素之前切换出 webview。

Given(/^I switch out of webview$/) do
    @driver.switch_to.context(@driver.contexts.first)
end
© www.soinside.com 2019 - 2024. All rights reserved.