0

我正在使用混合應用程序並在Ruby中使用Appium + Selenium Webdriver編寫測試。Selenium Webdriver(Appium) - 在混合應用程序(iOS)中切換到UIWebview

我開始測試一些文本編輯+點擊一個按鈕來打開UIWebview(到目前爲止一切正常)。問題是當UIWebview打開時 - 我無法訪問它(當我嘗試點擊一個html元素時(我正在使用Appium檢查器來查找元素並記錄我的Ruby測試),它立即關閉。我知道我有切換到一個UIWebView(因爲我發現here),但我不能讓它工作

代碼示例:

require 'rubygems' 
require 'selenium-webdriver' 
capabilities = { 
    'browserName' => 'iOS', 
    'platform' => 'Mac', 
    'version' => '7.1', 
    'device' => 'iPhone Retina (4-inch)', 
    'app' => '/Users/{my user here}/Library/Developer/Xcode/DerivedData/{path here}/Build/Products/Debug-iphonesimulator/SDK.app' 
} 

server_url = "http://127.0.0.1:4723/wd/hub" 

@wd = Selenium::WebDriver.for(:remote, :desired_capabilities => capabilities, :url => server_url) 
# ... 
# Do all kind of native actions here 
# ... 

@wd.find_element(:name, "showWebviewButton").click 
@wd.manage.timeouts.implicit_wait = 30 # seconds 
# ??? 
# How do I switch to my UIWebview here??? 
# (cannot access html elements here with @wd.find_element(:name, "htmlElement")) 
# ??? 

@wd.quit 

編輯: 使用Appium檢查我發現我的UIWebView是「窗口(1)「,所以我試過了:

@wd.switch_to.window(1) 

這給我的錯誤:

A request to switch to a different window could not be satisfied because the window could not be found 

(加載的UIWebView之前拋出的錯誤)

回答

0

終於爲我工作的唯一解決方案(使用Appium 1.2)是(從here取,寫在node.js中)

// javascript 
// assuming we have an initialized `driver` object for an app 
driver 
    .contexts().then(function (contexts) { // get list of available views. Returns array: ["NATIVE_APP","WEBVIEW_1"] 
     return driver.context(contexts[1]); // choose the webview context 
    }) 

    // do some web testing 
    .elementsByCss('.green_button').click() 

    .context('NATIVE_APP') // leave webview context 

    // do more native stuff here if we want 

    .quit() // stop webdrivage 

在上面的link中,您可以找到用其他語言編寫的解決方案。

2

看來,它加載之前您切換到的WebView。請暫停腳本一段時間,然後在出現WebView後切換到WebView。

+0

好的,tnx。這似乎是合理的,但正如你所看到的 - 我打算在打開Webview之前等待30秒。你有什麼想法我在這裏做錯了什麼? TNX! –

0

我已經嘗試了以下在Java中,併爲我工作得很好,你可能需要找到相同的紅寶石版本。

driver.switchTo().window("WEBVIEW"); 

嘗試這種

@wd.switch_to.window("WEBVIEW") // i am not sure about the syntax 
0

您需要訪問該元素如下所示

findElement(By.xpath( 「//輸入[@名稱= '的firstName']」))

相關問題