1 回答
TA贡献1921条经验 获得超9个赞
解决了我的问题。问题来自测试文件。具有函数属性的对象似乎不太适合cy.task(). (可能在尝试引用 addtionalSteps 函数时遇到了麻烦。需要研究我的最初尝试是否可以在 Cypress 中进行。)相反,我在 Cypress 插件索引文件中添加了additionStep 检查。
赛普拉斯插件:
const {customizedLogin} = require('../../src/Plugins').CustomizedLogin
async function fewMoreSteps({page, options} = {}) {
console.log('This is the addtional step...')
await page.waitForSelector('#header_notification_link', {visible: true, timeout: 6000})
await page.click('#header_notification_link')
}
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on('task', {
customizedLogin: (options) => {
if (options.moreSteps) {
options.additionalSteps = fewMoreSteps
}
return CustomizedLogin(options)
}
}
)
}
现在,我不需要为 CustomizedLogin 函数添加额外的变量,并且在调用 addtionalSteps 时(预计有检查条件)。
module.exports.CustomizedLogin = async function CustomizedLogin(options = {}) {
if (options.usernameField && options.passwordField) {
const typeUsername = async function({page, options} = {}) {
await page.waitForSelector(options.usernameField, {visible: true})
await page.type(options.usernameField, options.username)
if (options.usernameSubmitBtn) {
await page.click(options.usernameSubmitBtn)
}
}
const typePassword = async function({page, options} = {}) {
await page.waitForSelector(options.passwordField, {visible: true})
await page.type(options.passwordField, options.password)
if (options.passwordSubmitBtn) {
await page.click(options.passwordSubmitBtn)
}
}
const postLogin = async function ({page, options} = {}) {
await page.waitForSelector(options.postLoginClick)
await page.click(options.postLoginClick)
}
return baseLoginConnect(typeUsername, typePassword, null, null, postLogin, options)
} else {
throw new Error('Please review your option properties. Propeties usernameField and passwordField are required as type String.')
}
}
// When options.addtionalSteps is invoked
if (typeof options.additionalSteps !== 'undefined') {
await options.additionalSteps({page, options})
}
添加回答
举报