2015-10-26 57 views
1

我可以在Android webview中找到iframe,但我需要與它進行交互。各種頁面提示在Cucumber/Calabash的Android上,如何使用webview iframe測試?

query("webView css:'iframe'", :stringByEvaluatingJavaScriptFromString => '...') 

,但導致「沒有這樣的方法找到:stringByEvaluatingJavaScriptFromString([字符串])」,它看起來像一個iOS的功能,所以我敢肯定這是一款iOS,唯一的解決辦法:我需要Android。

我知道我可以通過在URL中打開它們來傳遞JavaScript查詢,但我只經歷了將特定代碼Chrome對象鏈接到應用程序中的東西從Android Web視圖中取出。

獲取數據的任何想法?

回答

2

iOS的迴應是正確的,您應該使用JavaScript來獲取iframe的內容。但是,您應該使用evaluate_javascript(query_string, javascript)來評估Android中的JavaScript。 e.g

evaluate_javascript("webview", "return document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('...').getBoundingClientRect();")

使用座標挖掘視圖。

如果矩形是不正確地分析,你可以做這樣的事情: var boundingBox = document.getElementsByTagName('iframe')[0].contentWindow.document.getElementById('main').getBoundingClientRect(); var rect = {}; rect.width = boundingBox.width; rect.height = boundingBox.height; rect.left = boundingBox.left; rect.top = boundingBox.top; return rect;

其次,query('webView css:#grabby')是不是有效的查詢。 query('webView css:"#grabby"')query("webView css:'#grabby'")是。如果測試服務器崩潰而不是報告,您很可能會運行Calabash-Android的(非常)舊版本。

+0

這非常有幫助,謝謝 - 特別是將bbox複製到一個新的哈希:沒有它,它只是返回「{}」。 –

+0

我發現Javascript座標需要通過包含iframe在webView中的位置進行偏移,然後偏移並縮放到Android座標系,然後才能用於點擊事物。特別是,我通過用javascript的window.innerWidth和window.innerHeight除以query('webview index:0')的寬度和高度來確定webView的Android和Javascript縮放因子。我需要在其他手機上測試此功能,但目前看起來貌似合理。 –

+0

你可能是對的。你可以看看我們如何縮放和轉換webview座標https://github.com/calabash/calabash-android/blob/master/ruby-gem/test-server/instrumentation-backend/src/sh/calaba/ instrumentationbackend/query/WebContainer.java#L192 – Tobias

0

我寫了什麼等於一個答案,而我正在探索,但我發佈它在這裏保存別人的時間(希望)。


我可以成功地在視圖中運行JavaScript:

query("webView index:0", :loadUrl => 'javascript:alert("Hello")') 

所以我也許可以安排按下按鈕振振有詞地滿足測試:

query("webview index:0", :loadUrl => 'javascript: ifr = document.getElementsByTagName("iframe")[0]; idoc = ifr.contentDocument || ifr.contentWindow.document; idoc.getElementById('myCheck').click(); false') 

要出再次獲取數據,我可以創建一個DIV並將其標題(例如)設置爲一些「返回」值:

query("webView index:0", :loadUrl => 'javascript:var idiv = document.createElement("div"); idiv.id="grabby"; document.getElementsByTagName("body")[0].appendChild(idiv); idiv.title="some return value"; false') 

(中的javascript尾隨「假」:命令,以防止有運行的JavaScript的結果:從更換頁面的命令 - 這花了一段時間來制定出)

我可以看看的iframe太:

query("webView index:0", :loadUrl => 'javascript:var idiv = document.getElementById("grabby"); ifr = document.getElementsByTagName("iframe")[0]; idoc = ifr.contentDocument || ifr.contentWindow.document; idiv.title = idoc.getElementsByTagName("input")[3].id; false') 

當我試圖用

query('webView css:#grabby') 

它掛了一段時間檢索,然後用超時墜毀

HTTPClient::ReceiveTimeoutError: execution expired 
    from /Users/tim./.rvm/gems/ruby-2.1.5/gems/httpclient-2.6.0.1/lib/httpclient/session.rb:876:in `gets' 
    from /Users/tim./.rvm/gems/ruby-2.1.5/gems/httpclient-2.6.0.1/lib/httpclient/session.rb:876:in `block in parse_header' 
    from ... 

奇怪的是,查詢(「web視圖CSS:‘*’」),主要似乎沒有超時工作,並報告「貪婪」 DIV與標題字符串

query('webView css:"*"').find { |e| e['id'] == 'grabby' } 

所以這是足夠有用的完成。我將不得不編寫一些可怕的遞歸下降來剔除iframe的內容 - 可能會使div不可見,只需將htmlContent複製到那裏即可。

我真的不知道爲什麼直接查詢崩潰。

+0

您可能不需要創建div或加載url。我在使用JavaScript iOS WebView API使用iframe的應用程序上做了一些複雜的工作。 _沒有找到這樣的方法:stringByEvaluatingJavaScriptFromString([String])_它只是iOS,你應該知道API已經改變了WKWebView。 https://github.com/calabash/calabash-ios/wiki/06-WebView-Support – jmoody

相關問題