How to emulate a blur event in WebDriver?

18,316 views
Skip to first unread message

Hava Edelstein

unread,
Sep 29, 2012, 8:31:52 PM9/29/12
to seleniu...@googlegroups.com
In the IDE, I use this line:
fireEvent | [element] | blur

"fireEvent" is no longer supported in Selenium 2. I know there's TypeKeys, but to my understanding, this doesn't simulate what happens if the user leaves the field by manually clicking in another field with the mouse.

I'm using Java.

Any ideas?

Mark Collin

unread,
Sep 30, 2012, 4:05:12 AM9/30/12
to seleniu...@googlegroups.com

You can fire events in Selenium 2:

 

WebElement element = driver.findElement(By.id("myElement"));

JavascriptLibrary javascript = new JavascriptLibrary();

javascript.callEmbeddedSelenium(driver, "triggerEvent", element, "blur");

 

You can also execute any JavaScript you want to execute:

 

JavascriptExecutor jsexec = (JavascriptExecutor) driver;

jsexec.executeScript("return alert('Hello world!');");

 

The only thing you need to remember when using executeScript is that it must return something.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/teqVzsKml7wJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Daniel Wagner-Hall

unread,
Sep 30, 2012, 3:05:30 PM9/30/12
to seleniu...@googlegroups.com
If you're aiming to simulate what happens when a user leaves a field by manually clicking in another field with the mouse, that's exactly what you should do.

Assuming the field with ID someField currently has focus:

driver.findElement(By.id("someOtherField")).click()

will fire all of the relevant events on someField, and then focus someOtherField.

Alternatively, you could (again, assuming someField already has focus), do something like press the tab key:

driver.switchTo().activeElement().sendKeys(Key.TAB)

Manually firing events is a worse and more fiddly way to test than just doing what a user would do :)

David

unread,
Sep 30, 2012, 5:00:26 PM9/30/12
to seleniu...@googlegroups.com
Is the JavascriptLibrary a new feature of Selenium 2? Never heard of it, only JavascriptExecutor. If it's been there all along, must be something not well publicized compared to JavascriptExecutor.

Peter Gale

unread,
Sep 30, 2012, 5:06:45 PM9/30/12
to Selenium Users
Daniel

Interesting ... but (using your example) surely clicking on the field with ID someField should at least trigger the onclick event of that field, rather than having to click some other field?

I can see that many events, such as "blur" might not trigger until some other field gets the focus.

Peter


Date: Sun, 30 Sep 2012 12:05:30 -0700
From: dawa...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: How to emulate a blur event in WebDriver?
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/oQ1jjd_n-RMJ.

Mark Collin

unread,
Sep 30, 2012, 6:35:57 PM9/30/12
to seleniu...@googlegroups.com

It’s been around as long as I can remember, a least a year? (Unless my memory is going with age…)

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/O-MVFW-0nWQJ.

Jim Evans

unread,
Oct 1, 2012, 3:10:29 AM10/1/12
to seleniu...@googlegroups.com
The "JavascriptLibrary" is not part of WebDriver. It's used as part of the WebDriverBackedSelenium class, so yes, it's been around awhile, but it's not part of WebDriver proper. It ostensibly contains the Selenium RC JavaScript code, but maintenance will be an issue given that not much effort is going into RC.

Daniel Wagner-Hall

unread,
Oct 1, 2012, 6:16:24 PM10/1/12
to seleniu...@googlegroups.com
On Sunday, 30 September 2012 22:06:58 UTC+1, PeterJef...@Hotmail.co.uk wrote:
Daniel

Interesting ... but (using your example) surely clicking on the field with ID someField should at least trigger the onclick event of that field, rather than having to click some other field?

I can see that many events, such as "blur" might not trigger until some other field gets the focus.

Peter
 
Sure, but again, what will a user do? Maybe you could click on the body tag, or a header, or some text, or press the tab button.  But a user can't "blur" a text field, so it doesn't make a lot of sense for your test to do so.  Clicking on a text box necessarily blurs the currently focused element (unless it happens to be the same one as the one currently focused)

Simon Stewart

unread,
Oct 3, 2012, 11:48:41 AM10/3/12
to seleniu...@googlegroups.com
It's an implementation detail of the webdriver-backed selenium (which
is why it's in an "internal" package). I'd really not rely on it as
it's liable to change without notice.

Simon

Gopi Krishna Kancharla

unread,
Oct 11, 2013, 11:55:34 AM10/11/13
to seleniu...@googlegroups.com
Thank you Mark. This example was really useful.

Thanks
...Gopi
www.allibilli.com

David

unread,
Oct 14, 2013, 2:49:59 PM10/14/13
to seleniu...@googlegroups.com
A response to Mark's earlier post that I didn't look through in detail last time around.

  1. I'm thinking one could also execute the javascript equivalent of blur event using JavascriptExecutor rather than the JavascriptLibrary. It's more javascript code to deal with in writing the execution code, but it would be the same as executing code that developers write in javascript to trigger blur event in an application. It would be less dependent on the internals of Selenium to do the same thing. Here's some example references (though not Selenium specific, sadly most examples on the web base off jQuery rather than native javascript):

    http://www.cristinawithout.com/content/function-trigger-events-javascript

    http://stackoverflow.com/questions/5212651/blur-vs-onblur

  2. JavascriptExecutor I believe doesn't require you to return anything. So you don't actually have to add a "return" keyword to the javascript being executed. You only need that if you are expecting a return value. Unlike Selenium RC's getEval() method, the JavascriptExecutor won't return anything by default unless you have the return keyword prepended. But if you're just executing code to do something and not care about a return value, it's kind of pointless to add.

On Sunday, September 30, 2012 1:05:48 AM UTC-7, Mark Collin wrote:
Reply all
Reply to author
Forward
0 new messages