isEnabled 属性 在 Appium 中没有按预期工作

isEnabled property not working as expected in Appium

谁能通俗地向我解释命令 isEnabled() 在 Appium 中是如何工作的 场景:特定移动页面上有 3 个复选框。 - 复选框 1、复选框 2 和复选框 3 “Checbox3”默认禁用,只有当我们select“Checkbox2”

时才启用

TC是为了验证“Checkbox3”是否默认禁用并打印以下输出 “复选框 3 当前已禁用”

启用“Checkbox3”后,我们需要打印以下输出 “复选框 3 当前已启用”

我将其作为 TestNG 执行并使用以下代码行

boolean FirstValue = XMLPage.ListofCheckboxes.get(2).isEnabled();
System.out.println(FirstValue);
if(FirstValue=false)
{
    System.out.println("Checkbox3 is currently Disabled");
}
else
{
    System.out.println("Checkbox3 is currently Enabled");
}
Thread.sleep(4000);
XMLPage.ListofCheckboxes.get(1).click();
boolean SecondValue = XMLPage.ListofCheckboxes.get(2).isEnabled();
System.out.println(SecondValue);
if(SecondValue=true)
{
    System.out.println("Checkbox3 is currently Enabled");
}
else
{
    System.out.println("Checkbox3 is currently Disabled");
}

预期输出:

  1. 错误
  2. "Checkbox3 is currently Disabled"
  3. 正确
  4. "Checkbox3 is currently Enabled"

实际输出:

  1. 错误
  2. "Checkbox3 is currently Enabled"
  3. 正确
  4. "Checkbox3 is currently Enabled"

第一次遇到 if 语句时,它不应该将输出打印为 "Checkbox3 is currently Disabled" 我不确定为什么它会打印“else”下提到的输出{即:- "Checkbox3 is currently Enabled"} . 如输出中所示,命令“boolean FirstValue = XMLPage.ListofCheckboxes.get(2).isEnabled();”返回的值是错误的,因此代码应该打印“if”而不是“else”下提到的输出。

您在代码中使用的 if 语句是错误的。

代码中的 if 语句

if(FirstValue=false){
....
}

这是一个赋值操作,因为您使用了单个 = 符号。它会做的是,它会将 FirstValue 赋值为 false,然后由于 if 块中的值现在为 false,它会转到 else 块。这是一个有效的操作,因此可以正常编译。但是 if 语句 应该是一个比较步骤 就像下面使用 == 符号,

if语句应该是一个比较步骤,

if(FirstValue==false){
....
}

这就是你输出错误的原因。

我对之前粘贴的代码做了一处更改,它显示了预期的输出。我将第一个 if 语句中的条件从“false”更改为“true” 但这让我更加困惑。为什么在用户第一次到达移动页面时禁用复选框时返回的值为“true”

boolean FirstValue = XMLPage.ListofCheckboxes.get(2).isEnabled();
System.out.println(FirstValue);
if(FirstValue=true)
{
    System.out.println("Checkbox3 is currently Disabled");
}
else
{
    System.out.println("Checkbox3 is currently Enabled");
}
Thread.sleep(4000);
XMLPage.ListofCheckboxes.get(1).click();
boolean SecondValue = XMLPage.ListofCheckboxes.get(2).isEnabled();
System.out.println(SecondValue);
if(SecondValue=true)
{
    System.out.println("Checkbox3 is currently Enabled");
}
else
{
    System.out.println("Checkbox3 is currently Disabled");
}

预期输出: 1. 假的 2. "Checkbox3 is currently Disabled" 3.真实 4. "Checkbox3 is currently Enabled"

实际输出: 1. 假的 2. "Checkbox3 is currently Disabled" 3.真实 4. "Checkbox3 is currently Enabled"