Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Test Blur - Firefox Selenium driver?

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.

like image 767
Mike Avatar asked Sep 09 '12 06:09

Mike


People also ask

How do I run Selenium test in Firefox?

Step 1: Navigate to the official Selenium website. Under third-party drivers, one will find all the drivers. Just click on the Mozilla GeckoDriver documentation as shown below. Now, it will navigate to the GeckoDriver downloads link, where one can download the suitable driver based on the OS as it is platform agnostic.

Which driver is used for Firefox automation?

Marionette is an automation driver for Mozilla's Gecko engine. It can remotely control either the UI or the internal JavaScript of a Gecko platform, such as Firefox.

How do I download Geckodriver for Firefox?

Step 1: Selenium FirefoxDriver or Selenium GeckoDriver can be downloaded from the official GitHub repository of Mozilla. Go to the link and scroll towards the bottom of the page. Open the Assets menu and download the Selenium FirefoxDriver respective to your operating system. Step 2: Extract the downloaded file.

How do I check my Firefox driver version?

Next, go to https://firefox-source-docs.mozilla.org/testing/geckodriver/geckodriver/Support.html and check for required Firefox version supported by Selenium and Gecko driver: In my case, Selenium Webdriver version is 3.9. 1, so the supported Firefox browser is version 55+ and gecko driver supported is version 0.20. 1.


2 Answers

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()");
like image 129
Aurand Avatar answered Sep 29 '22 10:09

Aurand


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");
like image 29
Rich O'Kelly Avatar answered Sep 29 '22 11:09

Rich O'Kelly