收集来自多个柏树的结果 [英] Gather results from multiple cypress promisses

查看:96
本文介绍了收集来自多个柏树的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cypress.io中是否可以在 .then 构造内收集多个断言的结果,以便可以在之外使用结果。 code>?

Is it possible in Cypress.io to gather results of multiple assertions inside .then construction so that results can be used outside .then?

基于以下示例-如果我有一些要进行烟熏测试的页面(例如,检查代码是否不同于404) )如何收集有关他们的信息?如何在一次烟雾测试中一起验证结果?

Based on below example - if I have some pages that I would like to smoke-test (to check eg. if code is different than 404) how to gather information about them? How to verify results all together in one smoke test?

请看一下显示问题的简单代码:

Please look at this simple piece of code that shows the problem:

describe('Smoke tests for some pages', () => {
    it('should be able to view page that was loaded correctly', () => {
      // Arrange:
      const pageAddressList = [
        'https://en.wikipediaaa.org',
        'https://en.wikipedia.org'];
      const errors = Array();

      // Act:
      pageAddressList.forEach((pageAddress) => {
        cy.request(pageAddress).then((response) => {
            // check response and add some error to errors if needed
        })

      });
      // Assert:
      // check if errors is empty
    });
});




  1. 以上方法是否正确?

  2. 每个页面都应该有单独的测试吗?

  3. 如果我要检查50多个页面怎么办?

  4. 赛普拉斯的最佳方法是什么?

  1. Is above approach correct?
  2. Should there be separate tests for each page?
  3. What if I have 50+ pages to check?
  4. What is best approach in Cypress.io in such case?


推荐答案

编辑:使用 Promise.all ,这不是正确的解决方案。赛普拉斯的链接器对象不是 Promises / A + 兼容的,它们看起来像是承诺,因为它们实现了。然后界面。这就是 Promise.all 能够使用一系列链接器对象的原因,仅此而已。传递给 Promise.all()。then 的分辨率值不会达到您的期望(请参阅 https://github.com/cypress-io/cypress/issues/915 )。

often someone suggests using Promise.all, which is not a correct solution. Cypress chainer objects aren't Promises/A+-compatible, they simply appear to be promises because they implement .then interface. That's why Promise.all is able to consume an array of chainer objects, but that's it. The resolution values passed to Promise.all().then callback is not going to be what you expect (see https://github.com/cypress-io/cypress/issues/915).

您可以在类似的答案中使用我建议的帮助器

You can use a helper I've suggested in a similar answer:

// put this in cypress/support/index.js

const chainStart = Symbol();
cy.all = function ( ...commands ) {
    const _           = Cypress._;
    const chain       = cy.wrap(null, { log: false });
    const stopCommand = _.find( cy.queue.commands, {
        attributes: { chainerId: chain.chainerId }
    });
    const startCommand = _.find( cy.queue.commands, {
        attributes: { chainerId: commands[0].chainerId }
    });
    const p = chain.then(() => {
        return _( commands )
            .map( cmd => {
                return cmd[chainStart]
                    ? cmd[chainStart].attributes
                    : _.find( cy.queue.commands, {
                        attributes: { chainerId: cmd.chainerId }
                    }).attributes;
            })
            .concat(stopCommand.attributes)
            .slice(1)
            .flatMap( cmd => {
                return cmd.prev.get('subject');
            })
            .value();
    });
    p[chainStart] = startCommand;
    return p;
}

用法:

it('test', () => {
    const urls = [
        'https://en.wikipediaaa.org',
        'https://en.wikipedia.org'
    ];

    cy.all(
        ...urls.map(url => cy.request(url))
    ).then(responses => {
        responses.forEach( resp => {
            expect(resp.status).to.eq(200);
        });
    });
});

话虽如此,您也可以这样做:

That being said, you can also do this:

const urls = [
    'https://en.wikipediaaa.org',
    'https://en.wikipedia.org'
];

let passes = 0;

urls.forEach( url => {
    cy.request(url).then( resp => {
        if ( resp.status === 200 ) passes++;
    });
});

cy.then(() => {
    expect(passes).to.eq(urls.length);
});

顶部的 cy.all 助手确实如果您想访问结果而不必在全局范围内访问并从 cy.then()回调中访问它们,则很有用---但就像我在上一节中显示的那样例如,一切都可以仅使用香草柏来解决。

The cy.all helper atop is really useful if you want to access the results without having to keep around globals and accessing them from a cy.then() callback --- but like I've shown in the last example, everything can be worked around using just vanilla cypress.

或者,如果您根本不需要响应,则只需执行以下操作:

Or, if you don't need responses at all, you can simply do:

const urls = [
    'https://en.wikipediaaa.org',
    'https://en.wikipedia.org'
];
urls.forEach( url => {
    cy.request(url).its('status').should('eq', 200);
});

这篇关于收集来自多个柏树的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