Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Firefox to be the foreground application when testing with Selenium on Mac OS X

We recently started testing our Django-based application using Selenium. Tests run fine on Linux, but some fail on Mac OS X. We're using Firefox as the browser in both cases, and it took us a while (and some luck) to figure out that the important difference between the two cases is whether FF is running as the foreground application or as a background window. Here's what happens on Linux:

  1. Selenium tells Firefox to go to a user registration page.
  2. Selenium fills in the user name and an invalid email address.
  3. Selenium changes focus from the email address field to another field on the same page (it happens to be the search box, but that's not important---all that matters is the change-of-focus event).
  4. The Javascript validation code in the web page notices that the email address is invalid and inserts an error message in the page.
  5. Selenium detects that error message and marks the test as passed.

When we run on Mac OS X, however, step #4 doesn't happen unless we manually foreground the Firefox window while the test is running---if we leave it in the background (which is where it comes up by default), the Javascript in the browser doesn't get the change-of-focus event, so the error message is never inserted in the page's DOM, so Selenium times out waiting for it, and the test fails.

Can we force Selenium to foreground the browser when tests run, so that our Javascript will get events as we want it to? If so, how?

like image 705
Greg Wilson Avatar asked Oct 24 '11 20:10

Greg Wilson


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.


1 Answers

Selenium drives the browser through Javascript. There is no way to control browser window through JS. In general, testing the 'onfocus' and 'onblur' events in Selenium is very difficult.

You can try firing this event yourself by doing a JS evaluation using either the 'runScript' or the 'getEval' methods of Selenium after entering the invalid email address. This is not "pure" black box testing i.e. your test needs to be changed if the implementation of the validation is changed, but this is the most feasible way to carry out this test.

like image 64
Pavan Avatar answered Oct 07 '22 13:10

Pavan