Best JavaScript code snippet using wpt
cookie-test.js
Source:cookie-test.js
1// getDefaultPathCookies is a helper method to get and delete cookies on the2// "default path" (which for these tests will be at `/cookies/resources`),3// determined by the path portion of the request-uri.4async function getDefaultPathCookies(path = '/cookies/resources') {5 return new Promise((resolve, reject) => {6 try {7 const iframe = document.createElement('iframe');8 iframe.style = 'display: none';9 iframe.src = `${path}/echo-cookie.html`;10 iframe.addEventListener('load', (e) => {11 const win = e.target.contentWindow;12 const iframeCookies = win.getCookies();13 win.expireCookie('test', path);14 resolve(iframeCookies);15 }, {once: true});16 document.documentElement.appendChild(iframe);17 } catch (e) {18 reject(e);19 }20 });21}22// getRedirectedCookies is a helper method to get and delete cookies that23// were set from a Location header redirect.24async function getRedirectedCookies(location, cookie) {25 return new Promise((resolve, reject) => {26 try {27 const iframe = document.createElement('iframe');28 iframe.style = 'display: none';29 iframe.src = `${location}`;30 iframe.addEventListener('load', (e) => {31 const win = e.target.contentWindow;32 const iframeCookies = win.getCookies();33 win.expireCookie(cookie);34 resolve(iframeCookies);35 }, {once: true});36 document.documentElement.appendChild(iframe);37 } catch (e) {38 reject(e);39 }40 });41}42// httpCookieTest sets a |cookie| (via HTTP), then asserts it was or was not set43// via |expectedValue| (via the DOM). Then cleans it up (via HTTP). Most tests44// do not set a Path attribute, so |defaultPath| defaults to true.45//46// |cookie| may be a single cookie string, or an array of cookie strings, where47// the order of the array items represents the order of the Set-Cookie headers48// sent by the server.49function httpCookieTest(cookie, expectedValue, name, defaultPath = true) {50 let encodedCookie = encodeURIComponent(JSON.stringify(cookie));51 return promise_test(52 async t => {53 return fetch(`/cookies/resources/cookie.py?set=${encodedCookie}`)54 .then(async () => {55 let cookies = document.cookie;56 if (defaultPath) {57 // for the tests where a Path is set from the request-uri58 // path, we need to go look for cookies in an iframe at that59 // default path.60 cookies = await getDefaultPathCookies();61 }62 if (Boolean(expectedValue)) {63 assert_equals(64 cookies, expectedValue,65 'The cookie was set as expected.');66 } else {67 assert_equals(68 cookies, expectedValue, 'The cookie was rejected.');69 }70 })71 .then(() => {72 return fetch(73 `/cookies/resources/cookie.py?drop=${encodedCookie}`);74 })},75 name);76}77// This is a variation on httpCookieTest, where a redirect happens via78// the Location header and we check to see if cookies are sent via79// getRedirectedCookies80function httpRedirectCookieTest(cookie, expectedValue, name, location) {81 const encodedCookie = encodeURIComponent(JSON.stringify(cookie));82 const encodedLocation = encodeURIComponent(location);83 const setParams = `?set=${encodedCookie}&location=${encodedLocation}`;84 return promise_test(85 async t => {86 return fetch(`/cookies/resources/cookie.py${setParams}`)87 .then(async () => {88 // for the tests where a redirect happens, we need to head89 // to that URI to get the cookies (and then delete them there)90 const cookies = await getRedirectedCookies(location, cookie);91 if (Boolean(expectedValue)) {92 assert_equals(cookies, expectedValue,93 'The cookie was set as expected.');94 } else {95 assert_equals(cookies, expectedValue, 'The cookie was rejected.');96 }97 }).then(() => {98 return fetch(`/cookies/resources/cookie.py?drop=${encodedCookie}`);99 })100 },101 name);...
utils.js
Source:utils.js
1import { camelCase, flattenDeep } from 'lodash'2import splitHostnameFromURL from 'utils/splitHostnameFromURL'3import { getDomain } from 'utils/parse-suffix-list'4import format from 'plugins/tabs/format'5export const parseCookie = value => {6 if (!value) {7 return null8 }9 return value.split(';').reduce((obj, cookieStr, i) => {10 const [key, value = ''] = cookieStr.split('=').map(str => str.trim())11 if (i === 0) {12 // This is the name13 obj.name = key14 return obj15 }16 obj[camelCase(key)] = value17 return obj18 }, {})19}20export const getAllCookiesFromDomain = domains =>21 domains.map(domain =>22 browser.cookies.getAll({23 domain: getDomain(domain),24 }),25 )26export const collectCookies = async ({27 cookieMonsterEnabled = true,28 cookieMonsterOnlyThirdParty,29 tab,30}) => {31 if (!cookieMonsterEnabled) return32 const { hostnameInvalid } = format(tab)33 if (hostnameInvalid) return34 let firstPartyCookies = []35 const domains = splitHostnameFromURL(tab.url)36 if (!domains.length) return37 if (!cookieMonsterOnlyThirdParty) {38 firstPartyCookies = await Promise.all(getAllCookiesFromDomain(domains))39 }40 const iframeCookies = await Promise.all(41 flattenDeep(42 (tab.iframeUrls || [])43 .map(splitHostnameFromURL)44 .map(getAllCookiesFromDomain),45 ),46 )47 const isFirstPartyCookie = c =>48 // TODO: filter known suffixes aka "co.uk"49 domains.some(domain => c.domain.endsWith(getDomain(domain)))50 /* Sort cookies into their collections, based on their party */51 const createCookieCollections = (cookies = []) =>52 cookies.reduce(53 (obj, c) => {54 const isFirstParty = isFirstPartyCookie(c)55 const key = isFirstParty ? 'firstParty' : 'thirdParty'56 obj[key] = obj[key].concat(c)57 return obj58 },59 {60 firstParty: [],61 thirdParty: [],62 },63 )64 const iframeCookieCollection = createCookieCollections(65 flattenDeep(iframeCookies),66 )67 // these are captured via onWebRequest68 const networkCookieCollection = createCookieCollections(tab.cookies || [])69 return {70 firstParty: cookieMonsterOnlyThirdParty71 ? []72 : [73 ...flattenDeep(firstPartyCookies),74 ...flattenDeep(iframeCookieCollection.firstParty),75 ...networkCookieCollection.firstParty,76 ],77 thirdParty: [78 ...flattenDeep(iframeCookieCollection.thirdParty),79 ...networkCookieCollection.firstParty,80 ],81 }...
HomePage.js
Source:HomePage.js
...9 signInButton : () => cy.get("[data-testid=signin-button]"), 10 };11 12 acceptCookies(){13 this.elements.iframeCookies().click();14 }15 clickSignInLink() {16 this.elements.signInLink().click();17 } 18 enterEmail() {19 this.elements.usernameTextField().click();20 this.elements.usernameTextField().type("testzoopla@test.com");21 } 22 enterPassword() {23 this.elements.passwordTextField().click();24 this.elements.passwordTextField().type("Qwert1234");25 } 26 clickSignInButton() {27 this.elements.signInButton().click();...
Using AI Code Generation
1var wptagent = new Object();2wptagent.iframeCookies = function(domain, path, name, value, expires) {3 var iframe = document.createElement('iframe');4 iframe.style.display = 'none';5 document.body.appendChild(iframe);6 var doc = iframe.contentWindow.document;7 doc.open();8 doc.write('<html><head><title></title></head><body></body></html>');9 doc.close();10 doc.cookie = name + '=' + value + '; expires=' + expires + '; path=' + path + '; domain=' + domain;11 document.body.removeChild(iframe);12};13wptagent.iframeCookies('www.example.com', '/', 'test', 'test', 'Fri, 31 Dec 9999 23:59:59 GMT');14console.log(document.cookie
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new wpt('your api key');3wpt.iframeCookies('www.example.com', function(err, data) {4 console.log(data);5});6var wpt = require('wpt');7var wpt = new wpt('your api key');8wpt.runTest('www.example.com', function(err, data) {9 console.log(data);10});11var wpt = require('wpt');12var wpt = new wpt('your api key');13wpt.testStatus('www.example.com', function(err, data) {14 console.log(data);15});16var wpt = require('wpt');17var wpt = new wpt('your api key');18wpt.getLocations(function(err, data) {19 console.log(data);20});21var wpt = require('wpt');22var wpt = new wpt('your api key');23wpt.getTesters(function(err, data) {24 console.log(data);25});26var wpt = require('wpt');27var wpt = new wpt('your api key');28wpt.getTesters(function(err, data) {29 console.log(data);30});31var wpt = require('wpt');32var wpt = new wpt('your api key');33wpt.getTesters(function(err, data) {34 console.log(data);35});36var wpt = require('wpt');37var wpt = new wpt('your api key');38wpt.getTesters(function(err, data) {39 console.log(data);40});41var wpt = require('wpt');42var wpt = new wpt('your api key');43wpt.getTesters(function(err, data) {44 console.log(data);45});
Using AI Code Generation
1var iframeCookies = require('wptdriver').iframeCookies;2var frame = document.getElementById('iframe');3iframeCookies(frame).then(function(cookies) {4 console.log(cookies);5});6var iframeCookies = require('wptdriver').iframeCookies;7iframeCookies().then(function(cookies) {8 console.log(cookies);9});10[ { name: 'NID', value: '67=RTYU', domain: 'www.google.com', path: '/', httponly: true, secure: false, expiry: 1454221174 } ]11[ { name: 'NID', value: '67=RTYU', domain: 'www.google.com', path: '/', httponly: true, secure: false, expiry: 1454221174 } ]12[ { name: 'NID', value: '67=RTYU', domain: 'www.google.com', path: '/', httponly: true, secure: false, expiry: 1454221174 } ]13[ { name: 'NID', value: '67=RTYU', domain: 'www.google.com', path: '/', httponly: true, secure: false, expiry: 1454221174 } ]14[ { name: 'NID', value: '67=RTYU', domain: 'www.google.com', path: '/', httponly: true, secure: false, expiry: 1454221174 } ]
Using AI Code Generation
1var iframeCookies = require('./iframeCookies.js');2 build();3iframeCookies.setCookie(driver, 'cookieName', 'cookieValue', 'google.com', '/', 1000);4driver.quit();5var webdriver = require('selenium-webdriver');6var By = webdriver.By;7var until = webdriver.until;8 build();9var iframeCookies = {10 setCookie: function(driver, cookieName, cookieValue, domain, path, expiry) {11 driver.wait(until.elementLocated(By.id('iframeCookies')), 10000);12 driver.switchTo().frame(driver.findElement(By.id('iframeCookies')));13 driver.wait(until.elementLocated(By.id('setCookie')), 10000);14 driver.findElement(By.id('setCookie')).click();15 driver.wait(until.elementLocated(By.id('cookieName')), 10000);16 driver.findElement(By.id('cookieName')).sendKeys(cookieName);17 driver.wait(until.elementLocated(By.id('cookieValue')), 10000);18 driver.findElement(By.id('cookieValue')).sendKeys(cookieValue);19 driver.wait(until.elementLocated(By.id('domain')), 10000);20 driver.findElement(By.id('domain')).sendKeys(domain);21 driver.wait(until.elementLocated(By.id('path')), 10000);22 driver.findElement(By.id('path')).sendKeys(path);23 driver.wait(until.elementLocated(By.id('expiry')), 10000);24 driver.findElement(By.id('expiry')).sendKeys(expiry);25 driver.wait(until.elementLocated(By.id('submit')), 10000);26 driver.findElement(By.id('submit')).click();27 driver.switchTo().defaultContent();28 }29}30module.exports = iframeCookies;31But I am already using wait() method to wait for
Using AI Code Generation
1var driver = new WebDriver();2var iframeCookies = driver.iframeCookies();3var cookies = iframeCookies.get();4console.log(cookies);5iframeCookies.add('TestCookie', 'TestValue');6var cookies = iframeCookies.get();7console.log(cookies);8iframeCookies.delete('TestCookie');9var cookies = iframeCookies.get();10console.log(cookies);11iframeCookies.deleteAll();12var cookies = iframeCookies.get();13console.log(cookies);14driver.quit();15driver.close();16delete driver;
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!