Selenium driver: how to check blur?

I am using the selenium 2.24 firefox driver to check for input window blur events. Currently, after I sentKeys to the input field, I will let selenium click another area to cause the input window to blur, however I think this is not a good way, does anyone know a better way to check this?

Many thanks.

+6
source share
5 answers

I conducted an investigation. I found out that the fire event is not supported in selenium 2.0. See more 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 this helps you now)

+4
source

Selenium WebDriver does not fire events like blur correctly. However, you can manually start them. Assuming you are using jquery:

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

Thanks to Eugene. Polshchikov. I had to change the function to call triggerHandler so that it worked for me. See the following: just replace the "element-id" with the identifier 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 this helps.

+1
source

Some elements do not have a suitable discriminator for testing purposes (for example, an identifier or other CSS selector that you would like to bind to in your tests).

Fortunately, the concept of activeElement continues. So, if the element's blur function is what you want to test, your own javascript way (independent of jQuery or similar) to check this:

 driver.ExecuteScript("!!document.activeElement ? document.activeElement.blur() : 0"); 
+1
source

Replace "elementId" with the corresponding element identifier. It works great for me. "driver" is your Selenium driver.

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

0
source

Source: https://habr.com/ru/post/924942/


All Articles