Adding Javascript to Scripts

Using JScript

Badboy can execute Javascript (or JScript) against the visible Browser window as part of your tests. This can be a very useful technique in some cases where you need to do advanced operations or where other mechanisms for playing back aren't sufficient to automate your web site correctly.

Adding JScript Items to your Script

You can add JScript to your tests by right-clicking in the Script tree on a Step or Request and selecting Add-> JScript or Insert->JScript.

You can also add JScript items to your script by dragging them from the Toolbox (press Ctrl-T to show the Toolbox) and dropping them into your script at the point where you would like the javascript to be executed

Editing JScript Properties

When adding a JScript item or if you edit it's properties, you will be shown a dialog where you can enter the JScript for Badboy to run when it plays the given item:

Badboy supports auto-completion for properties and functions in the Browser window. Press Ctrl-Space to see the available properties displayed when you are creating your Javascript.

You can also choose the frame that the Javascript should run in. If your web site has only one frame or you want the script to run in the top frame then you can just leave this box empty. If, however, there are child frames in the page then it can be convenient and occasionally essential to comply with browser security constraints to run the script in the context of the correct frame.

Plugin Priviliges

Badboy offers additional features and functions to Badboy Plugins. However you don't need to write a plugin to access these special functions: you can make it possible by checking the box marked "Allow Access to Plugin Privileges". If you do this then your script can access the plugin API by creating a "plugin" object as follows:

Example 1:

  var plugin = badboy.plugin();
  alert("Badboy is configured to use " + plugin.getPreference("Undo Levels") + " undo levels");
  

It is important to be mindful of security: Badboy's plugin API can be used to modify your script and could even be manipulated to launch malicious programs on your computer. Therefore you should make sure you do not set references to the "plugin" object outside of your script in case they might be accessed by web pages you do not trust.

Advanced JScript

JScript can be used to perform advanced operations and thereby extend the functionality of Badboy. This is especially true if you change your security options so that JScript can use ActiveX controls. Here is an example JScript that will let you save a file to your local hard disk when it executes:

Example 2:

  var fso, textFile;
  var ForWriting = 2;
  fso = new ActiveXObject("Scripting.FileSystemObject");
  textFile = fso.OpenTextFile("c:\\test.txt", ForWriting, true);
  textFile.Write ("This is a test.");
  textFile.Close();
  

This script has a difficulty: in order for it to work you must set your Internet Explorer security level to Low for the site that you are accessing so that ActiveX controls can run and access the file system. Otherwise you will get security warnings shown, or it might be simply refused by the browser. To get around this you can use the Badboy Plugin API instead to create the ActiveX object:

Example 3:

  var fso, textFile;
  var ForWriting = 2;
  fso = badboy.plugin.createObject("Scripting.FileSystemObject");
  textFile = fso.OpenTextFile("c:\\test.txt", ForWriting, true);
  textFile.Write ("This is a test.");
  textFile.Close();
  

For this to work you must check the box marked "Allow access to plugin privileges" in your JScript item.

If you are a programmer, you can even write automation objects of your own using your favorite language and access them in the same way.

For more information on controlling Badboy with JScript, see Automating Badboy with OLE. You can call any of these OLE function from JScript itself and thereby control Badboy itself as part of your scripts.


Badboy Documentation Generated on Mon Dec 29 22:28:41 EST 2008