XCUITest我使用isHittable,但是发生断言失败吗?我的iPhone是ios 12

问题描述

我的代码

if(button.isHittable)//when it returns false,test fail,I can not do button2.tap 
{ 
   button1.tap 
}else{ 
   button2.tap 
}

在点击按钮之前,我使用button.isHittable获取按钮是否可以点击。但是当我使用button.isHittable并得到false时,会发生断言失败,并且我的测试失败。我想要当button.isHittable为false时,我可以继续执行其他步骤。

我该怎么办?

解决方法

  1. 您检查按钮是否可以点击,但是请点击button1。您应该正确命名元素,以避免出现此类错误。
  2. 您不应在测试代码中包含if-else构造。您应该预先确定测试步骤,以减少假阳性测试。
,

如何在查询中识别按钮?你是这样吗?

XCUIApplication().buttons.firstMatch

您是否正在使用 firstMatch 属性? https://developer.apple.com/documentation/xctest/xcuielementtypequeryprovider/2902250-firstmatch

如果是,则必须先检查 button.exists 是否存在,然后再检查 button.isHittable -如果您使用的是 firstMatch 这与强制展开可选内容非常相似。

示例:

if button.exists && button.isHittable { ... safely tap the button here }