开发者

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

How do I test which element has the focus in Seleni开发者_如何学运维um RC?


None of the other answers worked for me, for various reasons when I had the same issue. I ended up doing the following (with the Selenium 2.0 WebDriver, in Java).

WebDriver driver = new FirefoxDriver();
String elScript = "return document.activeElement;";

// Note that the executeScript call returns a WebElement, 
// so you can do quite a lot with the result.
WebElement focuseedEl  = (WebElement) ((JavascriptExecutor) driver).executeScript(elScript);

The comment clarifies a point of confusion I had: executeScript returns different things based on what you tell it to execute, which could be quite tricky.


I couldn't get the :focus pseudoclass idea to work, not sure why - targeting css=#search_input input.text matched, but css=#search_input input.text:focus didn't? But here is what worked for me:

self.assertEquals(self.se.get_element_index('dom=document.activeElement'),
     self.se.get_element_index('//input[@id="search-query"]'))

This is using the Python Selenium API, so the get_element_index() calls are wrappers around the core Selenium command list. Adapt this to your environment. It's evaluating the element index of the document's focused element (gotten with a Javascript DOM locator) and the element index of the element you want to test for focus (gotten with an XPath locator.) Also see this question.


You can use CSS selectors when providing an element locator to Selenium: http://release.seleniumhq.org/selenium-core/1.0/reference.html#locators

Therefore, you can use the :focus CSS pseudo-class on your selector to only match if the element is focused.

Combine that with a verifyElementPresent action and a target of something like the following: css=.yourclassname:focus. Replace yourclassname with your CSS class name, of course, or use one of the other CSS selectors; the important bit is the :focus at the end.

Note that this will almost certainly not work in the Selenium IDE Firefox plugin. I imagine that is because this plugin will have focus instead. I couldn't get it to work in the IDE (test always failed), but it worked fine once I exported it and ran it as a Java test.

HTH

Sam


I have found the solution to this problem, and developed a simple script to fix this issue when running your tests. The problem lies with your browser's native match and query selectors being operating system aware, meaning that unless your browser and the web page tab has the focus within the operating system, then the :focus selector will not match ANY items within your document. I've poked around inside JQuery to work out a way around this, and this can be done by simply forcing JQuery to use Sizzle and not optimise the selector queries using the browser's native functions.

If you include the following script https://gist.github.com/1166821 BEFORE you include JQuery, then your Selenium tests will pass the :focus tests.

For a complete write up of the issue, the solution and how to run tests, please review http://blog.mattheworiordan.com/post/9308775285/testing-focus-with-jquery-and-selenium-or

Hope that helps others who have this same issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