1

I have a text field after login. After login cursor will be focused on that text field automatically. How can I verify whether cursor/focus is present or not on that text field?

Here is the HTML code of text field:

<input type="text" name="field(TITLE)" id="widget_polarisCommunityInput_113_title">
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176

3 Answers3

2

You could also try the direct webdriver method:

driver.switchTo().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.activeElement" in Javascript. Returns: The WebElement with focus, or the body element if no element with focus can be detected.

Arek
  • 1,941
  • 4
  • 22
  • 27
1

You could check document.activeElement
This is supported in all major browsers.

See this on SO for more information. This question is probably a duplicate of this one.

Good luck!

Community
  • 1
  • 1
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
  • I think, JavascriptExecutor code is as below: jse.executeScript("document.getElementById('widget_polarisCommunityInput_113_title').activeElement"); How can I verify focus is on that text field? – Ripon Al Wasim Jul 31 '12 at 06:21
  • well, document.activeElement should give the active element.. so you want to compare: `if ( document.getElementById('widget_polarisCommunityInput_113_tit‌​le') === document.activeElement ) { //actions}` – GitaarLAB Aug 04 '12 at 15:30
  • After executing the above code (JS code), How can I do assertion? I want to implement assertTrue() if focus is on the text field or not – Ripon Al Wasim Aug 06 '12 at 08:29
  • I actually never heard of assertion. assertTrue() sounds like a function, but without it's code, I have no idea what you want to do. It should be pointed out (should you not be aware of this) that javascript is a PROTOTYPAL language NOT a Class based language like java or C. As I understand there are different uses of the concept assertion, so that does not help in understanding what you want either. I did however find some explanation and a assertion-function to add to javascript that MIGHT help over [here](http://aymanh.com/9-javascript-tips-you-may-not-know/#assertion). Hope this helps! – GitaarLAB Aug 08 '12 at 16:02
0

My details answer is:

WebElement actual = (WebElement)jse.executeScript("return document.getElementById('widget_polarisCommunityInput_113_title');");
WebElement expected = (WebElement)jse.executeScript("return document.activeElement;");
assertEquals(actual, expected);
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176