我寫了什麼等於一個答案,而我正在探索,但我發佈它在這裏保存別人的時間(希望)。
我可以成功地在視圖中運行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複製到那裏即可。
我真的不知道爲什麼直接查詢崩潰。
這非常有幫助,謝謝 - 特別是將bbox複製到一個新的哈希:沒有它,它只是返回「{}」。 –
我發現Javascript座標需要通過包含iframe在webView中的位置進行偏移,然後偏移並縮放到Android座標系,然後才能用於點擊事物。特別是,我通過用javascript的window.innerWidth和window.innerHeight除以query('webview index:0')的寬度和高度來確定webView的Android和Javascript縮放因子。我需要在其他手機上測試此功能,但目前看起來貌似合理。 –
你可能是對的。你可以看看我們如何縮放和轉換webview座標https://github.com/calabash/calabash-android/blob/master/ruby-gem/test-server/instrumentation-backend/src/sh/calaba/ instrumentationbackend/query/WebContainer.java#L192 – Tobias