//

Selenium – Last Resort

14.12.2009 | 2 minutes of reading time

Everyone working with Selenium probably knows some problems when testing “complicated” web sites. Complicated here often means extensive use of JavaScript. For such web sites tests recorded with the Selenium IDE cannot be executed successfully. But what is worse: Sometimes one can simply not find a proper XPath oder DOM expression to perform some action on an element. For those cases simulating a “native keystroke” could be the last resort indeed.

The following example is utilizing the Selenium-Library of the Robot Framework as this results in a very readable example. Of course the same approach could also be used with other Selenium-based libraries. Here a search is performed using Google, where the “Search” button is not clicked the normal (Selenium) way – which would be perfectly possible of course – but the button is clicked using some native keystrokes.

First of all the text to search for is entered to the input field. Assuming the “Search” button cannot be used otherwise, the input field is then first focused and then pressing the TAB key is simulated with Press Key Native 9. Now the “Search” button is focused and the button is then “pressed” by simulating that the RETURN key is pressed. This submits the search query.

It is extremly helpful if there is some known element on the page that can be focused and which can then be used as a starting point for simulating further key events. On the other hand some element always has the focus initially and thus using TAB every element should be accessible on the page. Of course this could get quite ugly. But as usually there are some elements that can be located directly there should be rarely a need using such a brute force approach. The used key codes are defined in java.awt.event.KeyEvent .

Simulating native keystrokes has some quite obvious drawbacks and should really only be used when everything else has failed. But then it could be indeed a kind of “last resort”.

share post

Likes

0

//

More articles in this subject area\n

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen

Wir helfen Deinem Unternehmen

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.