gpt4 book ai didi

android - 使用 Espresso 确定按钮是否不可点击

转载 作者:太空宇宙 更新时间:2023-11-03 12:48:48 25 4
gpt4 key购买 nike

onView(withId(R.id.login_button)).check(matches(isClickable()));

对于验证按钮是否 很有用。我如何验证按钮不是可点击的?

编辑:就像我说的,它只告诉我它是否 isClickable。我正在寻找一种方法来验证它不是可点击的。

最佳答案

Edit - Solution!

The solution is to use the not() function which returns a matcher that has the opposite logic of the matcher you pass to it.

它很简单:not( isClickable() )

onView(withId(R.id.login_button)).check(matches( not(isClickable()) ));

同样在较新的版本中,我们提供了以下功能。

 public static Matcher<View> isNotClickable() {
return new IsClickableMatcher(false);
}

按如下方式使用即可。

onView(withId(R.id.button)).check(matches(isNotClickable()))


解释-

我试图使用匹配或检查函数来测试它们传递的匹配器的虚假性,但这是不可能的。但是,可以使用 not() 函数创建一个与另一个匹配器逻辑相反的匹配器。

看完documentation on ViewAssertions (比如匹配),你会发现大多数 ViewAssertions 不接受参数(在这种情况下没有接受我们关心的参数),并且必须 "Throw junit.framework.AssertionError when the view断言不成立。”。这意味着我们无法更改 ViewAssertion 的工作方式,我们必须找到另一种方法。


Breaking down the code -

onView 函数返回我们要使用的 UI 对象(id 为 login_button 的按钮),我们调用 check在该 View 对象上应用断言,或者检查某个断言是否正确。

matches 函数返回我们通过传入 Matcher 构建的断言。 isClickable() 函数返回给我们一个匹配器,我们可以使用它返回给 matches()

not() 是一个匹配器,它接受另一个匹配器,并返回一个匹配器,该匹配器具有传递给它的匹配器的相反逻辑/返回值。 (这是一口!)

这归结为说我们对 login_button 应用断言。我们应用的断言是 matches( not( isClickable() ) )


作为 ViewAssertion (matches(not(isClickable()))) 被应用 (onView(...).check(...)) 到查看对象 (login_button),该操作将记录到 logcat,如果断言被评估为 false,则还将抛出 AssertionError

关于android - 使用 Espresso 确定按钮是否不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36536523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com