Webtest WebTest GitHub Home

Core Step storeCookie

Description

Provides the ability to store an HTTP Cookie value for later checking.

Parameters

name
Required? yes
The name of the cookie of interest. If the property name is not specified, the cookie name is used as key to store the value found.
description
Required? no
The description of this test step.
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".

Details

Suppose that we have created a JSP called visitCount.jsp which increments a cookie called VISITCOUNT each time we visit the page. The following test code can test the cookie usage of this JSP:

storeCookie Example1
<webtest name="cookie: visit count test example">
    <config ...>
    <steps>
        <invoke description="reset in case someone else has used this page"
            url="visitCount.jsp?reset=true"/>
        <invoke description="expect cookie-based counter to be 0url="visitCount.jsp"/>
        <verifyTitle description="confirm correct pageregex="truetext="Visit Counter.*" />
        <storeCookie name="JSESSIONIDproperty="sessid" />
        <verifyCookie name="VISITCOUNTtext="1" />
        <invoke description="session id shouldn't change, cookie-based counter should increment"
            url="visitCount.jsp"/>
        <verifyCookie name="JSESSIONIDtext="#{sessid}" />
        <verifyCookie name="VISITCOUNTtext="2" />
    </steps>
</webtest>

Cookie usage is as follows:

The JSESSIONID cookie is used by Java web servers to manage user sessions and should be unique between visitors.
The VISITCOUNT cookie is used to track unique hits to the site and should increment after each invoke.

Browsers store cookie values away persistently (except for transient cookies - but that is another story). Canoo WebTest doesn't support persistence of cookies (although you could simulate that using ANT functionality). What is possible however without further ANT tasks is the ability to set an initial cookie value. This lets you partially simulate a persistent cookie. This is done by using the header nested element within the config section of your test. Here is an example using verifyCookie and an initial cookie value for the above example.

storeCookie Example2
<webtest name="cookie: visit count test example">
    <config ...>
        <header name="Cookievalue="visitCount=100"/>
    </config>
    <steps>
        <invoke description="expect cookie-based counter to be 100url="visitCount.jsp"/>
        <verifyTitle description="confirm correct pageregex="truetext="Visit Counter.*" />
        <verifyCookie description="cookie-based counter should have been incremented from preset value"
            name="visitCounttext="101" />
    </steps>
</webtest>

See also: the verifyCookie step.