2016-03-01 26 views
0

我正在測試具有硒,appium和RubyGems的Android混合應用程序。當我嘗試點擊圖片使用如何使用Appium和RubyGems從NativeApp切換到WebView

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)

任何想法,爲什麼我會收到上下文錯誤?

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 
+0

我曾嘗試改變.window到.context,但我最終得到一個錯誤未定義的方法'上下文」爲#<硒:: webdriver的:: TargetLocator:0x000000050f8d40>(NoMethodError) – user3531858

回答

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 
+0

我添加的屏幕截圖該應用程序,因爲我再次收到未定義的方法「上下文」錯誤。我肯定錯過了什麼。 – user3531858

+0

http://www.rubydoc.info/gems/selenium-webdriver/0.0.28/Selenium/WebDriver/TargetLocator不理解這有助於嗎? – nullpointer

+0

我終於有時間回顧你的評論,我一定不會明白。我在上面添加了我的代碼,希望你能幫助我。一切正常,直到我到達頁面的底部。有什麼我需要聲明,或我缺少的其他代碼行?謝謝。 – user3531858

相關問題