XCUIApplication 的通用点击功能
generic tap function for XCUIApplication
我们正在尝试从 UIAutomation 迁移到 XCUITests。
对于 UIAutomation,我们想出了一个方便的 'tapOnName' 函数,它只是爬过整个子元素树并点击第一个匹配的元素。
function log(msg) {
UIALogger.logDebug(msg);
}
//recursive function crawling thru an elements hierarchy
//and tapping on the first match of accessibilityIdentifier
//or button text
function tapOnNameWithRoot(name,el) {
if (el.name()==name && el.isVisible()) {
log("tap on itt!!!")
el.tap();
return true;
}
if (el.toString()=="[object UIAButton]" && el.label()==name) {
log("tap on Button!!!")
el.tap();
return true;
}
var elements=el.elements();
if (elements===null || elements===undefined) {
log("elements null or undefined for:"+el.toString());
return false;
}
for(var i=0,len=elements.length ;i<len;i++) {
if (tapOnNameWithRoot(name,elements[i])) {
return true;
}
}
return false;
}
var win = UIATarget.localTarget().frontMostApp().mainWindow();
//for ex taps on a button with the text "pushme" in the
//main UIWindow
tapOnNameWithRoot("pushme",win);
没有问题:是否可以使用 XCUIApplication 实现相同的功能?
您是否正在寻找这样的东西:
func tapBasedOnAccessibilityIdentifier(elementType elementType: XCUIElementQuery, accessibilityIdentifier: String) {
var isElementExist = false
for element in elementType.allElementsBoundByIndex {
if element.label == accessibilityIdentifier {
element.tap()
isElementExist = true
break
}
}
if !isElementExist {
XCTFail("Failed to find element")
}
}
您在测试中调用方法的位置如下:
tapBasedOnAccessibilityIdentifier(elementType: app.staticTexts, accessibilityIdentifier: "Accessibility Identifier")
您可以稍微调整一下,使其涵盖所有要求。
XCTest shorthand 支持此功能。
对于从任何元素中挖掘第一个匹配项,您可以获取所有元素并挖掘第一个:
let app = XCUIApplication()
let element = app.descendentsMatchingType(.Any)["someIdentifier"]
element.tap()
如果您知道它将是什么类型的元素,最好先按该类型过滤:
let app = XCUIApplication()
let element = app.buttons["someIdentifier"]
element.tap()
我们正在尝试从 UIAutomation 迁移到 XCUITests。 对于 UIAutomation,我们想出了一个方便的 'tapOnName' 函数,它只是爬过整个子元素树并点击第一个匹配的元素。
function log(msg) {
UIALogger.logDebug(msg);
}
//recursive function crawling thru an elements hierarchy
//and tapping on the first match of accessibilityIdentifier
//or button text
function tapOnNameWithRoot(name,el) {
if (el.name()==name && el.isVisible()) {
log("tap on itt!!!")
el.tap();
return true;
}
if (el.toString()=="[object UIAButton]" && el.label()==name) {
log("tap on Button!!!")
el.tap();
return true;
}
var elements=el.elements();
if (elements===null || elements===undefined) {
log("elements null or undefined for:"+el.toString());
return false;
}
for(var i=0,len=elements.length ;i<len;i++) {
if (tapOnNameWithRoot(name,elements[i])) {
return true;
}
}
return false;
}
var win = UIATarget.localTarget().frontMostApp().mainWindow();
//for ex taps on a button with the text "pushme" in the
//main UIWindow
tapOnNameWithRoot("pushme",win);
没有问题:是否可以使用 XCUIApplication 实现相同的功能?
您是否正在寻找这样的东西:
func tapBasedOnAccessibilityIdentifier(elementType elementType: XCUIElementQuery, accessibilityIdentifier: String) {
var isElementExist = false
for element in elementType.allElementsBoundByIndex {
if element.label == accessibilityIdentifier {
element.tap()
isElementExist = true
break
}
}
if !isElementExist {
XCTFail("Failed to find element")
}
}
您在测试中调用方法的位置如下:
tapBasedOnAccessibilityIdentifier(elementType: app.staticTexts, accessibilityIdentifier: "Accessibility Identifier")
您可以稍微调整一下,使其涵盖所有要求。
XCTest shorthand 支持此功能。
对于从任何元素中挖掘第一个匹配项,您可以获取所有元素并挖掘第一个:
let app = XCUIApplication()
let element = app.descendentsMatchingType(.Any)["someIdentifier"]
element.tap()
如果您知道它将是什么类型的元素,最好先按该类型过滤:
let app = XCUIApplication()
let element = app.buttons["someIdentifier"]
element.tap()