尝试自动登录时赛普拉斯发布请求失败

时间:2021-05-07 05:30:59

标签: javascript testing cypress ui-automation e2e-testing

我正在尝试在 Cypress 中发出 POST 请求以执行自动登录并重定向到仪表板页面。 这是

错误

enter image description here

我添加的用于执行自动登录的代码是

Cypress.Commands.add('autologin', () => {
  cy.clearCookies();
  let options = {
    method: 'POST',
    url: 'https://admin-demo.nopcommerce.com/login?returnurl=%2Fadmin%2F',
    headers: {
      'authority': 'admin-demo.nopcommerce.com',
      'Content-Type': 'application/x-www-form-urlencoded',
      'origin': 'https://admin-demo.nopcommerce.com',
    },
    body: {
      Email: 'admin@yourstore.com',
      Password: 'admin',
      __RequestVerificationToken:
        'CfDJ8EoKK8bdbeVGtbyUmFkZjVXayQ00hwk5B6a1cPRgR3HvKKdGXLUNrX2hLPzPqPWV-RDY0zU-qaOalPnt9-fPC7xnshZqx1MMB_2aKitiCzSmm4CstnsQSi3YfrYGFzMVhck6tI02IznyqRUto2akvAA',
      RememberMe: false,
    },
  };
  cy.request(options).then(response => {
    console.log(response);
  });
});
Cypress.Commands.add('login', () => {
  cy.autologin();
  cy.visit('https://admin-demo.nopcommerce.com');
  cy.get('#nopSideBarPusher').should('be.visible').click();
  cy.get('#nopSideBarPusher').should('be.visible').click();
  cy.clearCookies();
  cy.get('.navbar-collapse .nav-link')
    .eq(1)
    .then(val => {
      cy.get(val).click();
    });
});

1 个答案:

答案 0 :(得分:1)

__RequestVerificationToken 的目的是它随着每次页面刷新而变化,所以如果你在那里添加这个字符串,服务器拒绝整个请求是有意义的。给定时间的有效令牌数量有限,在您发送请求时,此令牌可能已无效。