2016-09-02 7 views
1

우리는 UIAutomation에서 XCUITests로 마이그레이션하려고합니다. UIAutomation에서 우리는 전체 하위 요소 트리를 크롤링하고 첫 번째 일치 항목을 두드린 편리한 'tapOnName'기능을 생각해 냈습니다.XCUIApplication의 일반 탭 기능

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을 사용하여 동일한 기능을 구현할 수 있습니까?

답변

3

XCTest에서이 함수에 대한 약식 지원이 있습니다.

모든 요소에서 첫 경기 도청를 들어, 첫 번째 모든 요소를 ​​얻고 활용할 수 있습니다 :

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

당신이 될 것입니다 요소의 유형을 알고 있다면, 그것은으로 필터링하는 것이 좋습니다 해당 유형을 먼저 입력하십시오 :

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

매우 편리합니다. 우리는 모든 UI 요소의 이름을 고유하므로이 버튼, TabBar 버튼 등을 결정할 문제가 없습니다. – Leo

+0

속도면에서는 좋지만 형식으로 필터링하면 검사 할 문자열 수가 더 적으므로 빠릅니다 식별자와의 일치를 위해, 또한 모든 요소의 목록을 살펴볼 필요가 없으므로 문제가 발생하면 디버깅을 좀 더 쉽게 할 수 있습니다. :) – Oletha

2

는이 같은 뭔가를 찾고있다 : 당신이 좋아하는 테스트 메소드를 호출

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") 

당신은 그것을 조금 조정할 수 있습니다.