Additional User Gestures

Context:

There are often times when we might have needs for additional user gestures to perform. The operations/gestures that we have focused in Basic Tutorial was setting text, click, get text et al. What about gestures like mouse hover, right click, arrow down, enter etc?

The following user gestures will give you an idea of what can be done. Of course we can replicate all the actions possible on a Keyboard and Mouse pretty much through Selenium/Watir. Barring a few exceptional scenarios (mainly because of browser behavioral differences), all keyboard and mouse interactions should be possible to perform. Below are some of them I believe would be helpful.

Relevance to Automation:

Mouse over is a user gesture we see on many websites especially on top menus and sub menus open up once we hover on the main menu item. On websites with lot of user interaction like drag, drop elements , right clicks etc, we need to at least know where to start with Selenium. There are some differences in behavior between browsers with the advanced user gestures, however there are ways to overcome those as long as we know that we can emulate a Keyboard and mouse.

Agenda:

  • Focus on key lines of code
  • Entire Cucumber Scenario
  • Output
  • Closing Thoughts

Mouse Hover

Any of the below lines of code can be used to hover on a mouse. The when_present(30) is a best practice to follow, otherwise not necessary.

Scroll Element into View

Selenium webdriver has intelligence built-in to scroll to the element we want to interact with, but just in case we want to explicitly scroll element into view, we can fire the javascript that does scroll as below. The “true” means align with top of the window. As you might have guessed, ‘false’ means align with bottom of the window.

Scroll Window By

We can scroll window by a certain # of lines either in horizontal or vertical direction (think of horizontal and vertical scroll bars). However this works only if the scroll bars are visible. The below scrolls the window by 5 pixels down.

Right Click (Context Click)

Watir has abstracted nicely the context click and provided right on the element method as below

Arrow down and enter

These are pretty much special keys. There is a complete list of special keys that can be send to the browser. I found that right-click and arrow down is something we might use at times (I found right click and arrow down to be working on firefox much better than other browsers. This quirk belongs to the list I alluded to at the beginning of this page)

Double Click

Sometimes you might have to double click on a Web Element.

Entire Cucumber Scenario:

 

Step Definitions:

.

Output:

  • Each of the scenario description above is self explanatory in terms of what to expect when run please

Closing Thoughts:

I have NOT seen consistent behavior across browsers performing user gestures. For example right-click on Chrome yields different set of options than right-click on Firefox, hence the output is different. I will revisit some of the user gestures again in terms of how can we exactly emulate Keyboard and mouse. At this point, I am inclined to believe that Keyboard, mouse are shared resources across Browser, operating system and so on. So when there is a conflict for the shared resource, which controller takes precedence and we will have to emulate that controller to exactly see what we expect.