无法在其禁用和启用状态下断言双动按钮

时间:2015-06-24 10:44:38

标签: javascript java jquery selenium-webdriver webdriver

我在一个班级下面有一个按钮。它们配置为双动作。 他们在某个事件之前被禁用。他们被禁用时的DOM:

    <div class="doc-buttons">
<a href="#" onclick="actualsize();" id="tip-size" class="left btn btn-white btn-rounded btn-sm icon-size tooltipstered" disabled="disabled">
    <i></i>
</a>
<a href="#" onclick="scaletofit();" id="tip-fit" class="left btn btn-white btn-rounded btn-sm icon-fit tooltipstered" disabled="disabled" style="display: none;">
    <i></i>
</a>                

...

在某个事件发生后,它们会被启用并且它们的DOM会更改为:

<div class="doc-buttons">
<a href="#" onclick="actualsize();" id="tip-size" class="left btn btn-white btn-rounded btn-sm icon-size tooltipstered">
    <i></i>
</a>
 <a href="#" onclick="scaletofit();" id="tip-fit" class="left btn btn-white btn-rounded btn-sm icon-fit tooltipstered" style="display: none;">
    <i></i>
 </a>                

...    ...    ...

我所要做的就是断言(使用TestNG)它们在正确的时间被启用和禁用(听起来很简单!)

ele1 = _driver.findElement(By.xpath("//a[@id='tip-size']"));
ele2 = _driver.findElement(By.xpath("//a[@id='tip-fit']"));

ele1,2代表这些按钮的定位器。

System.out.println("ele1.getAttribute("disabled")");
System.out.println("ele2.getAttribute("disabled")");

令我惊讶的是,无论按钮的状态如何(启用或禁用),上面的打印语句总是返回TRUE

我应该如何在禁用和启用状态下断言它们?

PS:我是WebDriver,Java和TestNG的新手。任何解释,链接到博客等都将受到高度赞赏

2 个答案:

答案 0 :(得分:1)

你应该工作。不知道为什么不。但是,另一种解决方法可能是检查属性是否存在于JavaScriptExecutor

boolean hasTipSize = (boolean) ((JavascriptExecutor)driver).executeScript("return document.getElementById('tip-size').hasAttribute('disable')");
boolean hasTipFit = (boolean) ((JavascriptExecutor)driver).executeScript("return document.getElementById('tip-fit').hasAttribute('disable')");

如果disabled属性存在,则应返回 true false

答案 1 :(得分:0)

在所谓的“事件”之后,应该重新初始化元素。

步骤: 1.捕获元素ele1,ele2。

ele1.getAttribute("disabled") will be "true/disabled"
ele2.getAttribute("disabled") will be "true/disabled"
  1. 执行事件(更改元素的状态)

  2. 现在再次捕获元素。

    ele1 = _driver.findElement(By.xpath("//a[@id='tip-size']")) ele2 = _driver.findElement(By.xpath("//a[@id='tip-fit']"));

  3. 现在提取元素属性/属性。

    ele1.getAttribute("disabled") should be updated ele2.getAttribute("disabled") should be updated