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:
<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 0" url="visitCount.jsp"/>
<verifyTitle description="confirm correct page" regex="true" text="Visit Counter.*" />
<storeCookie name="JSESSIONID" property="sessid" />
<verifyCookie name="VISITCOUNT" text="1" />
<invoke description="session id shouldn't change, cookie-based counter should increment"
url="visitCount.jsp"/>
<verifyCookie name="JSESSIONID" text="#{sessid}" />
<verifyCookie name="VISITCOUNT" text="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.
<config ...>
<header name="Cookie" value="visitCount=100"/>
</config>
<steps>
<invoke description="expect cookie-based counter to be 100" url="visitCount.jsp"/>
<verifyTitle description="confirm correct page" regex="true" text="Visit Counter.*" />
<verifyCookie description="cookie-based counter should have been incremented from preset value"
name="visitCount" text="101" />
</steps>
</webtest>
See also: the verifyCookie step.