views:

495

answers:

3

I am trying to parse a multi-value cookie using the Selenium IDE. I have this as my Tracking Cookie Value: G=1&GS=2&UXD=MY8675309=&CC=234&SC=3535&CIC=2724624

So far I have simply captured the full cookie into a Selenium variable with the standard StoreCookieByName command:

storeCookieByName Tracking Tracking

However I want to get a particular sub element of the cookie for my test, such as the UXD value of MY8675309.

I have tried using Javascript to parse and all but have had no luck with it and the StoreCookieByName value.

Any help would be appreciated.

+1  A: 

If the Tracking Cookie Value is a string, then:

var subElements = cookieString.split("&");
var UXDValue = subElements[2].substring(4);
pianoman
From Tim: Thanks pianoman. I see how that works with a defined array position and sub-value variable length but I was hoping for something more dynamic that I could request any cookie sub-value by name and have it return its value as a string. Thanks.
pianoman
+1  A: 

Thanks pianoman. I see how that works with a defined array position and sub-value variable length but I was hoping for something more dynamic that I could request any cookie sub-value by name and have it return its value as a string. Thanks.

+1  A: 

Here's a generalized solution. This is a bit clumsy, but I can't think of a more concise method:

// Declare variables.
var subElements = cookieString.split("&");
var subElemPairs = new Array();
var subNameValues = new Array();

// Obtain sub-element names and values.
for (i = 0; i < subElements.length; i++)
{
    subElemPairs[i] = subElements[i].split("=");
}

// Place sub-element name-value pairs in an associative array.
for (i = 0; i < subElemPairs.length; i++)
{
    subNameValues[subElemPairs[i][0]] = subElemPairs[i][1];
}

// Example sub-element value request.
var requestedElemName = "SC";
var resultingElemValue = subNameValues[requestedElemName];
pianoman