Xcode UItesting – 通过id在Webview中查找元素

我们的应用程序是混合的,包含webview。 我试图使用Xcode UItesting来自动化我们的应用程序。 我能够find网页button使用:

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

但是在我们的一个testing中,我们testing了本地化 – 这意味着文本的变化和静态的文本查询不再有效。 找不到任何文档如何通过他的id(甚至是类名)find一个元素。 有任何想法吗?

我有一个类似的问题,试图填写SFSafariViewController中的文本字段。

我发现使用aria-label属性允许我find页面中的元素,例如

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

然后在你的代码中select那个元素

 webViewsQuery.textFields["email"] 

通过id(ui-test代码,例如在swift上):

  app.buttons["yourBtnAccessibilityId"] 

这个ID设置成你的应用程序的代码 (例如obj-c),应该是唯一的。

  yourButton.accessibilityIdentifier = @"yourBtnAccessibilityId"; 

按类(ui-test code):

  app.buttons.elementBoundByIndex(1)