Skip to content

GitLab

  • Menu
Projects Groups Snippets
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • TestCafé TestCafé
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1
    • Issues 1
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Packages & Registries
    • Packages & Registries
    • Package Registry
    • Infrastructure Registry
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Microsoft SharePoint
  • SPFx
  • TestCaféTestCafé
  • Issues
  • #1

Closed
Open
Created Jun 15, 2021 by Laurent Sittler@lsittler©Owner

Support Node Authentication

Currently, the only way to authenticate the TestCafé test is to use the MSAL authentication. In other words, fill out the Microsoft authentication page with user credentials.

It works, but use another method is preferable to have an alternative if the authentication is too complicated (2FA, Key, Intune, etc.)

  • See TestCafé RequestHook to get a bearer: https://testcafe.io/documentation/402842/guides/advanced-guides/intercept-http-requests#create-a-custom-request-hook
  • An example of Cypress with localStorage: https://mechanicalrock.github.io/2020/05/05/azure-ad-authentication-cypress.html
  • Code sample:
Cypress.Commands.add("visitWithAdal", (pageUrl) => { 
  const config = {
    username: process.env.CI ? Cypress.env('USERNAME') : Cypress.env('username'),
    password: process.env.CI ? Cypress.env('PASSWORD') : Cypress.env('password'),
    tenant: process.env.CI ? Cypress.env('TENANT') : Cypress.env('tenant'),
    clientId: process.env.CI ? Cypress.env('CLIENTID') : Cypress.env('clientid'),
    clientSecret: process.env.CI ? Cypress.env('CLIENTSECRET') : Cypress.env('clientsecret'),
    resource: process.env.CI ? Cypress.env('RESOURCE') : Cypress.env('resource')
  };

  // Fetch the access token for the Microsoft Graph
  cy.request({
    method: 'POST',
    url: `https://login.microsoft.com/${config.tenant}/oauth2/token`,
    header: {
       'cache-control': 'no-cache',
       'Content-Type': 'application/x-www-form-urlencoded'
    },
    form: true,
    body: {
      grant_type: 'password',
      client_id: config.clientId,
      client_secret: config.clientSecret,
      resource: config.resource,
      password: config.password,
      username: config.username
    }
  }).then(response => {
    if (response && response.status === 200 && response.body) {
      const accessToken = response.body["access_token"];
      const expires = response.body["expires_on"];
      // Store the retrieved access token in the session storage
      cy.window().then((crntWindow) => {
        crntWindow.sessionStorage.setItem(`adal.token.keys`, `${config.resource}|`);
        crntWindow.sessionStorage.setItem(`adal.expiration.key${config.resource}`, expires);
        crntWindow.sessionStorage.setItem(`adal.access.token.key${config.resource}`, accessToken);
        
        cy.visit(pageUrl);
      });
    }
  });
});
Edited Mar 18, 2022 by Laurent Sittler
Assignee
Assign to
Time tracking