问题
As EarlGrey runs in the process and there is no install/uninstall process involved as in XCUI test framework, I expected tests to run faster but I noticed it is almost the same speed as XCUI test framework. Any reasons?
It's very slow to get TabBar or NavBar items. How can I speed up the process?
I'm using this for TabBar elements to match
let tabButtonMatcher: GREYMatcher = grey_allOf([grey_accessibilityLabel("Something"), grey_accessibilityTrait(UIAccessibilityTraitButton), grey_sufficientlyVisible()])
EarlGrey.selectElement(with: tabButtonMatcher).perform(grey_tap()).assert(grey_sufficientlyVisible())
Similar for NavBar buttons also
let navMatch: GREYMatcher = grey_allOf([grey_accessibilityID("Something"), grey_accessibilityTrait(UIAccessibilityTraitButton), grey_sufficientlyVisible()])
EarlGrey.selectElement(with: navMatch).perform(grey_tap())
回答1:
You must remove grey_sufficientlyVisible
from your matchers because it doesn't make sense. This matcher is guaranteed to work well in the assert
statement, but it seems it's inefficient to pass as a matcher parameter.
EarlGrey interacts with touch events on the run loop and, generally, it interacts with the application as well as a real user. Its unlikely tests will be fast in this case, especially compared with KIF which calls selectors on UI elements instead of passing touch events. That's not bad, it's an expected behavior for functional tests. By the way, you could speed up tests by disabling animations:
GREYTestHelper.enableFastAnimation()
Regarding your question related to matchers, I would suggest you try several approaches and find one which is suited in your case.
At first sight, it should be worth to just find a UITabBarItem
using grey_kindOfClass
.
Matcher example (not working)
grey_allOf([grey_kindOfClass(UITabBarItem.self), grey_text(title)])
But it's not the working solution. Let's see on view hierarchy:
So if you rely on UITabBarItem
it doesn't work, because there is no such element. You can see, that there is a private UIKit class called UITabBarButton
which is not supposed to interact with. Moreover, it's not a subclass of UIButton
, but it's a subclass of UIControl
😀
Instead, I would recommend to select element on UITabBar
using inRoot
function.
Matcher example (working)
EarlGrey.selectElement(with: grey_text(tabbarTitle))
.inRoot(grey_kindOfClass(UITabBar.self))
.perform(grey_tap())
In this case, you find an element, which has the expected text and which is located on the UITabBar
.
For selection elements on the navigation bar it's possible to use variations of this matcher:
Matcher example to find the element on navigation bar
EarlGrey.selectElement(with: grey_kindOfClass(UIButton.self))
.inRoot(grey_kindOfClass(UINavigationBar.self))
来源:https://stackoverflow.com/questions/52827294/earlgrey-is-slow-when-accessing-navbar-tabbar-items