How to use this.getCookie method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

MyCookies.js

Source: MyCookies.js Github

copy
1"use strict";
2function MyCookies (vars) {
3    this.vars = vars;
4    this.vars.myCookies = this;
5    this.getCookie = function (sName) {
6        var oRegex = new RegExp("(?:; )?" + sName + "=([^;]*);?");
7        
8        if (oRegex.test(document.cookie)) {
9            return decodeURIComponent(RegExp["$1"]);
10        } else {
11            return null;
12        }
13    }
14    
15    this.setCookie = function (sName,sValue) {
16        var value = "";
17        var today = new Date(), expires = new Date();
18        expires.setTime(today.getTime() - 60);
19        if (sValue!=null) {
20            value=encodeURIComponent(sValue);
21            expires.setTime(today.getTime() + (365*24*60*60*1000));
22        }
23        var dateexp = expires.toGMTString();
24        document.cookie = sName + "=" + value + ";expires=" + dateexp;
25    }
26    
27    this.getAllCookies = function () {
28        var tmp = this.getCookie("SUPR0");
29        this.vars.del1step0 = ((tmp==="1") || (tmp==="0"))?tmp=="1":false;
30        tmp = this.getCookie("SUPR1");
31        this.vars.del1step1 = ((tmp==="1") ||  (tmp==="0"))?tmp=="1":false;
32//        tmp = this.getCookie("EMAILOK");
33//        this.vars.emailok = ((tmp=="1") ||  (tmp=="0"))?tmp=="1":true;
34        tmp = this.getCookie("ROUTEWPTS");
35        this.vars.routewpts = ((tmp==="1") ||  (tmp==="0"))?tmp=="1":false;
36        tmp = this.getCookie("EXTRAWPTS");
37        this.vars.extrawpts = ((tmp==="1") ||  (tmp==="0"))?tmp=="1":false;
38        tmp = this.getCookie("EXTRATRK");
39        this.vars.extratrk = ((tmp==="1") ||  (tmp==="0"))?tmp=="1":false;
40        this.vars.fname = this.getCookie("FNAME");
41        this.vars.ename = this.getCookie("ENAME");
42        tmp = this.getCookie("SENDTYPE");
43        this.vars.sendtype = (tmp==null)?'file':tmp;
44        tmp = this.getCookie("TYPEEXT");
45        this.vars.typeext = (tmp==null)?'typeitn':tmp;
46        //var youremail = '';
47    }
48    
49    this.setAllCookies = function () {
50        this.setCookie ("EMAILOK", this.vars.emailok?"1":"0");
51        this.setCookie ("TYPEEXT", this.vars.typeext);
52        this.setCookie ("ROUTEWPTS", this.vars.routewpts?"1":"0");
53        this.setCookie ("EXTRAWPTS", this.vars.extrawpts?"1":"0");
54        this.setCookie ("EXTRATRK", this.vars.extratrk?"1":"0");
55        this.setCookie ("FNAME", this.vars.fname);
56        this.setCookie ("ENAME", this.vars.ename);
57        this.setCookie ("SUPR0", this.vars.del1step0?"1":"0");
58        this.setCookie ("SUPR1", this.vars.del1step1?"1":"0");
59        this.setCookie ("SENDTYPE", this.vars.sendtype);
60    }
61    
62    /*function url (i) {
63        return $('#url'+i).val().replace(/(\n|\r|\s)/g,""); 
64    }*/
65} 
66
Full Screen

CookieStorageManager.js

Source: CookieStorageManager.js Github

