12

I'm using selenium 2.24 Firefox Driver to test an input box's blur event. Currently, after I sendKeys to an input box, I let selenium to click another area which triggers the input box blur.

However, I think it is not a good way, anyone knows a better way to test this?

Many thanks.

enkicoma
  • 461
  • 4
  • 25
Mike
  • 3,515
  • 10
  • 44
  • 67

5 Answers5

7

Some elements do not have a suitable discriminator for testing purposes (e.g. an id or another CSS selector that you'd like to be bound to in your tests).

Fortunately, the notion of an activeElement exisits. So, if the element's blur functionality is what you wish to test, a native javascript way (not reliant upon jQuery or similar) to test this is:

driver.ExecuteScript("!!document.activeElement ? document.activeElement.blur() : 0");
Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
6

The selenium WebDriver does not properly trigger events like blur. You can, however, manually trigger them. Assuming that you're using jquery:

firefoxWebDriver.executeScript("$('#yourID').blur()");

Or without jquery:

firefoxWebDriver.executeScript("document.querySelector('#yourID').blur()");
Henry Woody
  • 14,024
  • 7
  • 39
  • 56
Aurand
  • 5,487
  • 1
  • 25
  • 35
5

I've made a lil investigation. I found out that fire event is not supported in selenium 2.0. See details. So this piece of code worked for me:

 driver.get("http://www.onliner.by/");

        String cssSelctr= "div.b-top-search-box input[id=\"g-search-input\"]";
        WebElement testElement=driver.findElement(By.cssSelector(cssSelctr));
        testElement.sendKeys("fvsdfs");

        JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssSelctr+"\');");
        stringBuilder.append("x.blur();");
        js.executeScript(stringBuilder.toString());

Hope now this helps you)

Community
  • 1
  • 1
eugene.polschikov
  • 7,254
  • 2
  • 31
  • 44
  • unfortunately using the method will lead to exception: ........... at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:188) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:472) at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:381) at org.openqa.selenium.internal.seleniumemulation.JavascriptLibrary.callEmbeddedHtmlUtils(JavascriptLibrary.java:82) – Mike Sep 10 '12 at 13:47
1

Thanks eugene.polschikov. I had to change the function to call triggerHandler to make it work for me. See the following - just replace "element-id" with the id of your element.

JavascriptExecutor jsexec = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $('" + element-id + "');");
stringBuilder.append("x.triggerHandler('blur');");
jsexec.executeScript(stringBuilder.toString());

Hope it helps.

Rory Hunter
  • 3,425
  • 1
  • 14
  • 16
Ahmegen
  • 9
  • 1
0

Replace "elementId" with relevant element ID. It works fine for me. "driver" is your Selenium driver

JavascriptExecutor jsexec = (JavascriptExecutor) driver; jsexec.executeScript("document.getElementById('" + elementId +"').onblur();");

Chamiz
  • 54
  • 5