Xcode UI Testing - Finding element in Webview by id

浪尽此生 提交于 2019-12-10 19:06:39

问题


Our app is hybrid and contains webview. I'm trying to automate our app using Xcode UI Testing. I was able to locate web buttons using:

let app = XCUIApplication() 
app.launch() 
let button = app.staticTexts["Button's text"]

But in one of our tests we test localization - meaning the text changes and the static text query is no longer valid. Couldn't find any documentation how to locate an element by his id (or even class name). Any ideas?


回答1:


I have had a similar problem trying to fill in text fields in a SFSafariViewController.

I found that using the aria-label attribute allowed me to locate elements in the page e.g.

<input role="textbox" aria-label="email" type="email" value="" name="user[email]" id="user_email">

And then in your code to select that element

webViewsQuery.textFields["email"]



回答2:


By id(ui-test code e.g on swift):

    app.buttons["yourBtnAccessibilityId"]

this id sets into you app`s code (e.g on obj-c) and should be unique.

    yourButton.accessibilityIdentifier = @"yourBtnAccessibilityId";

By class(ui-test code):

    app.buttons.elementBoundByIndex(1)


来源:https://stackoverflow.com/questions/34871650/xcode-ui-testing-finding-element-in-webview-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!