Using Ranorex Webtestit Snippets


#1

We are building Ranorex Webtestit to speed you up. When putting together your page objects, tests or even just helper classes, you do not want to write the same code over and over again. To assist you with this, Ranorex Webtestit comes with a plethora of code snippets, and in this article, we will show you how to use some of them and find more.

This is part three of a series about working with the keyboard.
See Working with the keyboard - Actions for part one
and Working with the keyboard - Navigating files for part two.

Note: All keyboard shortcuts specified in this article assume the default settings. Keybindings can be changed via Help → Keyboard shortcuts.

Introduction to snippets

Snippets are (often small) pieces of code likely to be used multiple times throughout a project. For example, when you want to create a test, you will want to instantiate a Page Object instance every time. Or for Page Objects, you need to create new actions and populate them with steps. Snippets take away the boring work of memorizing and repeatedly typing out the same code (or copy-pasting it :wink: ).

Let’s have a look at some examples:

Creating actions and using elements

In Page Object files, you will want to create page object actions and populate them with steps (such as “click” or “hover”). While you can use our drag and drop functionality for this, you could save some time using snippets.

To create a new action, start typing somewhere in the class body newAction and find the corresponding snippet in the suggestions. The keyboard shortcut Ctrl+I →Ctrl+A (⌘I → ⌘A on Mac) works as well.

To use elements inside actions, start typing the name of the element you want to use, within an action method. For example, if you want to “click” on an element called checkOutButton, type checkOutButton_click and the snippet should appear in your auto-completion suggestions.

Create test stubs and instantiate page objects

In Test files, you will create lots of tests. Just place your cursor inside the test class’s body (Java) or into a describe-block (TypeScript), and start typing newTest. Alternatively, you can use the keyboard shortcut Ctrl+I → Ctrl+T (⌘I → ⌘T on Mac).

Once the test stub is created, you can type initPo inside. This snippet will create an instance of the Page Object class of your choice, and call the “open” function with your specified URL. The keyboard shortcut Ctrl+I → Ctrl+P (⌘I → ⌘P on Mac) does the same thing.

Generic code snippets

If you have worked with other IDEs, you expect to have certain snippets available regardless of context. Things like try/catch, if/else or various types of loops. Of course, Ranorex Webtestit has you covered. Just start typing a keyword, such as trycatch, or ifelse, and keep your eyes open for auto-completion suggestions with this icon: snippet-icon.

“Fill out” your snippets

You might have noticed that freshly inserted snippet, has some parts highlighted. Welcome to the interactive mode of the snippet. While in this mode, it lets you tab through these boxes (Shift+Tab to move backward), to fill out the snippet like a form.

With this functionality, it should be a breeze to insert the same snippet multiple times in a row, just with different values each time. Here is a short example of using snippets in Ranorex Webtestit:

Explore snippets with the context menu and auto-completion

These are just some examples for snippets that you can find in Ranorex Webtestit. We are constantly adding more useful snippets like these. As a rule of thumb, you can find some of them in the right-click context menu, and even more in the auto-completion suggestions, accessible through Ctrl+. (⌘. on Mac).

Conclusion

After reading this article you are familiar with snippets, how to use them and how to find them. This should help you be more efficient in writing your page objects and tests.

If you haven’t already, check out part one, Working with the Keyboard - Actions, to learn about the F1 Quick Command bar and customizing your keyboard shortcuts. You might be interested in part two, Working with the keyboard - Navigating files.


Working with the keyboard - Actions
Release notes v1.0.0
How to perform the Key-down and Move to element (hover over element) Action
listed #2

Using Ranorex Webtestit quick-fixes