简体   繁体   English

如何使用 cy.intercept() 使用不同的存根存根两个请求?

[英]how to stub two requests with differents stubs using cy.intercept()?

I am trying to stub the same http GET request using two cy.intercept functions with different responses.我正在尝试使用两个具有不同响应的 cy.intercept 函数来存根相同的 http GET 请求。 One way I tried to do that is to use a conditional if statement.我尝试这样做的一种方法是使用条件 if 语句。 Inside that if statement, I would call the cy.intercept function.在 if 语句中,我将调用 cy.intercept function。 I used a boolean variable as the condition.我使用 boolean 变量作为条件。 The problem is that the boolean variable does not change based on the test scenario (I am using cypress with the cypress-cucumber-preprocessor).问题是 boolean 变量不会根据测试场景而改变(我正在使用带有 cypress-cucumber-preprocessor 的 cypress)。 How can I implement my test file such that it defines the condition as true or false depending on the test, thus in turn, dynamically defining a different cy.intercept response?如何实现我的测试文件,使其根据测试将条件定义为真或假,从而动态定义不同的 cy.intercept 响应?

my test file:我的测试文件:

 let isValid = false

 Given('I am on the "user-login" page', () => {
     cy.log(isValid)
     cy.visit("http://localhost:8080/user-login")
     cy.title().should('eq',"User Login Page")
     isValid = true
     cy.log(isValid)
 })

 Given('I am on the "user-login" page', () => {
     cy.log(isValid)
     cy.visit("http://localhost:8080/user-login")
     cy.title().should('eq',"User Login Page")

     isValid = false
     cy.log(isValid)
 })

 When('I enter "George312"', () => {
     
     cy.get('input[type="text"]').should("be.visible").type("George312")
 })

 When('I enter "George312"', () => {
     cy.get('input[type="text"]').should("be.visible").type("George312")
 })


 And('I enter "hsj%2*sc5$"', () => {

     cy.get('input[type="password"]').should("be.visible").type("hsj%2*sc5$")   
 })

 And('I enter "hsj%2*sc5$3"', () => {

     cy.get('input[type="password"]').should("be.visible").type("hsj%2*sc5$3")   
 })


 And('I Click the "Submit" button', () => {
     if(isValid === true){
         cy.intercept('api/users',
         {
             "body": { "isAuthenticated": true}
         }
       ).as("loginUser")
     }
     
     cy.get('button[id="LoginBtn"]').should('be.visible').click()
     cy.wait(2000)
     cy.wait("@loginUser")
 })


 And('I Click the "Submit" button', () => {
     isValid = false
     if(isValid === false){
         cy.intercept('api/users',
         {
             "body": { "isAuthenticated": false}
         }
       ).as("loginUser")
     }
     cy.get('button[id="LoginBtn"]').should('be.visible').click()
     cy.wait(2000)
     cy.wait("@loginUser")
 })


 Then('I should see written in a window user "George312 is now logged in!"', () => {

     cy.get("p").contains('user "George312 is now logged in!"').should("be.visible")


 })

 Then('I should see written in a window user "Login Failed! wrong password"', () => {

     cy.get("modal").contains("Login Failed! wrong password").should("be.visible")
 })

cy.log() is like console.log(). cy.log() 就像 console.log()。 I have indicated in red the output of the four calls of cy.log() in my code.我在我的代码中用红色表示了 cy.log() 的四个调用的 output。 The output does not make sen Here is cypress' output: output 不做 sen 这里是 cypress 的 output: 在此处输入图像描述

cy.log() is like console.log(). cy.log() 就像 console.log()。 I have indicated in red the output of the four calls of cy.log() in my code.我在我的代码中用红色表示了 cy.log() 的四个调用的 output。 The output does not make sense. output 没有意义。 it's as if the variable is set to true and never changes afterwards.就好像变量被设置为 true 并且之后永远不会改变。

I have found the solution.我找到了解决方案。 The variable that I am declaring has to be declared this way: this.isValid so it could be accessed else where in the file.我要声明的变量必须以这种方式声明: this.isValid ,以便可以在文件中的其他位置访问它。 Secondly: The Given() statements should be different from each other.其次: Given() 语句应该彼此不同。 Otherwise both will activated on both scenarios causing the variable value to be overriden in the last initialization.否则,两者都将在两种情况下都被激活,导致变量值在最后一次初始化中被覆盖。

as such:像这样:

Given(/^I am on the "user-login" 1 page$/, () => {
    cy.visit("http://localhost:8080/user-login")
    cy.title().should('eq',"User Login Page")
    this.isValid = true
    cy.log(this.isValid)
})
Given(/^I am on the "user-login" page$/, () => {
    cy.log(this.isValid)
    cy.visit("http://localhost:8080/user-login")
    cy.title().should('eq',"User Login Page")

    this.isValid = false
    cy.log(this.isValid)
})

and then cypress output becomes this:然后 cypress output 变成这样:

在此处输入图像描述

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

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