Can I test on the order of elements using Selenium Webdriver?

十年热恋 提交于 2020-08-08 07:20:40

问题


There is a form in which there are 3 fields with 3 different ID's:

<fieldset>
<div><label for="field1">
 FIELD1
 </label>
 <textarea id="field1" name="field1" rows="10" cols="40"></textarea></div>


 <div><label for="field2">
  FIELD2
</label><textarea id="field2" name="field2" rows="10" cols="40"></textarea></div>


<div><label for="field3">
  FIELD3
</label><textarea id="field3" name="field3" rows="10" cols="40"></textarea></div>

</fieldset>

Now, there is a Story in our scrum process which places FIELD3 at the top of the page, above FIELD1, so that the new order of fields will be 3, 1, 2.

My question is: Is there a way to verify/assert the order? If yes, how do I test this automatically using Selenium Webdriver (java)?


回答1:


You sure can. Here's some pseudo-code.

assertTrue(
    driver.findElement(
        By.cssSelector("div#parent > div:nth-child(1) > textarea#field3")
    ).isDisplayed()
);

The logic here pretty much says:

Look for the first div that is under <div id="parent">, that is immediately followed by <textarea id="field3">. If this assertion fails, that means it's out of order, and the third field is NOT first.

(note, that div#parent is obviously subject to change. You need to change it to the parent of these divs, and you may also need to fiddle about with the :nth-child(n) depending on the order)



来源:https://stackoverflow.com/questions/23133024/can-i-test-on-the-order-of-elements-using-selenium-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!