admin管理员组

文章数量:1124155

I'm working with Cypress and trying to automate a login process where I need to read the login credentials (username and password) from an Excel file and use them in my test. However, there is a Cloudflare CAPTCHA page in the middle, and I need to send custom headers to bypass it. I've tried few different methods but since I'm new to cypress and still learning coding, I'm a bit lost. I got the custom headers I need to use to bypass the cloudflare page but I dont know if I added it correctly? Also should add the headers in command.js file?

Below I have added a what I have so far:

command.js

import ExcelJS from 'exceljs';

Cypress.Commands.add('getLoginDetails', () => {
  // Read the Excel file from the fixtures folder
  cy.readFile('cypress/fixtures/loginDetails.xlsx', 'binary') 
    .then((fileContent) => {
      const workbook = new ExcelJS.Workbook();
      
      // Load the binary content into ExcelJS workbook
      return workbook.xlsx.load(fileContent).then(() => {
        const worksheet = workbook.getWorksheet(1);
        
        const username = worksheet.getCell('A2').text; 
        const password = worksheet.getCell('B2').text; 
        
        return { username, password };

      });
    });
});

testlogin.js

import ExcelJS from 'exceljs';

describe('Login Test', () => {
    it('Logs in using credentials from the spreadsheet', () => {

        cy.visit('www.testurl');

        cy.getLoginDetails().then((loginDetails) => {
            const { username, password } = loginDetails;

            // Enter username and password into the login form
            cy.get('[data-testid="username"]').type(username);
            cy.get('[data-testid="password"]').type(password);

            // Click the login button
            cy.intercept('POST', '/useraccount/', (req) => {
                req.headers['headerKey'] = 'headerValue';
                req.continue();
            });
            cy.get('[data-testid="login-btn"]').click();

            //uncaught exception
            Cypress.on('uncaught:exception', (err, runnable) => {
                console.log('Uncaught exception:', err);
                return false; 
            });

        });

    });
});

本文标签: cypressHow to ensure custom headers are not lost during redirectsStack Overflow