Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

selenium-rc

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

like image 331
hwiechers Avatar asked Apr 17 '10 20:04

hwiechers


People also ask

How do you know if an element is focused in Selenium?

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.

How do you know if an element is focused or not?

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.

What is driver switchTo () activeElement ()?

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.

Why Selenium RC is not used?

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.


3 Answers

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.

like image 86
rcreswick Avatar answered Sep 27 '22 22:09

rcreswick


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.

like image 40
3 revs, 2 users 74% Avatar answered Sep 28 '22 00:09

3 revs, 2 users 74%


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

like image 26
Sam Avatar answered Sep 27 '22 23:09

Sam