Ios XCUI应用程序的通用tap函数

Ios XCUI应用程序的通用tap函数,ios,ios-ui-automation,xcode-ui-testing,Ios,Ios Ui Automation,Xcode Ui Testing,我们正在尝试从UIAutomation迁移到XUITests。 对于UIAutomation,我们提供了一个方便的“tapOnName”函数,它可以爬过整个子元素树,并在第一个匹配的元素上点击 function log(msg) { UIALogger.logDebug(msg); } //recursive function crawling thru an elements hierarchy //and tapping on the first match of accessibili

我们正在尝试从UIAutomation迁移到XUITests。 对于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")

您可以稍微调整一下,使其涵盖所有需求。

您是否正在寻找类似的产品:

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中对该函数提供了速记支持

要从任何元素中点击第一个匹配项,可以获取所有元素并点击第一个元素:

let app = XCUIApplication()
let element = app.descendentsMatchingType(.Any)["someIdentifier"]
element.tap()
如果您知道它将是什么类型的元素,最好先按该类型进行过滤:

let app = XCUIApplication()
let element = app.buttons["someIdentifier"]
element.tap()

XCTest中对该函数有速记支持

要从任何元素中点击第一个匹配项,可以获取所有元素并点击第一个元素:

let app = XCUIApplication()
let element = app.descendentsMatchingType(.Any)["someIdentifier"]
element.tap()
如果您知道它将是什么类型的元素,最好先按该类型进行过滤:

let app = XCUIApplication()
let element = app.buttons["someIdentifier"]
element.tap()

速记很方便。我们将所有UI元素命名为唯一,因此我们永远不会有问题确定这是否是按钮、TabBar按钮等。这很好,但就速度而言,如果按类型筛选,则速度更快,因为要检查与标识符匹配的字符串更少,而且,如果遇到问题,调试也会变得容易一些,因为您不需要查看每个元素的列表速记很方便。我们将所有UI元素命名为唯一,因此我们永远不会有问题确定这是否是按钮、TabBar按钮等。这很好,但就速度而言,如果按类型筛选,则速度更快,因为要检查与标识符匹配的字符串更少,而且,如果遇到问题,调试也会变得容易一些,因为您不需要查看每个元素的列表