Kruger Brent Kruger Brent - 1 month ago 7
Swift Question

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

Suppose I am using a code snippet in

setup()
method just before running a test case like this:

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


I am running into an error.
Assertion
'assertWithMatcher: matcherForSufficientlyVisible(>=0.750000)' was not performed because no UI element matching (respondsToSelector(accessibilityIdentifier) && accessibilityID("TabBar-Navigation-Activity"))
was found.

Actually I want to check the presence of an element. If element is present, do a certain step or do something else. But although the element is not present on that particular screen, the code inside the loop is not executed.
Any suggestion would be appreciated.

Answer

Try below method for nil check:

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

Try below code to check 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")
            }