Relic Solution: Get/Interact with Cookies Script

//This script will check for a cookie and pass/fail the script based on the return value.
var By = $driver.By;
var assert = require('assert');
var browser = $browser.manage();

// we need to request a domain to mess with cookies
$browser.get('http://google.com').then(function(){

    // create cookies 
    $browser.manage().addCookie({name:'myCookie', value: 'chocolate chip'});
    $browser.manage().addCookie({name:'myOtherCookie', value: 'macadamia nut'}).then(function(){

    // get the cookies
    browser.getCookies().then(function(cookies){
      
      //do stuff with your cookies
      console.log(cookies);
    
    }).then(function(){
      
      // or just get one cookie
      browser.getCookie('myCookie').then(function(cookie){
        
        // do stuff with your cookie
        console.log(cookie);
        // if your chocolate chips are not here. alert the realm!
        return assert.equal(cookie.value, 'chocolate chip', 'no chocolate chips');
      
      });
    });
  });
});
3 Likes

To get this to work I needed to change the script to be…

//This script will check for a cookie and pass/fail the script based on the return value.
var By = $driver.By;
var assert = require('assert');
var browser = $browser.manage();

// we need to request a domain to mess with cookies
$browser.get('http://google.com').then(function(){

    // create cookies
    $browser.manage().addCookie({name:'myCookie', value: 'chocolate chip'});
    $browser.manage().addCookie({name:'myOtherCookie', value: 'macadamia nut'}).then(function(){

    // get the cookies
    browser.getCookies().then(function(cookies){
  
    //do stuff with your cookies
    console.log(cookies);

    }).then(function(){
  
      // or just get one cookie
      browser.getCookie('myCookie').then(function(cookie){
    
        // do stuff with your cookie
        console.log(cookie);
        // if your chocolate chips are not here. alert the realm!
        return assert.equal(cookie.value, 'chocolate chip', 'no chocolate chips');
  
      });
    });
   });
 });
1 Like

Thanks for sharing @David.Bell - you’re right, it looks like the cookie syntax needs to be updated Seth’s post, I’ll get that updated.

@RyanVeitch Have docs been updated? I am writing a test and I also have to pass the cookie as an object as highlighted by David Bell.

When I use $browser.manage().addCookie('nameString', valueString') I get the following error: Error: invalid argument: missing ‘name’

When I pass it as an object $browser.manage().addCookie({ name: 'nameString', value: 'valueString' }) the New Relic IDE flares up red but my test passes??

Could you please confirm passing the object is correct and could you please update the docs if so?

@Martijn.Kuhlemeier - Passing the object is correct.

The docs have two options, depending on the age of your Synthetics Monitor - some on an older runtime will follow these docs (this is how it was when Seth wrote the original script here):
https://docs.newrelic.com/docs/synthetics/new-relic-synthetics/scripting-monitors/synthetics-scripted-browser-reference-monitor-versions-04x-or-lower#options

And this is the doc for the latest Synthetics Runtime:
https://docs.newrelic.com/docs/synthetics/new-relic-synthetics/scripting-monitors/synthetics-scripted-browser-reference-monitor-versions-050#options

As you can see here we do say that the addCookie option requires an object to be passed in.