Authenticating via Third Party Providers with Cypress Social Login Plugin

  1. Get the Plugin Installed for your cypress project
npm install --save-dev cypress-social-logins
const { GoogleSocialLogin, GitHubSocialLogin } = require(‘cypress-social-logins’).plugins/*** @type {Cypress.PluginConfig}*/module.exports = (on, config) => {// `on` is used to hook into various events Cypress emits// `config` is the resolved Cypress configon(‘task’, {GoogleSocialLogin: GoogleSocialLogin,GitHubSocialLogin: GitHubSocialLogin})}
Cypress.Commands.add(‘userLoginWithGmail’, () => {const socialLoginOptions = {username: ‘test@gmail.com’,password: ‘test@1234’,loginUrl: ‘https://mysample.app.com/login',headless: false,logs: true,loginSelector: ‘button[type=”button”][data-testid=”login-page-sign-in-with-google”]’,postLoginSelector: “[data-testid=side-panel-items-applications]”,popupDelay: 3000,cookieDelay: 2000,args: [‘ — disable-web-security’, ‘ — user-data-dir’, ‘ — allow-running-insecure-content’],isPopup: true,getAllBrowserCookies: true}cy.task(‘GoogleSocialLogin’, socialLoginOptions).then(({ cookies, lsd, ssd }) => {cookies.map((cookie) => {cy.setCookie(cookie.name, cookie.value, {domain: cookie.domain,expiry: cookie.expires,httpOnly: cookie.httpOnly,path: cookie.path,secure: cookie.secure})Cypress.Cookies.defaults({preserve: cookie.name})})cy.window().then(window => {Object.keys(ssd).forEach(key => window.sessionStorage.setItem(key, ssd[key]))Object.keys(lsd).forEach(key => window.localStorage.setItem(key, lsd[key]))})cy.log(‘login successful.’)cy.visit(‘/’);})})
describe(“1.1- Sign in with Google”,function () {it(“1.1.1- Sign in with Google Sample Test”, () => {cy.userLoginWithGmail();});});
username = Email address for the Google account that you need to authenticatepassword = Password for the Google account that you need to authenticateloginUrl = The URL for the login page that includes the social network buttonsargs = String array which allows providing further arguments to puppeteerheadless = Whether to run puppeteer in headless mode or notloginSelector = A selector on the page that defines the specific social network to use and can be clicked, such as a button or a linkpostLoginSelector = A selector on the post-login page that can be asserted upon to confirm a successful logingetAllBrowserCookies = Whether to get all browser cookies instead of just ones with the domain of loginUrlisPopup = Boolean, is your google auth displayed like a popuppopupDelay = number, delay a specific milliseconds before popup is shown. Pass a falsy (false, 0, null, undefined, ‘’) to avoid completelycookieDelay = number, delay a specific milliseconds before get a cookies. Pass a falsy (false, 0, null,undefined,’’) to avoid completely

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shanika Wickramasinghe

Shanika Wickramasinghe

Senior Software Engineer and Freelance Technical Writer. I write about any Computer Science related topic. https://www.linkedin.com/in/shanikawickramasinghe