How to Test Blur - Firefox Selenium driver? How to Test Blur - Firefox Selenium driver? selenium selenium

How to Test Blur - Firefox Selenium driver?


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");


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()");


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)