首页 > 解决方案 > 在 cypress 中使用机会插件

问题描述

是否可以将机会插件与 cypress.io 一起使用?

https://chancejs.com

我通过 npm 将插件安装到 node_modules\chance 并编辑了 /plugins/index.js 文件,但仍然从 cypress 收到错误 - 无法启动,插件文件丢失或无效。

如果无法使用此插件-您建议根据注册新用户编写测试什么?我计划利用机会生成“随机:电子邮件和密码。

// ***********************************************************
// 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
}

标签: javascriptcypress

解决方案


只需使用 npm 安装 chancejs:

npm install chance

然后只需在您的测试中使用它,例如:

/// <reference types="cypress" />

import Chance from 'Chance';

const chance = new Chance();

describe('Testing chance', function (){
   const company =chance.company();
   it('type company in duckduckgo.com', function () {
        cy.visit('https://duckduckgo.com/')
        cy.get('#search_form_input_homepage')
        .should('be.visible')
        .type(company)
    })
})

推荐阅读