I'm working in an cookieless application. A cookieless application cannot store the SessionID in a cookie, and it is stored in the Url, so when a user request the first page in the application, the server redirects the user to a url with this SessionID in it.
When we capture a session to do a web test (see previous post here), we capture also the SessionID, and this ID will be in hardcode in all requests. So we need to take the new SessionID in the first request and replace it in all pending requests.
To achieve this, we need to create a CustomExtractionRule, creating a new class inheriting ExtractionRule:
1: using System;
2: using System.Collections.Generic;
3: using System.Text;
4: using Microsoft.VisualStudio.TestTools.WebTesting;
5:
6: namespace WebTest.Funcional
7: {
8: public class ExtractCookielessSessionId : ExtractionRule
9: {
10: public override void Extract(object sender, ExtractionEventArgs e)
11: {
12: if (!e.Response.IsHtml)
13: {
14: e.Success = false;
15: e.Message = "The response did not contain HTML";
16: }
17:
18: //Get the cookieless SessionID
19: string beginSessionId = "(S(";
20: string endSessionId = "))";
21: string url = e.Response.ResponseUri.AbsoluteUri;
22: int inicioSID = url.IndexOf(beginSessionId);
23: int finSID = url.IndexOf(endSessionId);
24:
25: if (inicioSID >= 0 && finSID > inicioSID)
26: {
27: string sessionId = url.Substring(inicioSID, finSID - inicioSID + endSessionId.Length);
28: e.WebTest.Context.Add(this.ContextParameterName, sessionId);
29: }
30:
31: }
32:
33: public override string RuleName
34: {
35: get { return "ExtractCookielessSessionId"; }
36: }
37:
38: public override string RuleDescription
39: {
40: get { return "Extracts Cookieless Session Id"; }
41: }
42: }
43: }
When we have this class, we have to compile our test project. Then we add a new extraction rule to our first request, giving a value SESSION to it's Context Parameter Name (Extracting to SEESION context parameter the SessionID of the request).
Then, we just need to replace the SessionID with the value {{SESSION}} in the requests:
Now, when we run the web test, we can see a new SessionID for each test instead of reusing the captured SessionID.