Manually setting cookie value in browser for testing and automation

26 Apr

Sometimes you have to set/use cookie for testing, and you may not have control to force set cookie on server side application/site, so you have to set it on the client side.

But what’s a good option to set cookie that’s cross platform across browsers and operating systems? I assume probably javascript, as not all browsers have a cookie editor section like Firebug related tools for Firefox. Most browsers only seem to have a cookie viewer built in with the dev tools unless I’m mistaken.

With javascript, you can set the cookie for use with Selenium (or use Selenium’s API to set cookie instead of pure javascript), and you can also use it for manual testing as well.

Here’s one example of how to do it adapted from http://www.w3schools.com/js/js_cookies.asp


var doSetCookie = function setCookie(c_name,value,exdays){ var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value; }; doSetCookie("cookieName","cookieValue",1);

For manual testing, simply open up the javascript/error/developer console of the browser developer tools and then paste & execute the above code. May then have to refresh page for cookie to take effect.

I do notice that setting cookies this way for Selenium doesn’t persist as well as doing it via native Selenium APIs, but this is a workaround should you have issues with the native approach like with Selenium issue 5503.

And one last thing to end this post, anyone have a more optimized javascript version of the above code to execute? What I have above I’m assuming is just rudimentary proof of concept and could be fine tuned.

Leave a comment

Lets Learn

Opinion Matters

The Technical Communicator

Technical Writing at Its Best...

caulkwarmer

Tips and Tricks from "The Warming Experts"

I Don't Know Squat About Networking

One radio guy's journey into an IP world. These are my musings alone and don't represent my employer.

M.V.M-n.

Chronologically captured moments...

Ted's Energy Tips

Practical tips for making your home more comfortable, efficient and safe

It must be the network...

Ramblings of JD (@subnetwork)

TestAndAnalysis

Mike Harris' Testing and Quality Blog

JUNIPER JUNOS & CISCO IOS 互联路由

Shiyu Meng on JUNOS Routing with IOS: Practical Juniper Networks and Cisco Systems

Lifestyle Screens

Lifestyle Garage Screens....The most versatile garage screen on the planet!

Iphone Games

My games and apps for iphone

Code Ghar

Code, Scripts, Configurations, and Discussion

mobiarch

Mobile architecture blog

Mobility, Management, & Security

Bringing you detailed information about securing endpoints. All thoughts, views, and opinions are my own.

pictures & ponderings

adventures in gratitude

ReginS

Surf here for Programming Ideas, Online Tricks and much more. Stay connected to explore new things. Mail your queries to "askregins@gmail.com"

The Counter-top Guy's Blog

All about the wonderful world of Countertop Maintenance, Repair & Installation

Chris Tech Blog

“We, the unwilling, led by the unknowing, are doing the impossible for the ungrateful. We have done so much, for so long, with so little, we are now qualified to do anything with nothing.”

Random ASCII - tech blog of Bruce Dawson

Forecast for randomascii: programming, tech topics, with a chance of unicycling