Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium IDE: How to check if an element has a focus?

Is there a built in method for checking that an input text element has a focus ?

Well, I didn't find one, so I tried this extension.

But, it doesn't work for me either (i.e. the test fails).

Any ideas ?

like image 890
Misha Moroshko Avatar asked Mar 23 '11 03:03

Misha Moroshko


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 get focused elements in Selenium?

In Selenium 2.0, if you are using WebDriver to drive the tests in the browser, you can use the WebDriver. TargetLocator class to get the element in focus, in a window/frame: WebDriver driver = ... // initialize the driver WebElement currentElement = driver.

How do you change focus in Selenium?

Selenium driver object can access the elements of the parent window. In order to switch its focus from the parent to the new popup tab, we shall take the help of the switchTo(). window method and pass the window handle id of the popup as an argument to the method.

Which command is used to check the presence of element in Selenium IDE?

We can use following two commands to verify the presence of an element: verifyElementPresent – returns TRUE if the specified element was FOUND in the page; FALSE if otherwise. verifyElementNotPresent – returns TRUE if the specified element was NOT FOUND anywhere in the page; FALSE if it is present.


1 Answers

I have had numerous problems detecting if an element has focus because the browser Selenium is controlling typically does not have the focus within the Operating System, and as such the browser will NOT consider any elements to have focus until the browser regains the focus.

I have been pulling my hair out over this, so worked up a solution to this problem. See http://blog.mattheworiordan.com/post/9308775285/testing-focus-with-jquery-and-selenium-or for a full explanation of the problem and a solution to this.

If you can't be arsed to read the lengthy explanation, simply include https://gist.github.com/1166821 BEFORE you include JQuery, and use $(':focus') to find the element that has focus, or .is(':focus') to check if an element has focus.

like image 123
Matthew O'Riordan Avatar answered Oct 30 '22 20:10

Matthew O'Riordan