简体   繁体   English

赛普拉斯拦截

[英]Cypress intercept

I am novice in automation and having a problem in cypress intercept method.我是自动化方面的新手,在柏树拦截方法中遇到问题。 I have to achieve following thing:我必须实现以下目标:

1- visit: https://opensource-demo.orangehrmlive.com 2- enter login credentials 3- on the click of l0gin button "I have to validate the entered username" through intercept method. 1- 访问: https://opensource-demo.orangehrmlive.com 2- 输入登录凭据 3- 单击 l0gin 按钮“我必须通过拦截方法验证输入的用户名”。 Following is the my code以下是我的代码

describe("cypressAssignment", ()=>{
it('Login a user and validate his name via intercept', ()=>{

    cy.intercept('POST', '/auth/login', (req) => {
        expect(req.body).to.include('Admin')
    }).as('username')
      
    cy.visit('https://opensource-demo.orangehrmlive.com')

    
    cy.get('form').within(($form)=>{
        cy.get('#divUsername').type('Admin')
        cy.get('#divPassword').type('admin123')

        cy.get('#btnLogin').click()

    })
  cy.wait('@username').its('response.body').should('to.have.property', 'name').and('include', 'Admin')

   

    cy.url().should('include','dashboard')
   

})

I'd go for something like this.对于这样的事情,我会拨打 go。 )Updated based on additional information in the comment section.) )根据评论部分中的其他信息进行更新。)

it('SO Answer', () => {
    cy
      .visit('/');

    cy
      .intercept('POST', '**/auth/validateCredentials')
      .as('loginRoute');

    cy
      .get('#txtUsername')
      .type('Admin');

    cy
      .get('#txtPassword')
      .type('admin123');

    cy
      .get('#btnLogin')
      .click();

    cy
      .wait('@loginRoute')
      .its('request.body')
      .should('include', 'txtUsername=Admin');
});

To see the whole test case in the Test Runner:在 Test Runner 中查看整个测试用例:

在此处输入图像描述

The app renders HTML on the server side, so the response to the login action is a 302 redirection to /index.php/dashboard and a complete HTML sent in the response, so you can't check any username in the response in this case.该应用程序在服务器端呈现 HTML,因此对登录操作的响应是到 /index.php/dashboard 的 302 重定向以及在响应中发送的完整/index.php/dashboard ,因此在这种情况下您无法检查响应中的任何用户名.

This is what I done after your response @pavelsaman.这是我在您回复@pavelsaman 后所做的。 Now I am getting ideas how to play with intercept:现在我得到了如何使用拦截的想法:

describe("cypressAssignment", ()=>{
it('Login a user and validate his name via intercept', ()=>{

    cy.intercept('POST','**/auth/validateCredentials', (request) => { expect(request.body).to.include('txtUsername=Admin')
    })

    cy.visit('/')
    
    cy.get('form').within(($form)=>{
        cy.get('#txtUsername').type('Admin')
        cy.get('#txtPassword').type('admin123')
        cy.get('#btnLogin').click()

    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM