赛普拉斯请求是异步的-但是如何创建新对象呢?

时间:2019-05-27 12:16:23

标签: javascript testing async-await mocha cypress

我尝试使用赛普拉斯执行请求,然后使用响应中接收到的值创建新对象,但是立即创建了新对象,并在所有对象都已创建之后发送请求:

describe("example", function() {

    it("of test", function() {

        console.log("1");

        cy.request({
            url: Cypress.env("URL"),
            method: "POST",
            headers: auth_req.authHeaders,
            body: merged_body
        })
            .as("/authorize")
            .then((resp) => {
                console.log("2");
                Cypress.env("resp_code", resp.status);
                console.log("RESP CODE FROM RESPONSE:");
                console.log(resp.status);
            })
            .its("headers")
            .its("content-type")
            .should("include", "application/json");

        console.log("3");
        const var1 = new something.NotImportant;
        console.log("4");
        const var2 = new something.AlsoNotImportant;
        console.log("5");

    }

}

我希望在控制台中获得“ 1”,“ 2”,“ 3”,“ 4”和“ 5”。但是,我几乎立即得到“ 1”,“ 3”,“ 4”和“ 5”,几秒钟后(服务器响应缓慢),我看到请求已发送,并且向我吐出了“ 2”控制台。

我的问题是:如果我正确理解了该请求,则请求是异步的,而赛普拉斯正在等待它们完成,那么为什么测试不等待接收响应并然后创建对象呢?如何修改代码以确保在创建新对象期间要在请求后创建的对象正在使用从请求中接收的值?我尝试使用Cypress.env,但是在请求之前创建了对象,并且使用了错误的值,而不是从响应中读取的值。


奖金问题:为什么此代码无法按预期方式工作(挂起计算机)?

    while (true) {
        cy.wait(5000);
        var resp_code = Cypress.env("resp_code");
        console.log("RESP CODE:");
        console.log(Cypress.env("resp_code"));
    }

“ RESP CODE”每隔100毫秒打印一次到控制台,我想这是因为测试是异步的,因此我尝试在此处使用同步方法。我说得对吗?

1 个答案:

答案 0 :(得分:0)

因为它是异步的,所以您必须保留链接命令以将它们排队。赛普拉斯确实会等待命令(承诺)完成,但前提是您用then()来告知它。

您可以执行以下操作:

describe("example", function() {

    it("of test", function() {

        console.log("1");

        cy.request({
            url: Cypress.env("URL"),
            method: "POST",
            headers: auth_req.authHeaders,
            body: merged_body
        })
            .as("/authorize")
            .then((resp) => {
                console.log("2");
                Cypress.env("resp_code", resp.status);
                console.log("RESP CODE FROM RESPONSE:");
                console.log(resp.status);

                console.log("3");
                const var1 = new something.NotImportant;
                console.log("4");
                const var2 = new something.AlsoNotImportant;
                console.log("5");
            })
            .its("headers")
            .its("content-type")
            .should("include", "application/json"); 
    }

}

或者这个:

describe("example", function() {

    it("of test", function() {

        console.log("1");

        cy.request({
            url: Cypress.env("URL"),
            method: "POST",
            headers: auth_req.authHeaders,
            body: merged_body
        })
            .as("/authorize")
            .then((resp) => {
                console.log("2");
                Cypress.env("resp_code", resp.status);
                console.log("RESP CODE FROM RESPONSE:");
                console.log(resp.status);
            })
            .its("headers")
            .its("content-type")
            .should("include", "application/json")
            .then(() => {
                console.log("3");
                const var1 = new something.NotImportant;
                console.log("4");
                const var2 = new something.AlsoNotImportant;
                console.log("5");
            }); 
    }

}