Skip to content

Commit

Permalink
feat: add option to set cookies before visiting loginUrl (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascalmh committed Apr 8, 2022
1 parent 47149ed commit ee43739
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Options passed to the task include:
| screenshotOnError | Optional: will grab a screen shot if an error occurs on the username, password, or post-login page and saves in the Cypress screenshots folder. | false |
| additionalSteps | Optional: function, to define any additional steps which may be required after executing functions for username and password, such as answering security questions, PIN, or anything which may be required to fill out after username and password process. The function and this property must be defined or referenced from index.js for Cypress Plugins directory. | `async function moreSteps({page, options} = {}) { await page.waitForSelector('#pin_Field') await page.click('#pin_Field') }` |
| trackingConsentSelectors | Optional: selectors to find and click on after clicking the login button, but before entering details on the third-party site (useful for accepting third-party cookies e.g. Facebook login). Provide multiple if wanting to accept only essential cookies and it requires multiple clicks | `['button[data-testid="cookie-policy-dialog-manage-button"]', 'button-data-testid="cookie-policy-manage-dialog-accept-button"]']` |
| preVisitLoginUrlSetCookies | Optional: array of cookies to set before visiting the `loginUrl` | `[{name: 'enable-social-login', value: 'true', domain: '.cypress.io'}]` |

## Install

Expand Down
6 changes: 6 additions & 0 deletions src/Plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const fs = require('fs')
* @param {options.password} string password
* @param {options.loginUrl} string password
* @param {options.loginUrlCredentials} Object Basic Authentication credentials for the `loginUrl`
* @param {options.preVisitLoginUrlSetCookies} array[{name: string, value: string, domain: string}] cookies to set before visiting `loginUrl`
* @param {options.args} array[string] string array which allows providing further arguments to puppeteer
* @param {options.loginSelector} string a selector on the loginUrl page for the social provider button
* @param {options.loginSelectorDelay} number delay a specific amount of time before clicking on the login button, defaults to 250ms. Pass a boolean false to avoid completely.
Expand Down Expand Up @@ -203,6 +204,11 @@ async function baseLoginConnect(
if (options.loginUrlCredentials) {
await page.authenticate(options.loginUrlCredentials)
}

if (options.preVisitLoginUrlSetCookies && options.preVisitLoginUrlSetCookies.length > 0) {
await page.setCookie(...options.preVisitLoginUrlSetCookies)
}

await page.goto(options.loginUrl)
await login({page, options})

Expand Down

0 comments on commit ee43739

Please sign in to comment.