Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Relic Solution: Get/Interact with Cookies Script

synthetics-script
developer

#1
//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');
      
      });
    });
  });
});

How do I handle cookies
Browser OR Synthetic Monitor to capture the cookie size
#2

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');
  
      });
    });
   });
 });

#3

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.