问题
I'm new to this iOS world but quite excited to use XCUITest to automate my iOS webView application. I'm trying to figure out as to how can i run javascript from my XCTest for webView application and fetch the value of the hidden variable.
For e.g. my app has webView and it's page source contains the following elements
<input type="hidden" name="demo" value="test">
I've got the handle of the webView using the following:
let webViewQury:XCUIElementQuery = app.descendantsMatchingType(.WebView)
let webview = webViewQury.elementAtIndex(0)
but i'm not sure as to how can run javascript to get the value of the element mentioned above i.e. "demo". I know we can use stringByEvaluatingJavaScriptFromString
but this method is applicable for UIWebView
.
回答1:
Sadly, the answer is you can't
. Indeed, you cannot access your app code while UI Testing:
UI testing exercises your app's UI in the same way that users do without access to your app's internal methods, functions, and variables. This enables your tests to see the app the same way a user does, exposing UI problems that users encounter.
Your test code runs as a separate process, synthesizing events that UI in your app responds to.
(Source: Apple doc)
You only can interact with your app by using XCUIApplication and XCUIElement. They do not allow you to do what you want.
You really should concider UI Testing as a way to act as a user on your application. You will never ask to a user do my UIWebView contains this source code?
来源:https://stackoverflow.com/questions/39472898/how-to-run-javascript-and-fetch-a-value-of-hidden-variable-in-page-source-using