首页 > 解决方案 > isEnabled 属性在 Appium 中未按预期工作

问题描述

有人可以用外行的方式向我解释命令 isEnabled() 在 Appium 场景中的工作原理:特定移动页面上有 3 个复选框。- Checkbox 1, Checkbox2 & Checkbox3 “Checkbox3”默认是禁用的,只有当我们选择“Checkbox2”时才会启用</p>

TC是验证“Checkbox3”是否默认禁用,并打印如下输出“Checkbox3 is current Disabled”</p>

启用“Checkbox3”后,我们需要打印以下输出“Checkbox3 is current Enabled”</p>

我将其作为 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. “复选框 3 当前已禁用”
  3. 真的
  4. “复选框 3 当前已启用”

实际输出:

  1. 错误的
  2. “复选框 3 当前已启用”
  3. 真的
  4. “复选框 3 当前已启用”

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

标签: appiumisenabled

解决方案


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

代码中的 if 语句

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

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

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

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

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


推荐阅读