使用 EarlGrey 按导航栏上的后退按钮

kha*_*pur 5 testing ios earlgrey

我在尝试编写一个使用后退按钮的测试时遇到了一些障碍,希望有人能用这个为我指明正确的方向。

我的视图非常标准,我有一个 UINavigation 控制器作为我的根视图控制器,我将其推到某个点,然后需要返回。

我尝试制作一个匹配器来查找导航栏中的后退按钮,但没有取得任何成功。我尝试使用标签、id 和值,在后退按钮项上设置可访问性标识符并未导致匹配,即使它适用于我的其他界面元素。

[self.navigationItem.backBarButtonItem setAccessibilityIdentifier:@"Back"];
Run Code Online (Sandbox Code Playgroud)

在我的测试中我有以下内容:

// Press the back button

id<GREYMatcher> backMatcher =
    grey_allOf(grey_accessibilityID(@"Back"), grey_sufficientlyVisible(), nil);

[[EarlGrey selectElementWithMatcher:backMatcher] performAction:grey_tap()];
Run Code Online (Sandbox Code Playgroud)

我使用断点和PO进行更深入的研究,发现后退按钮的accessibilityLabel确实设置为@“Back”,但它与以下代码也不匹配。

// Press the back button

id<GREYMatcher> backMatcher =
    grey_allOf(grey_accessibilityID(@"Back"), grey_sufficientlyVisible(), nil);

[[EarlGrey selectElementWithMatcher:backMatcher] performAction:grey_tap()];
Run Code Online (Sandbox Code Playgroud)

gra*_*aci 3

问题是您需要一个更具体的匹配器才能BackUINavigationController. 如果您查看测试失败时转储的 ui 层次结构,您会注意到多个元素具有可访问性标签Back。因此,您需要使用匹配器进一步缩小搜索范围,grey_accessibilityTrait如下所示:

id<GREYMatcher> matcherForBackButton =
    grey_allOf(grey_accessibilityLabel(@"Back"),
               grey_accessibilityTrait(UIAccessibilityTraitButton),
               nil);

[[EarlGrey selectElementWithMatcher:matcherForBackButton]
    performAction:grey_tap()];
Run Code Online (Sandbox Code Playgroud)

  • 我用过:`[EarlGrey selectElementWithMatcher:grey_buttonTitle(@"Back")];` (3认同)