在功能测试用例中,检查元素是否存在无法正常工作

时间:2016-11-03 07:03:07

标签: ios swift earlgrey

假设我在运行这样的测试用例之前在setup()方法中使用了代码段:

if EarlGrey().selectElementWithMatcher(grey_accessibilityID("TabBar-Navigation-Search")).assertWithMatcher(grey_sufficientlyVisible()) != nil {
    TabBarNavigation().navigateToSearch()
} else {
    assentViewModelsSwifts.signInCheck()
    assentViewModelsSwifts.enterLoginCredentials("username", password: "password")
    let visibleSignInButtonMatcher = grey_allOfMatchers(grey_accessibilityID("Login-Button-SignIn"), grey_sufficientlyVisible())
    EarlGrey().selectElementWithMatcher(visibleSignInButtonMatcher).performAction(grey_tap())
    TabBarNavigation().navigateToSearch()
    Log.info("Landed on Search Page")
}

我遇到了错误。 断言'assertWithMatcher: matcherForSufficientlyVisible(>=0.750000)' was not performed because no UI element matching (respondsToSelector(accessibilityIdentifier) && accessibilityID("TabBar-Navigation-Activity"))被发现。

实际上我想检查一个元素的存在。如果元素存在,请执行某个步骤或执行其他操作。但是,虽然该特定屏幕上不存在该元素,但循环内的代码不会被执行。 任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:2)

尝试以下方法进行零检查:

- (instancetype)assertWithMatcher:(id<GREYMatcher>)matcher error:(__strong NSError **)errorOrNil

请尝试以下代码检查nil:

let errorOrNil
EarlGrey().selectElementWithMatcher(grey_accessibilityID("TabBar-Navigation-Search")).assertWithMatcher(grey_sufficientlyVisible(), error:&errorOrNil)
 if errorOrNil != nil {
                TabBarNavigation().navigateToSearch()
            } else {
            assentViewModelsSwifts.signInCheck()
            assentViewModelsSwifts.enterLoginCredentials("username", password: "password")
            let visibleSignInButtonMatcher = grey_allOfMatchers(grey_accessibilityID("Login-Button-SignIn"), grey_sufficientlyVisible())
            EarlGrey().selectElementWithMatcher(visibleSignInButtonMatcher).performAction(grey_tap())
            TabBarNavigation().navigateToSearch()
            Log.info("Landed on Search Page")
            }