SharePoint Online O365 Authentication Automation Testing Using Cypress.io



Prerequisite

Initially, Node.js must be installed on the system, as Cypress is a Node.js - based application. Node.js is a JavaScript runtime environment. You can download and install Node.js from the Node.js download page.
Verify the Node.js installation by running the command: node -version in the terminal. Verify the npm version by running the command: npm - version.
It is also necessary to have a code editor like Microsoft's Visual Studio Code to make programming and project management easier.
For users using npm to install Cypress, Cypress supports Node.js 10 and above.

How to install Cypress
To install Cypress using the npm (Node package manager) navigate to the project directory and execute the following command.

npm init

The command above creates the package.json file. Then run the following command to install Cypress.

npm install cypress --save-dev

The command above installs Cypress locally as a dev dependency for the project.

SharePoint Authentication
With Cypress, you can test React and Angular applications as well as other types of applications. The tool was created around the idea of creating quick reliable, and powerful testing frameworks that run within a browser.

How it works for SharePoint?

  • You will need a username and password (most likely a managed account), and you will have to complete the full authentication loop.
  • Even when you implement the authentication flow, you nevertheless run into an authentication issue. The reason for this is that the page you will open does not get the authentication cookies that SharePoint needs.

How to solve this Authentication Issue?

  • First You need to install node-sp-auth dependency.
    Run below command:
    npm install node-sp-auth --save-dev

  • Write below code in Cypress.config.js
    const { defineConfig } = require("cypress");
    const spauth = require("node-sp-auth");
    
    let getLoginTest = async () => {
    
      const username = "********"; //Here you need to add username/email
      const password = "********"; //Here you need to add password
      const pageUrl = https://*****.sharepoint.com/; //add site URL
    
      // Connect to SharePoint
      const data = await spauth.getAuth(pageUrl, {
        username: username,
        password: password
      });
      return data;
    };
    
    module.exports = defineConfig({
    viewportWidth:1920,
    viewportHeight:1080,
      e2e: {
        setupNodeEvents(on, config) {
          // implement node event listeners here
          on('task', {
            // deconstruct the individual properties
            async getLogin() {
              try {
                const res = await getLoginTest();
                return res;
              } catch (error) {
                return error;
              }
            }
          });
        },
      },
    });

  • Write below code in Sharepointlogin.cy.js
    /// <reference types="cypress" />
    require('cypress-xpath')
    
    describe('SharePoint Authentication', () => {
        before(() => {
            cy.task("getLogin").then(token => {
                cy.visit({
                    method: "GET",
                    url: "https://******.sharepoint.com/****/Pages/****-Automation-Test.aspx",
                    headers: token.headers
                });
            });
        });
    
    //here you need to add some testcases
    
        it('SharePoint Authentication Test', () => {
            cy.get('.chat_button > .fa').click();
            cy.xpath("//p[contains(text(),'Hi, how may i serve you?')]").first().should('be.visible');     
        });
    });
    

Result

Now you can open Cypress from your project root in terminal with following command.

 npx cypress open

 

Add comment

BlogList

MonthList

TagCloud

BlogList

MonthList

TagCloud