Search code examples
androidiframecalabash

On Android in Cucumber/Calabash, how do I test with a webview iframe?


I can locate an iframe in an Android webview, but I need to interact with it. Various pages suggest

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

but that results in "No such method found: stringByEvaluatingJavaScriptFromString([String])" and it looks like an iOS function, so I'm pretty sure this is an iOS-only solution: I need Android.

I know I can pass javascript queries in by opening them in a URL, but I've only experienced getting stuff out of an Android webview with specially-code Chrome objects linked in to the application.

Any ideas for getting data out?


Solution

  • The iOS responses are correct, you should use javascript to get the contents of an iframe. However, you should use evaluate_javascript(query_string, javascript) to evaluate javascript in Android. e.g

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

    Use the coordinates to tap the view.

    If the rect is not parsed correctly, you could do something like this: 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;

    Secondly, query('webView css:#grabby') is not a valid query. query('webView css:"#grabby"') or query("webView css:'#grabby'") is. You are most likely running a (very) old version of Calabash-Android if the test-server is crashing instead of reporting it.