How do I test which element has the focus in Selenium RC?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 18:50:39

The following locator should select the active element in the document:

dom=document.activeElement

If a form field or similar element has focus, then it should be the active element. Hope this helps.

As AlistairH mentions, you can use document.activeElement on most current browsers. To use this in Selenium you can store the active element and compare it to the active element. Below is an example for Selenium IDE, which should point you in the right direction for Selenium RC too.

storeEval | this.browserbot.findElement("name=targetElement").id; | targetElement
storeEval | this.browserbot.getUserWindow().document.activeElement.id; | activeElement
verifyEval | '${targetElement}' | ${activeElement}

Note that the above relies on the element having a unique id assigned.

Another way to test (assuming you are using JQuery or Sizzle) is to use a simple library I wrote which allows Selenium tests to query the DOM using the :focus selector such as .is(':focus') or $(':focus').

For more information, see http://blog.mattheworiordan.com/post/9308775285/testing-focus-with-jquery-and-selenium-or where I explain the reason why Selenium test for :focus don't work out of the box, and how to fix this.

source code:

selenium code: 1 method: String searchValue=selenium.getEval("var test=document.activeElement;");

2 method: String testvalue=selenium.getText("css=#div textarea[class='left_to_right']:focus");

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