How to use driver.setCookie method in Appium Xcuitest Driver

Best JavaScript code snippet using appium-xcuitest-driver

safari-basic-e2e-specs.js

Source:safari-basic-e2e-specs.js Github

copy

Full Screen

...364            doesIncludeCookie(cookies, oldCookie1);365            doesIncludeCookie(cookies, oldCookie2);366          });367          it('should be able to set a cookie for a page', async function () {368            await driver.setCookie(newCookie);369            const cookies = await driver.allCookies();370            doesIncludeCookie(cookies, newCookie);371            // should not clobber old cookies372            doesIncludeCookie(cookies, oldCookie1);373            doesIncludeCookie(cookies, oldCookie2);374          });375          it('should be able to set a cookie with expiry', async function () {376            const expiredCookie = Object.assign({}, newCookie, {377              expiry: parseInt(Date.now() / 1000, 10) - 1000, // set cookie in past378              name: 'expiredcookie',379            });380            let cookies = await driver.allCookies();381            doesNotIncludeCookie(cookies, expiredCookie);382            await driver.setCookie(expiredCookie);383            cookies = await driver.allCookies();384            // should not include cookie we just added because of expiry385            doesNotIncludeCookie(cookies, expiredCookie);386            // should not clobber old cookies387            doesIncludeCookie(cookies, oldCookie1);388            doesIncludeCookie(cookies, oldCookie2);389            await driver.deleteCookie(expiredCookie.name);390          });391          it('should be able to delete one cookie', async function () {392            await driver.setCookie(newCookie);393            let cookies = await driver.allCookies();394            doesIncludeCookie(cookies, newCookie);395            await driver.deleteCookie(newCookie.name);396            cookies = await driver.allCookies();397            doesNotIncludeCookie(cookies, newCookie);398            doesIncludeCookie(cookies, oldCookie1);399            doesIncludeCookie(cookies, oldCookie2);400          });401          it('should be able to delete all cookies', async function () {402            await driver.setCookie(newCookie);403            let cookies = await driver.allCookies();404            doesIncludeCookie(cookies, newCookie);405            await driver.deleteAllCookies();406            cookies = await driver.allCookies();407            cookies.length.should.equal(0);408            doesNotIncludeCookie(cookies, oldCookie1);409            doesNotIncludeCookie(cookies, oldCookie2);410          });411          describe('native context', function () {412            const notImplementedRegExp = /Method is not implemented/;413            let context;414            beforeEach(async function () {415              context = await driver.currentContext();416              await driver.context('NATIVE_APP');417            });418            afterEach(async function () {419              if (context) {420                await driver.context(context);421              }422            });423            it('should reject all functions', async function () {424              await driver.setCookie(newCookie).should.eventually.be.rejectedWith(notImplementedRegExp);425              await driver.allCookies().should.eventually.be.rejectedWith(notImplementedRegExp);426              await driver.deleteCookie(newCookie.name).should.eventually.be.rejectedWith(notImplementedRegExp);427              await driver.deleteAllCookies().should.eventually.be.rejectedWith(notImplementedRegExp);428            });429          });430        });431        describe('secure', function () {432          /*433           * secure cookie tests are in `./safari-ssl-e2e-specs.js`434           */435        });436      });437    });438  });...

Full Screen

Full Screen

instagram.js

Source:instagram.js Github

copy

Full Screen

...35        await this.driver.removeCookie('mid');36        await this.driver.removeCookie('sessionid');37        await this.driver.removeCookie('ig_did');38        await this.driver.removeCookie('csrftoken');39        await this.driver.setCookie({ name: 'ig_did', value: sessionFile.ig_did, 'sameSite': 'Strict' });40        await this.driver.setCookie({ name: 'sessionid', value: sessionFile.sessionid, 'sameSite': 'Strict' });41        await this.driver.setCookie({ name: 'ds_user_id', value: sessionFile.ds_user_id, 'sameSite': 'Strict' });42        await this.driver.setCookie({ name: 'mid', value: sessionFile.mid, 'sameSite': 'Strict' });43        await this.driver.setCookie({ name: 'csrftoken', value: sessionFile.csrftoken, 'sameSite': 'Strict' });44        this.driver.sleep(6);45        await this.driver.openUrl("https://www.instagram.com/");46    }47    /**48     * @param  {string} username49     * @param  {string} password50     * @param  {string} path51     */52    async normalLogin(username, password, path) {53        let url = 'https://www.instagram.com/accounts/login/';54        await this.driver.openUrl(url);55        let inputs = 'form input';56        var element = await this.driver.getElements('css', inputs);57        await this.driver.type(element[0], username);...

Full Screen

Full Screen

cookies-base.js

Source:cookies-base.js Github

copy

Full Screen

1"use strict";2var setup = require("../setup-base"),3    webviewHelper = require("../../../helpers/webview"),4    loadWebView = webviewHelper.loadWebView,5    isChrome = webviewHelper.isChrome,6    testEndpoint = webviewHelper.testEndpoint,7    _ = require('underscore');8module.exports = function (desired) {9  describe('cookies', function () {10    var driver;11    setup(this, desired, {'no-reset': true}).then(function (d) { driver = d; });12    describe('within iframe webview', function () {13      it('should be able to get cookies for a page with none', function (done) {14        loadWebView(desired, driver, testEndpoint(desired) + 'iframes.html',15            "Iframe guinea pig").then(function () {16          return driver17            .deleteAllCookies()18            .get(testEndpoint(desired))19            .allCookies().should.eventually.have.length(0);20        }).nodeify(done);21      });22    });23    describe('within webview', function () {24      // TODO: investigate why we need that25      function _ignoreEncodingBug(value) {26        if (isChrome(desired)) {27          console.warn('Going round android bug: whitespace in cookies.');28          return encodeURI(value);29        } else return value;30      }31      beforeEach(function (done) {32        loadWebView(desired, driver).nodeify(done);33      });34      it('should be able to get cookies for a page', function (done) {35        driver36          .allCookies()37          .then(function (cookies) {38            cookies.length.should.equal(2);39            cookies[0].name.should.equal("guineacookie1");40            cookies[0].value.should.equal(_ignoreEncodingBug("i am a cookie value"));41            cookies[1].name.should.equal("guineacookie2");42            cookies[1].value.should.equal(_ignoreEncodingBug("cookié2"));43          }).nodeify(done);44      });45      it('should be able to set a cookie for a page', function (done) {46        var newCookie = {name: "newcookie", value: "i'm new here"};47        driver48          .deleteCookie(newCookie.name)49          .allCookies()50          .then(function (cookies) {51            _.pluck(cookies, 'name').should.not.include(newCookie.name);52            _.pluck(cookies, 'value').should.not.include(newCookie.value);53          }).then(function () {54            return driver55              .setCookie(newCookie)56              .allCookies();57          })58          .then(function (cookies) {59            _.pluck(cookies, 'name').should.include(newCookie.name);60            _.pluck(cookies, 'value').should.include(newCookie.value);61            // should not clobber old cookies62            _.pluck(cookies, 'name').should.include("guineacookie1");63            _.pluck(cookies, 'value').should.include(_ignoreEncodingBug("i am a cookie value"));64          })65          .nodeify(done);66      });67      it('should be able to set a cookie with expiry', function (done) {68        var newCookie = {name: "newcookie", value: "i'm new here"};69        var now = parseInt(Date.now() / 1000, 10);70        newCookie.expiry = now - 1000; // set cookie in past71        driver72          .deleteCookie(newCookie.name)73          .allCookies()74          .then(function (cookies) {75            _.pluck(cookies, 'name').should.not.include(newCookie.name);76            _.pluck(cookies, 'value').should.not.include(newCookie.value);77          })78          .then(function () {79            return driver80              .setCookie(newCookie)81              .allCookies();82          }).then(function (cookies) {83            // should not include cookie we just added because of expiry84            _.pluck(cookies, 'name').should.not.include(newCookie.name);85            _.pluck(cookies, 'value').should.not.include(newCookie.value);86            // should not clobber old cookies87            _.pluck(cookies, 'name').should.include("guineacookie1");88            _.pluck(cookies, 'value').should.include(_ignoreEncodingBug("i am a cookie value"));89          })90          .nodeify(done);91      });92      it('should be able to delete one cookie', function (done) {93        var newCookie = {name: "newcookie", value: "i'm new here"};94        driver95          .deleteCookie(newCookie.name)96          .allCookies()97        .then(function (cookies) {98          _.pluck(cookies, 'name').should.not.include(newCookie.name);99          _.pluck(cookies, 'value').should.not.include(newCookie.value);100        }).then(function () {101          return driver102            .setCookie(newCookie)103            .allCookies();104        }).then(function (cookies) {105          _.pluck(cookies, 'name').should.include(newCookie.name);106          _.pluck(cookies, 'value').should.include(newCookie.value);107        }).then(function () {108          return driver109            .deleteCookie('newcookie')110            .allCookies();111        }).then(function (cookies) {112          _.pluck(cookies, 'name').should.not.include(newCookie.name);113          _.pluck(cookies, 'value').should.not.include(newCookie.value);114        }).nodeify(done);115      });116      it('should be able to delete all cookie', function (done) {117        var newCookie = {name: "newcookie", value: "i'm new here"};118        driver119          .deleteCookie(newCookie.name)120          .allCookies()121          .then(function (cookies) {122            _.pluck(cookies, 'name').should.not.include(newCookie.name);123            _.pluck(cookies, 'value').should.not.include(newCookie.value);124          }).then(function () {125            return driver126              .setCookie(newCookie)127              .allCookies();128          }).then(function (cookies) {129            _.pluck(cookies, 'name').should.include(newCookie.name);130            _.pluck(cookies, 'value').should.include(newCookie.value);131          }).then(function () {132            return driver133              .deleteAllCookies()134              .allCookies();135          }).then(function (cookies) {136            cookies.length.should.equal(0);137          }).nodeify(done);138      });139    });140  });...

Full Screen

Full Screen

cookies-specs.js

Source:cookies-specs.js Github

copy

Full Screen

...32      await driver.deleteCookie(newCookie.name);33      let cookies = await driver.getCookies();34      cookies.map((c) => c.name).should.not.include(newCookie.name);35      cookies.map((c) => c.value).should.not.include(newCookie.value);36      await driver.setCookie(newCookie);37      cookies = await driver.getCookies();38      cookies.map((c) => c.name).should.include(newCookie.name);39      cookies.map((c) => c.value).should.include(newCookie.value);40      // should not clobber old cookies41      cookies.map((c) => c.name).should.include('guineacookie1');42      cookies.map((c) => c.value).should.include('i am a cookie value');43    });44    it('should be able to set a cookie with expiry', async function () {45      let newCookie = {46        name: `newcookie`,47        value: `i'm new here`48      };49      newCookie.expiry = parseInt(Date.now() / 1000, 10) - 1000; // set cookie in past50      await driver.deleteCookie(newCookie.name);51      let cookies = await driver.getCookies();52      cookies.map((c) => c.name).should.not.include(newCookie.name);53      cookies.map((c) => c.value).should.not.include(newCookie.value);54      await driver.setCookie(newCookie);55      cookies = await driver.getCookies();56      // should not include cookie we just added because of expiry57      cookies.map((c) => c.name).should.not.include(newCookie.name);58      cookies.map((c) => c.value).should.not.include(newCookie.value);59      // should not clobber old cookies60      cookies.map((c) => c.name).should.include('guineacookie1');61      cookies.map((c) => c.value).should.include('i am a cookie value');62    });63    it('should be able to delete one cookie', async function () {64      let newCookie = {65        name: `newcookie`,66        value: `i'm new here`67      };68      await driver.deleteCookie(newCookie.name);69      let cookies = await driver.getCookies();70      cookies.map((c) => c.name).should.not.include(newCookie.name);71      cookies.map((c) => c.value).should.not.include(newCookie.value);72      await driver.setCookie(newCookie);73      cookies = await driver.getCookies();74      cookies.map((c) => c.name).should.include(newCookie.name);75      cookies.map((c) => c.value).should.include(newCookie.value);76      await driver.deleteCookie('newcookie');77      cookies = await driver.getCookies();78      cookies.map((c) => c.name).should.not.include(newCookie.name);79      cookies.map((c) => c.value).should.not.include(newCookie.value);80    });81    it('should be able to delete all cookie', async function () {82      let newCookie = {83        name: `newcookie`,84        value: `i'm new here`85      };86      await driver.deleteCookie(newCookie.name);87      let cookies = await driver.getCookies();88      cookies.map((c) => c.name).should.not.include(newCookie.name);89      cookies.map((c) => c.value).should.not.include(newCookie.value);90      await driver.setCookie(newCookie);91      cookies = await driver.getCookies();92      cookies.map((c) => c.name).should.include(newCookie.name);93      cookies.map((c) => c.value).should.include(newCookie.value);94      await driver.deleteCookies();95      cookies = await driver.getCookies();96      cookies.length.should.equal(0);97    });98  });...

Full Screen

Full Screen

safari-cookie-e2e-specs.js

Source:safari-cookie-e2e-specs.js Github

copy

Full Screen

...62    it('should be able to set a cookie for a page', async () => {63      await driver.deleteCookie(newCookie.name);64      let cookies = await driver.allCookies();65      doesNotIncludeCookie(cookies, newCookie);66      await driver.setCookie(newCookie);67      cookies = await driver.allCookies();68      doesIncludeCookie(cookies, newCookie);69      // should not clobber old cookies70      doesIncludeCookie(cookies, oldCookie1);71      doesIncludeCookie(cookies, oldCookie2);72    });73    it('should be able to set a cookie with expiry', async () => {74      let expiredCookie = _.defaults({75        expiry: parseInt(Date.now() / 1000, 10) - 1000 // set cookie in past76      }, newCookie);77      await driver.deleteCookie(expiredCookie.name);78      let cookies = await driver.allCookies();79      doesNotIncludeCookie(cookies, expiredCookie);80      await driver.setCookie(expiredCookie);81      cookies = await driver.allCookies();82      // should not include cookie we just added because of expiry83      doesNotIncludeCookie(cookies, expiredCookie);84      // should not clobber old cookies85      doesIncludeCookie(cookies, oldCookie1);86      doesIncludeCookie(cookies, oldCookie2);87    });88    it('should be able to delete one cookie', async () => {89      await driver.deleteCookie(newCookie.name);90      let cookies = await driver.allCookies();91      doesNotIncludeCookie(cookies, newCookie);92      await driver.setCookie(newCookie);93      cookies = await driver.allCookies();94      doesIncludeCookie(cookies, newCookie);95      await driver.deleteCookie(newCookie.name);96      cookies = await driver.allCookies();97      doesNotIncludeCookie(cookies, newCookie);98      doesIncludeCookie(cookies, oldCookie1);99      doesIncludeCookie(cookies, oldCookie2);100    });101    it('should be able to delete all cookies', async () => {102      await driver.deleteCookie(newCookie.name);103      let cookies = await driver.allCookies();104      doesNotIncludeCookie(cookies, newCookie);105      await driver.setCookie(newCookie);106      cookies = await driver.allCookies();107      doesIncludeCookie(cookies, newCookie);108      await driver.deleteAllCookies();109      cookies = await driver.allCookies();110      cookies.length.should.equal(0);111      doesNotIncludeCookie(cookies, oldCookie1);112      doesNotIncludeCookie(cookies, oldCookie2);113    });114  });...

Full Screen

Full Screen

safari-ssl-e2e-specs.js

Source:safari-ssl-e2e-specs.js Github

copy

Full Screen

...70        driver = await initSession(caps);71      });72      beforeEach(async function () {73        await driver.get(LOCAL_HTTPS_URL);74        await driver.setCookie(oldCookie1);75        await driver.deleteCookie(secureCookie.name);76      });77      it('should be able to set a secure cookie', async function () {78        let cookies = await driver.allCookies();79        doesNotIncludeCookie(cookies, secureCookie);80        await driver.setCookie(secureCookie);81        cookies = await driver.allCookies();82        doesIncludeCookie(cookies, secureCookie);83        // should not clobber old cookie84        doesIncludeCookie(cookies, oldCookie1);85      });86      it('should be able to set a secure cookie', async function () {87        await driver.setCookie(secureCookie);88        let cookies = await driver.allCookies();89        doesIncludeCookie(cookies, secureCookie);90        // should not clobber old cookie91        doesIncludeCookie(cookies, oldCookie1);92        await driver.deleteCookie(secureCookie.name);93        cookies = await driver.allCookies();94        doesNotIncludeCookie(cookies, secureCookie);95      });96    });97  });...

Full Screen

Full Screen

ios-safari.js

Source:ios-safari.js Github

copy

Full Screen

...45  it("should delete cookie passing domain and path", function () {46    var complexCookieDelete = function(name, path, domain) {47      return function() {48        path = path || '|';49        return driver.setCookie({name: name, value: '', path: path, 50          domain: domain, expiry: 0});        51      };52    };53    return driver54      .get('http://en.wikipedia.org')55      .waitForElementByCss('.mediawiki', 5000)56      .allCookies() // 'GeoIP' cookie is there57      .deleteCookie('GeoIP') 58      .allCookies() // 'GeoIP' is still there, because it is set on59                    // the .wikipedia.org domain60      .then(complexCookieDelete('GeoIP', '/', '.wikipedia.org'))61      .allCookies() // now 'GeoIP' cookie is gone62      .sleep(1000);63  });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const wdio = require('webdriverio');2const opts = {3    capabilities: {4    }5};6async function main() {7    const client = await wdio.remote(opts);8    await client.setCookie({9    });10    const cookie = await client.getCookies();11    console.log(cookie);12    await client.deleteCookie('myCookie');13    await client.deleteAllCookies();14    await client.deleteSession();15}16main();17const wdio = require('webdriverio');18const opts = {19    capabilities: {20    }21};22async function main() {23    const client = await wdio.remote(opts);24    await client.setCookie({25    });26    const cookie = await client.getCookies();27    console.log(cookie);28    await client.deleteCookie('myCookie');29    await client.deleteAllCookies();30    await client.deleteSession();31}32main();33const wdio = require('webdriverio');34const opts = {35    capabilities: {

Full Screen

Using AI Code Generation

copy

Full Screen

1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder()3  .withCapabilities({4  })5  .build();6driver.sleep(2000);7driver.manage().addCookie({name:'cookie1',value:'cookie1'});8driver.manage().addCookie({name:'cookie2',value:'cookie2'});9driver.manage().addCookie({name:'cookie3',value:'cookie3'});10driver.sleep(2000);11driver.quit();

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2const assert = require('assert');3const chai = require('chai');4const chaiAsPromised = require('chai-as-promised');5chai.use(chaiAsPromised);6chai.should();7const desiredCaps = {8};9const driver = wd.promiseChainRemote('localhost', 4723);10driver.init(desiredCaps)11    .then(() => {12        driver.setCookie({13        })14    })15    .catch((err) => {16        console.log(err);17    });18const wd = require('wd');19const assert = require('assert');20const chai = require('chai');21const chaiAsPromised = require('chai-as-promised');22chai.use(chaiAsPromised);23chai.should();24const desiredCaps = {25};26const driver = wd.promiseChainRemote('localhost', 4723);27driver.init(desiredCaps)28    .then(() => {29        driver.setCookie({30        })31    })32    .catch((err) => {33        console.log(err);34    });35const wd = require('wd');36const assert = require('assert');37const chai = require('chai');38const chaiAsPromised = require('chai-as-promised');39chai.use(chaiAsPromised);40chai.should();41const desiredCaps = {42};43const driver = wd.promiseChainRemote('localhost', 4723);44driver.init(desired

Full Screen

Using AI Code Generation

copy

Full Screen

1const { remote } = require('webdriverio');2const caps = {3};4(async () => {5  const driver = await remote({6  });7  await driver.setCookie({ name: 'foo', value: 'bar' });8  await driver.deleteCookie('foo');9  await driver.deleteCookies();10  await driver.deleteSession();11})();12[debug] [W3C (e7c6b3d0)] Calling AppiumDriver.setCookie() with args: [{"name":"foo","value":"bar"},"e7c6b3d0-6d7e-4a9a-8c8a-9f9a9a9a9a9a"]13[debug] [W3C (e7c6b3d0)] Calling AppiumDriver.deleteCookie() with args: ["foo","e7c6b3d0-6d7e-4a9a-8c8a-9f9a9a9a9a9a"]14[debug] [W3C (e7c6b3d0)] Calling AppiumDriver.deleteCookies() with args: ["e7c6b3d0-6d7e-4a9a-8c8a-9f9a9a9a9a9a"]15[debug] [W3C (e7c6b3d0)] Received response: null16[debug] [W3C (e7c6b3d0)] But deleting session, so not returning17[debug] [W3C (e7c6b3d0)] Responding to client with driver.deleteSession

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3    .init({4    })5    .then(function() {6    })7    .then(function() {8        return driver.setCookie({9        });10    })11    .then(function() {12    })13    .then(function() {14        return driver.getCookies();15    })16    .then(function(cookies) {17        console.log(cookies);18    })19    .fin(function() {20        return driver.quit();21    })22    .done();

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium Xcuitest Driver automation tests on LambdaTest cloud grid

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

Sign up Free
_

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful