Как получить значения нескольких псевдонимов, не вводя ад в обратном вызове в Cypress? - PullRequest
0 голосов
/ 22 марта 2019

Скажем, я хочу получить значения двух псевдонимов Cypress и использовать их в моем тестовом примере.Как это сделать, не вкладывая их следующим образом?

cy.get('@alias1')
    .then((alias1) => {
        cy.get('@alias2').then((alias2) => {
            someFunctionThatUsesBothAliases(alias1, alias2);
        })
    })

Ответы [ 2 ]

2 голосов
/ 23 марта 2019

Вы можете сделать это:

it('test', () => {
    cy.wrap('foo').as('foo');
    cy.wrap('bar').as('bar');
    cy.wrap('baz').as('baz');

    const values = [];
    cy.get('@foo').then( val => {
        values.push(val);
        return cy.get('@bar');
    }).then( val => {
        values.push(val);
        return cy.get('@baz');
    }).then( val => {
        values.push(val);
        someFunc(...values);
    });
});

Или вы можете использовать этот помощник, который я собрал вместе (поместите его в ваш 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', () => {

    cy.wrap('one').as('one');
    cy.wrap('two').as('two');
    cy.wrap('three').as('three');

    cy.all(
        cy.get(`@one`),
        cy.get(`@two`),
        cy.get(`@three`)
    ).then( values => {
        someFunc(...values);
    });
});
1 голос
/ 25 марта 2019

Одно другое решение, разработанное на заказ Mr.Николас Болл , где вы получите псевдонимы в виде массива с помощью пользовательских команд createAlias/ getAliases,

// get many aliases - API is similar to Promise.all
cy.getAliases([getFoo, getBar, getOne]).then(([foo, bar, one]) => {
  foo // string
  bar // string
  one // number
  console.log(foo, bar, one) // logs 'foo', 'bar', 1
})

Вот полная информация в его блоге - https://medium.com/@NicholasBoll/cypress-io-making-aliases-type-safe-b6f5db07d580

Ссылка на код- https://github.com/NicholasBoll/cypress-example-todomvc/tree/feat/type-safe-alias

...