Canoo Webtest WebTest Canoo

Extension Step clickElement

Description

Clicks an HTML element identified by its id or xpath. If the click triggers the load of a new page, then this page becomes the current one.

Parameters

description
Required? no
The description of this test step.
htmlId
Required? yes/no
The id of the html element to click on. One of htmlId or xPath must be set.
password
Required? no
A password that can be provided for pages that require basic authentication. Required if username is specified.
save
Required? no
A shorthand: save='prefixName' is the same as savePrefix='prefixName' saveResponse='true'.
savePrefix
Required? no, default is the 'savePrefix' parameter as specified in <config>.
A name prefix can be specified for making a permanent copy of received responses. A unique number and the file extension (depending on the MIME-Type) will be appended. The resultpath attribute of the <config> element is used for determining the location of the saved result.
saveResponse
Required? no
Whether to make a permanent copy of received responses. Overrides the default value set in the <config> element.
username
Required? no
A username that can be provided for pages that require basic authentication. Typically only needed on the first step of any webtest accessing a secure site. Required if password is specified.
xpath
Required? yes/no
The XPath identifying the html element to click on. One of htmlId or xPath must be set.

Details

Consider the following HTML page:

clickElement HTML
<html>
<head>
<script type="text/JavaScript">
<--
function flipColor(obj) {
  var color=(obj.style.backgroundColor==obj.id) ? "yellow" : obj.id;
  obj.style.backgroundColor=color;
}
// -->
</script>
</head>
<body>
<table cellspacing="1border="1">
    <tr style="backgroundColor:red;background:redid="redonclick="flipColor(this)">
        <td width="150height="30"></td><td width="150"></td><td width="150"></td>
    </tr>
    <tr style="backgroundColor:green;background:greenid="greenonclick="flipColor(this)">
        <td width="150height="30"></td><td width="150"></td><td width="150"></td>
    </tr>
    <tr style="backgroundColor:blue;background:blueid="blueonclick="flipColor(this)">
        <td width="150height="30"></td><td width="150"></td><td width="150"></td>
    </tr>
</table>
</body>
</html>

The page contains no links or buttons but we want to be able to activate and check the onclick JavaScript. We can do this using the <clickElement> step as follows:

clickElement Example
<webtest name="clickElement: Click Table Cell">
  <config ... />
    <steps>
        <invoke description="starturl="colorChange.html"/>
        <-- first row tests -->
        <storeElementAttribute htmlId="redattributeName="stylepropertyName="colorBefore1"/>
        <verifyProperty name="colorBefore1text=".*backgroundColor:[ ]*red.*regex="true"/>
        <clickElement description="toggle color of first rowhtmlId="red"/>
        <storeElementAttribute htmlId="redattributeName="stylepropertyName="colorAfter1"/>
        <verifyProperty name="colorAfter1text=".*backgroundColor:[ ]*yellow.*regex="true"/>
        <clickElement description="toggle color of first row backhtmlId="red"/>
        <storeElementAttribute htmlId="redattributeName="stylepropertyName="colorFinal1"/>
        <verifyProperty name="colorFinal1text=".*backgroundColor:[ ]*red.*regex="true"/>
        <-- second row tests -->
        <storeElementAttribute xpath="//tr[2]attributeName="stylepropertyName="colorBefore2"/>
        <verifyProperty name="colorBefore2text=".*backgroundColor:[ ]*green.*regex="true"/>
        <clickElement description="toggle color of second rowxpath="//tr[2]"/>
        <storeElementAttribute xpath="//tr[2]attributeName="stylepropertyName="colorAfter2"/>
        <verifyProperty name="colorAfter2text=".*backgroundColor:[ ]*yellow.*regex="true"/>
    </steps>
</webtest>

See also: the storeElementAttribute step.

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