iOS | XCUITest — Automating on in-app WebViews
Automating on WebViews within native apps in iOS using XCTest is lot easier, than in most other mobile automation tools in the market.
The only difference between native commands and WebView commands in XCUITest is the presence and absence of ‘webViews’ between ‘app’ and screen ‘elements’…
app
.webViews
.staticTexts["abc_TextMessage"]app
.webViews
.links["abc_Link"]
.tap()app
.webViews
.buttons["abc_Button"]
.tap()
That also means, test professionals do not have to Switch Contexts between web and native app elements within scripting, like in Appium.
How does keyboard key work on WebViews?
Calling XCUIKeyboardKey.delete with the webView command also works perfectly fine as in the sample example below:
app
.webViews
.textFields["username_Id"]
.typeText(XCUIKeyboardKey.delete.rawValue)
How to enter text in password field on WebViews?
app
.webViews
.secureTextFields["password_Id"]
.typeText("MyPassword123")
How to force tap on WebView elements?
The below code might be handy in cases where normal tapping fails to work for any reason. Hence, force tapping using coordinates becomes the last resort, if it leaves you with no other way.
app
.webViews
.switches.element(boundBy: 3)
.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
.withOffset(CGVector(dx: 22, dy: 22))
.tap()
How to Verify URLs in WebViews?
let url = app.buttons[urlText].value as! String
let getUrl = url.components(separatedBy: “,”).first!XCTAssertTrue(getUrl.contains("https://www.xyz.com"))
Similarly, most components work perfectly fine while automating using in-app WebViews just by using ‘app.webViews’
Thanks for reading this article! Leave a comment below if you have any questions. Be sure to click on the 👏 icon to let me know that you encourage my writing.
Connect with me: