Используйте случайный плагин в кипарисе - PullRequest
0 голосов
/ 08 июля 2019

Можно ли использовать случайный плагин с cypress.io?

https://chancejs.com

Я установил плагин через npm для node_modules \ chance и отредактировал файл /plugins/index.js, новсе еще получаю ошибку от кипариса - не могу начать, файл плагинов отсутствует или недействителен.

Если использование этого плагина невозможно - что порекомендуете писать тесты на основе регистрации новых пользователей?Я планировал использовать шанс генерировать «случайные»: электронные письма и пароли.

// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = on => {
  on("task", {
    chance: require("chance"),
  });
};
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config
}

1 Ответ

0 голосов
/ 09 июля 2019

cypress/support/index.js:

Переопределить стандартную команду task, чтобы вы могли указать несколько аргументов, как обычно.

Cypress.Commands.overwrite('task', (origFn, name, ...args) => {
    return origFn(name, args);
});

// if you're gonna use `chance` plugin a lot, you can also add a custom command
Cypress.Commands.add('chance', (...args) => {
    return cy.task('chance', ...args);
});

cypress/plugins/index.js:

Обернуть задачи в функцию, которая будет распространять аргументы, а также обрабатывать случай, когда задача ничего не возвращает и вместо этого возвращает null, чтобы кипарис не жаловался.

const chance = require('chance').Chance();
// lodash should be installed alongside with cypress.
// If it doesn't resolve, you'll need to install it manually
const _ = require('lodash');

on('task', _.mapValues(
    {
        chance ( method, ...args ) {
            return chance[method](...args);
        }
    },
    func => args => Promise.resolve( func(...args) )
        // ensure we return null instead of undefined to satisfy cy.task
        .then( val => val === undefined ? null : val )
));

В вашем файле спецификации:

describe('test', () => {
    it('test', () => {
        cy.document().then( doc => {
            doc.body.innerHTML = `<input class="email">`;
        });

        cy.task('chance', 'email', {domain: 'example.com'}).then( email => {
            cy.get('.email').type(email);
        });

        // or use the custom `chance` command we added
        cy.chance('email', {domain: 'test.com'}).then( email => {
            cy.get('.email').type(email);
        });
    });
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...