How do I test which element has the focus in Selenium RC?
We can test if an element is focused with Selenium webdriver. This is determined with the help of the activeElement() method. First of all we need to identify the element with help of any of the locators like id, class, name, xpath or css.
The hasFocus() method of the Document interface returns a boolean value indicating whether the document or any element inside the document has focus. This method can be used to determine whether the active element in a document has focus.
activeElement. WebElement activeElement() Switches to the element that currently has focus within the document currently "switched to", or the body element if this cannot be detected. This matches the semantics of calling "document.
Selenium RC comprises an additional layer of JavaScript known as the core which makes it slower. Selenium RC has complicated and redundant APIs. Selenium RC is not compatible with the HTMLUnit browser (required for headless execution). Selenium RC has in-built HTML report generation features for test results.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With