copy
1var CookieStorageManager = function() {
2    var that = this;
3    this.listeners = MqttClientApp.listeners;
4    this.enabled = (typeof(Storage) !== "undefined");
5    this.cookieTtl = 30; // in days
6
7    this.listeners.addListener('connect', function(host, port, clientId, username, password,
8        keepAlive, useSsl, cleanSession, lastWillTopic, lastWillMessage, lastWillQos, lastWillRetain) {
9        if (that.enabled) {
10            that.setCookie("host", host);
11            that.setCookie("port", port);
12            that.setCookie("clientId", clientId);
13            that.setCookie("username", username);
14            that.setCookie("keepAlive", keepAlive);
15            that.setCookie("useSsl", useSsl);
16            that.setCookie("cleanSession", cleanSession);
17            that.setCookie("md5", $("#md5Input").prop("checked"));
18        }
19    });
20};
21
22CookieStorageManager.prototype.loadCredentials = function() {
23    if (this.enabled) {
24        if (this.getCookie("host")) {
25            $("#hostnameInput").val(this.getCookie("host"));
26        }
27
28        if (this.getCookie("port")) {
29            $("#portInput").val(this.getCookie("port"));
30        }
31
32        if (this.getCookie("clientId")) {
33            $("#clientIdInput").val(this.getCookie("clientId"));
34        }
35
36        if (this.getCookie("username")) {
37            $("#usernameInput").val(this.getCookie("username"));
38        }
39
40        if (this.getCookie("keepAlive")) {
41            $("#keepAliveInput").val(this.getCookie("keepAlive"));
42        }
43
44        if (this.getCookie("useSsl")) {
45            $("#useSslInput").prop('checked', JSON.parse(this.getCookie("useSsl")));
46        }
47
48        if (this.getCookie("cleanSession")) {
49            $("#cleanSessionInput").prop('checked', JSON.parse(this.getCookie("cleanSession")));
50        }
51
52        if (this.getCookie("md5")) {
53            $("#md5Input").prop('checked', JSON.parse(this.getCookie("md5")));
54        }
55    }
56};
57
58CookieStorageManager.prototype.setCookie = function(cookieName, cookieValue) {
59    var d = new Date();
60    d.setTime(d.getTime() + (this.cookieTtl * 24 * 60 * 60 * 1000));
61    var expires = "expires=" + d.toGMTString();
62    document.cookie = cookieName + "=" + cookieValue + "; " + expires;
63};
64
65CookieStorageManager.prototype.getCookie = function(cookieName) {
66    var name = cookieName + "=";
67    var ca = document.cookie.split(';');
68
69    for(var i = 0; i < ca.length; i++) {
70        var c = ca[i].trim();
71        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
72    }
73
74    return "";
75};
Full Screen

Gauges_Tracking_Code.js

Source: Gauges_Tracking_Code.js Github

