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
版权声明:本文标题:cypress - How to ensure custom headers are not lost during redirects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736617791a1945509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论