Money, Grease Monkey

We, the testers(toolsmith as we like to call ourselves) are not far behind in terms of programming. We(the toolsmiths) are the new breed, its the dawn of a new species.

Getting to the point, we use various tools in our testing endeavors that I believe makes out lives a bit easy so to speak. But one of the most useful that I believe is worth mentioning is a Firefox extension called greasemonkey. Greasemonkey is a Firefox extension that allows you to write user scripts(JavaScript) that alter the web pages you visit. A user script is just a chunk of Javascript code, with some additional information that tells Greasemonkey where and when it should run. Each user script can target a specific page, a specific site, or a group of sites. A user script can do anything you can do in Javascript. In fact, it can do even more than that. A small code snippet is as follows:

// ==UserScript==
// @name MyFirstGreaseMonkey Script
// @namespace http://www.somewebsite.net/
// @description This code will show the name of the page being visited.
// @include https://www.somewebsite.com/*
// @include https://www.someotherwebsite.com/*
// ==/UserScript==

alert(document.title);

The above code snippet, as suggested between the ‘==/UserScript==’ tag will only take effect in pages that follows the keyword ‘@include’. It will essentially start executing the JavaScript code when the pages mentioned are visited. There are ways that will enable the script to run on all pages and also exclude some of the pages in which case the keyword will be ‘@exclude’ and then the URL.

How to install a user script? Simple. Install Greasemonkey for Firefox, create a js file and while naming the file a certain convention has to be followed and that is fileName.user.js. Right click on the file and open the file with Firefox. Then a window will appear that will ask the user to install the script for the pages mentioned in the script which will also appear in the window and voila!! You have your own user script installed.

This is particularly useful for testing purpose because we are constantly faced with challenges such as entering values in innumerable text fields over and over again. So we wrote our own automation user script that helps us identify text fields in a page and populate them with values of our own choice.

Advertisements

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