copy
1
2
3//Original 
4/*
5var _gauges = _gauges || [];
6  (function() {
7    var t   = document.createElement('script');
8    t.type  = 'text/javascript';
9    t.async = true;
10    t.id    = 'gauges-tracker';
11    t.setAttribute('data-site-id', '4f1852bccb25bc4e10000007');
12    t.src = '//secure.gaug.es/track.js';
13    var s = document.getElementsByTagName('script')[0];
14    s.parentNode.insertBefore(t, s);
15  })();
16*/
17
18//TeamMentor modified 
19
20//from https://secure.gaug.es/track.js on 4/13/12
21
22GauguesID = null;                   // set to GaugesID to enable it
23
24if (GauguesID === null)
25{
26    my_Gauge = {};
27    my_Gauge.track = function() {}       
28}
29else
30{
31    var my_Gauge = 
32        {
33                data_site_id : GauguesID
34            ,   title        : null
35            ,   resource     : null
36            ,   track        : function(title, resource)
37                                {
38                                    my_Gauge.title = title;
39                                    my_Gauge.resource = resource;
40                                    _gauges.track();
41                                }
42        };
43
44    var _gauges= _gauges || [];
45
46    (function()
47        {
48            var h= (_gauges['slice']) ? _gauges.slice(0):[];
49            _gauges =   {
50                                track_referrer:true
51                            ,   image:new Image()
52                            ,   track:function()
53                                    {
54                                        this.setCookie('_gauges_cookie',1,1);
55                                        var a=this.url();
56                                        if(a)
57                                        {
58                                            this.image.src=a;
59                                            var b=60*60,d=b*24,f=d*31,c=d*365,i=c*10;
60                                            if(!this.getCookie('_gauges_unique_hour'))  { this.setCookie('_gauges_unique_hour',1,b) }
61                                            if(!this.getCookie('_gauges_unique_day'))   { this.setCookie('_gauges_unique_day',1,d) }
62                                            if(!this.getCookie('_gauges_unique_month')) { this.setCookie('_gauges_unique_month',1,f) }
63                                            if(!this.getCookie('_gauges_unique_year'))  { this.setCookie('_gauges_unique_year',1,c) }
64                                            if(!this.getCookie('_gauges_unique'))       { this.setCookie('_gauges_unique',1,i) }
65                                        }
66                                    }
67                            ,   push:function(a)
68                                    {
69                                        var b=a.shift();
70                                        if(b=='track')
71                                        {
72                                            _gauges.track()
73                                        }
74                                    }
75                            ,   url:function()
76                                    {
77                                        var a,b
78                                        //TM changes - start
79                                            //d=this.$('gauges-tracker');
80                                            // if(d)
81                                       {
82                                        
83                                            //b=d.getAttribute('data-site-id'); 
84                                            b = my_Gauge.data_site_id;
85
86                                            //a=d.src.replace('/track.js','/track.gif');
87                                            a = "https://secure.gaug.es/track.gif"
88                                            var title = my_Gauge.title ? my_Gauge.title : this.title();
89                                            var resource = my_Gauge.resource ? my_Gauge.resource : this.resource();
90                                           //TM changes - end
91
92                                            a+="?h[site_id]="       + b;
93                                            a+="&h[resource]="      + resource;
94                                            a+="&h[referrer]="      + this.referrer();
95                                            a+="&h[title]="         + title;
96                                            a+="&h[user_agent]="    + this.agent();
97                                            a+="&h[unique]="        + this.unique();
98                                            a+="&h[unique_hour]="   + this.uniqueHour();
99                                            a+="&h[unique_day]="    + this.uniqueDay();
100                                            a+="&h[unique_month]="  + this.uniqueMonth();
101                                            a+="&h[unique_year]="   + this.uniqueYear();
102                                            a+="&h[screenx]="       + this.screenWidth();
103                                            a+="&h[browserx]="      + this.browserWidth();
104                                            a+="&h[browsery]="      + this.browserHeight();
105                                            a+="&timestamp="        + this.timestamp()
106                                       }
107                                       return a
108                                    }
109                            ,   domain:function()
110                                    {
111                                        return window.location.hostname
112                                    }
113                            ,   referrer:function()
114                                    {
115                                        var a='';
116                                        if(!this.track_referrer)
117                                        {
118                                            return a
119                                        }
120                                        this.track_referrer=false;
121                                        try
122                                        {
123                                            a=top.document.referrer
124                                        }
125                                        catch(e1)
126                                        {
127                                            try
128                                            {
129                                                a=parent.document.referrer
130                                            }
131                                            catch(e2)
132                                            {
133                                                a=''
134                                            }
135                                        }
136                                        if(a=='')
137                                        {
138                                            a=document.referrer
139                                        }
140                                        return this.escape(a)
141                                    }
142                            ,   agent:function()
143                                    {
144                                        return this.escape(navigator.userAgent)
145                                    }
146                            ,
147                                escape:function(a)
148                                    {
149                                        return(typeof(encodeURIComponent)=='function') 
150                                            ? encodeURIComponent(a)
151                                            : escape(a)
152                                    }
153                            ,   resource    :function(){ return this.escape(document.location.href) }
154                            ,   timestamp   :function(){return new Date().getTime()}
155                            ,   title       :function(){return(document.title&&document.title!="")?this.escape(document.title):''}
156                            ,   uniqueHour  :function(){if(!this.getCookie('_gauges_cookie')){return 0}return this.getCookie('_gauges_unique_hour')?0:1}
157                            ,uniqueDay:function(){if(!this.getCookie('_gauges_cookie')){return 0}return this.getCookie('_gauges_unique_day')?0:1}
158                            ,uniqueMonth:function(){if(!this.getCookie('_gauges_cookie')){return 0}return this.getCookie('_gauges_unique_month')?0:1}
159                            ,uniqueYear:function(){if(!this.getCookie('_gauges_cookie')){return 0}return this.getCookie('_gauges_unique_year')?0:1}
160                            ,unique:function(){if(!this.getCookie('_gauges_cookie')){return 0}return this.getCookie('_gauges_unique')?0:1}
161                            ,screenWidth:function(){try{return screen.width}catch(e){return 0}}
162                            ,browserDimensions:function(){var a=0,b=0;try{if(typeof(window.innerWidth)=='number'){a=window.innerWidth;b=window.innerHeight}else if(document.documentElement&&document.documentElement.clientWidth){a=document.documentElement.clientWidth;b=document.documentElement.clientHeight}else if(document.body&&document.body.clientWidth){a=document.body.clientWidth;b=document.body.clientHeight}}catch(e){}return{width:a,height:b}}
163                            ,browserWidth:function(){return this.browserDimensions().width}
164                            ,browserHeight:function(){return this.browserDimensions().height},$:function(a){if(document.getElementById){return document.getElementById(a)}return null}
165                            ,setCookie:function(a,b,d){var f,c;b=escape(b);if(d){f=new Date();f.setTime(f.getTime()+(d*1000));c='; expires='+f.toGMTString()}else{c=''}document.cookie=a+"="+b+c+"; path=/"}
166                            ,getCookie:function(a){var b=a+"=",d=document.cookie.split(';');for(var f=0;f<d.length;f++){var c=d[f];while(c.charAt(0)==' '){c=c.substring(1,c.length)}if(c.indexOf(b)==0){return unescape(c.substring(b.length,c.length))}}return null
167                        }
168                    };
169            _gauges.track();
170            for(var g=0,j=h.length;g<j;g++)
171            {
172                _gauges.push(h[g])
173            }
174        })();
175}
176 
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)