How to Test Blur - Firefox Selenium driver?

Mike picture Mike · Sep 9, 2012 · Viewed 21.7k times · Source

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.

Answer

eugene.polschikov picture eugene.polschikov · Sep 9, 2012

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)