Canoo Webtest WebTest Canoo

Extension Step storeRandom

Description

Provides the ability to store a random number, string or token value for later processing. Useful to avoid setting (and maintaining) large numbers of properties containing test data when specific test values aren't important.

Maintaining test data can be one of the biggest challenges with keeping a test suite relevant and not too brittle as environments, requirements and external factors change. Random data provides another valuable tool to assist in maintaining test data in complex systems.

Care must be exercised when using random data. If selecting test data by hand, careful choices can be made to ensure that boundary cases are covered by tests. Using purely random data may make this harder to do. Having said that, random data can simplify build scripts, can reduce the risk that poor test cases are chosen and lends itself to being suitable for load or stress testing with different data in multiple test runs.

Parameters

chars
Required? no, default is the upper and lowercase alphabets plus numbers plus spaces
Used when storing a random string. The set of characters to choose from when creating the random string. Ignored unless length is set.
choice
Required? yes/no
Used when storing a random token. The comma delimited choice of tokens to randomly select between. Parameters from, to and length must be empty.
description
Required? no
The description of this test step.
from
Required? yes/no
Used when storing a random number. The random number stored will be greater than or equal to this number. Required if to is set. Parameters length and choice must be empty.
length
Required? yes/no
Used when storing a random string. The length of the random string to store (i.e. the number of characters to randomly select). Parameters from, to and choice must be empty.
property
Required? no
The name of the property in which to store the retrieved value.
propertyType
Required? no, default is the "defaultPropertyType" as specified in the "config" element is used.
The type of the property in which to store the retrieve value. Either "ant" or "dynamic".
to
Required? yes/no
Used when storing a random number. The random number stored will be less than or equal to this number. Required if from is set. Parameters length and choice must be empty.

Details

Consider the following JSP which allows user preference information to be gathered in a form and produces a subsequent page based on the choices made:

storeRandom JSP under test
<html>
<head>
    <title>Favourite preferences page</title>
</head>
<body>
<%
    String greeting="Hello " + request.getParameter("name");
    String fontStyle="font-family:" + request.getParameter("font");
    String colorStyle="color:" + request.getParameter("colour");
    String levelStr = request.getParameter("level");
    int level = 1;
    try {
        level = Integer.parseInt(levelStr);
        if (level < 1) level = 1;
        if (level > 6) level = 6;
    } catch (Exception e) {
        // ignore gives default level of one
    }
    String headingTag="h" + level;
%>
<%= "<" + headingTag + " id='heading' style='" + colorStyle + "; "
        + fontStyle + "'>" + greeting + "</" + headingTag + ">" %>
<form method="postaction="favourite.jsp">
    <input type="hiddenname="hiddenLevelvalue="<%= level %>">
    <label for="name">Enter your name</label>
    <input id="namename="nametype="textsize="10"><br/>
    <label for="colour">Enter your favourite colour</label>
    <select id="colourname="colour">
        <option>&lt;Select Colour&gt;</option>
        <option value="red"/>Red
        <option value="green"/>Green
        <option value="blue"/>Blue
        <option value="black"/>Black
    </select><br/>
    <label for="font">Enter your favourite font</label>
    <select id="fontname="font">
        <option>&lt;Select Font&gt;</option>
        <option value="arial"/>Arial
        <option value="times roman"/>Times Roman
        <option value="courier"/>Courier
        <option value="helvetica"/>Helvetica
    </select><br/>
    <label for="level">Enter a number between 1 and 6</label>
    <input id="levelname="leveltype="textsize="3"><br/>
    <br/>
    <input type="submitname="fav_submitvalue="Submit">
</form>
</body>
</html>

If a field value in our form takes on one of several values, e.g. the colours in this example, we can randomly select a value to use from that list of tokens:

storeRandom token Example
<steps>
    <invoke ...>
    <-- random colour using tokens -->
    <storeRandom description="choose colourproperty="expectedColourchoice="red,green,blue,black"/>
    <setSelectField name="colourvalue="#{expectedColour}"/>
    <-- additonal steps ... -->
</steps>

Here is an example which selects a random number for a field expecting a number between 1 and 6 (corresponding to setting the heading level to be one of "H1" through "H6"). Note that we also allow some numbers outside the desired range to test that server validation is working (in our examples it rounds to the nearest valid value):

storeRandom number Example
<steps>
    <-- previous steps ... -->
    <-- random level including out of range values to test server validation -->
    <storeRandom description="choose levelproperty="levelfrom="0to="9"/>
    <setInputField name="levelvalue="#{level}"/>
    <-- additonal steps ... -->
</steps>

We can combine XPath with random selection to perform very powerful operations, e.g. in this example we randomly select one of the "option" values from a "select" element in the HTML form (note that in this example we skip over the empty option value by starting the random number at "2"):

storeRandom tricky number and XPath Example
<steps>
    <-- previous steps ... -->
    <-- random font extracted from dropdown select list -->
    <storeXPath property="numselxpath="count(//select[@name='font']/option)"/>
    <storeRandom property="fontIndexfrom="2to="#{numsel}"/>
    <storeXPath property="expectedFontxpath="//select[@name='font']/option[#{fontIndex}]/@value"/>
    <setSelectField name="fontvalue="#{expectedFont}"/>
    <-- additonal steps ... -->
</steps>

In this final example, we generate random first and last names. Each name starts with a random uppercase character, and is followed by a random number of random lowercase characters:

storeRandom string Example
<steps>
    <-- previous steps ... -->
    <-- random two-word name (firstname up to 10 chars, lastname up to 20 chars) -->
    <storeRandom description="first name first letterproperty="firstStartchars="${UCCHARS}length="1"/>
    <storeRandom description="first name num remaining charsproperty="firstSizeRestfrom="3to="9"/>
    <storeRandom description="first name remaining charsproperty="firstRestchars="${LCCHARS}length="#{firstSizeRest}"/>
    <storeRandom description="last name first letterproperty="lastStartchars="${UCCHARS}length="1"/>
    <storeRandom description="last name num remaining charsproperty="lastSizeRestfrom="3to="19"/>
    <storeRandom description="last name remaining charsproperty="lastRestchars="${LCCHARS}length="#{lastSizeRest}"/>
    <storeProperty name="expectedNamevalue="#{firstStart}#{firstRest} #{lastStart}#{lastRest}" />
    <setInputField name="namevalue="#{expectedName}"/>
    <-- additonal steps ... -->
</steps>

The full example (found in storeRandom.xml in the selftests) the test then goes on to submit the form (by clicking its button) which causes a new page to be created based on the preferences chosen in the form. The form is then tested to ensure the actual attributes used to generate the form match the above randomly generated expected values.

news

Latest build: development
Posted: 19-Jul-2016 17:36

WebTest 3.0 released, featuring upgrades to Java 5, Groovy 1.6, and HtmlUnit 2.4.
The release includes support for maven integration, IDE-integration like for unit tests, capturing of background JavaScript errors, new steps for mouseOver and mouseOut events, better parallel execution of tests and - as usual - lots of handling improvements.
Posted: 5 March 2009

WebTest @ JavaOne
Dierk König presented "Functional testing of web applications: scaling with Java" on Wed May 7, 13:30 at JavaOne 2008 in the Tools and Scripting Languages track.
Posted: 8 May 2008

New WebTest screencast available:
Data Driven WebTest
Posted: 13 November 2007

First WebTest screencast available:
Creating a first Webtest Project

Extend WebTest with Groovy! Groovy in Action is available in every good bookstore.
Groovy in Action
Posted: 29 January 2007