Testing Dojo web applications with Selenium

I jot down here some findings about testing Dojo web applications (with a lot of Dijit Widget!) using Selenium, just in case someone will need them.

The first thing that you should never ever try to do is simulating events using JavaScript. Some libraries like jQuery provide convenient ways to click, focus, or type on HTML elements. But these “synthetic” events have two important limitations:

  • You have to find the right element to send an event. The DOM trees generated by Widgets are huge, with layers under layers. Sometimes it is necessary to read the code and decide which element is used by Dojo to receive which kind of event. Moreover, synthetic events are bad as it will not be able to consistently updating the focused elements the way human interactions do, while Widgets heavily rely on this behavior. Some Widgets even refuse to react upon faux clicks.
  • They can be sent to invisible elements, while human obviously cannot manipulate with such things. Dijit Widgets consist of many HTML elements that are carefully arranged in different positions. Any wrongly placed element can render the Widget unusable, while JavaScript does not care about (and understand) position. Therefore, synthetic events are not reliable at all, which would render your tests useless.

Without JavaScript, we have two more effective ways to trigger actions on Widget. They both simulate human interactions in the most native way.

The first one is to utilize native events implemented by all major web browsers. This approach should work well unless you encounter a bug in their implementation, or you are testing against an unpopular browser. It is quite straightforward to enable native events:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“nativeEvents”, true);

The second one, which will work for edge cases, is to use a robot. A robot can simulate human action as Operating System level. This means if the robot is trying to move the mouse, you will see on your screen that the pointer is moving. The drawback is that the executing machine needs to have a display device regardless of being a physical machine or a virtual one (in another word, a headless machine will not work). Another disadvantage is that you have to convert coordinates from window-relative into device-relative. To overcome this limitation, just run the browser in full-screen mode. For example, with Chrome, we use “kiosk” command switch for full-screen mode:

ChromeOptions options = new ChromeOptions();

And we control the robot like this:

Point location = input.getLocation();
int x = location.getX();
int y = location.getY();
Robot r = new Robot();
r.mouseMove(x + 10, y + 10);

Finally, Dijit Widget has a bug with touch event (Dojo manipulate browser’s events a lot, it even managing it own focusing behavior so it is really prone to bugs and browser’s compatibility. Make sure you have latest fixes and if things still happen, disable touch events in Chrome.

  1. From Google Chrome, type chrome://flags in the address bar, then press Enter.
  2. Scroll down to the “Enable Touch Events”.
  3. Select the drop-down menu and select Disabled.
  4. Restart and enjoy Chrome!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s