对元素是否存在的 nil 检查在功能测试用例中没有按预期工作

The nil check for presence of an element is not working as expected in functional test case

假设我在 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"))

其实我想检查一个元素是否存在。如果存在元素,则执行某个步骤或执行其他操作。但是,尽管该元素不存在于该特定屏幕上,但循环内的代码并未执行。 如有任何建议,我们将不胜感激。

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

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