1 回答

TA貢獻(xiàn)1921條經(jīng)驗 獲得超9個贊
解決了我的問題。問題來自測試文件。具有函數(shù)屬性的對象似乎不太適合cy.task(). (可能在嘗試引用 addtionalSteps 函數(shù)時遇到了麻煩。需要研究我的最初嘗試是否可以在 Cypress 中進(jìn)行。)相反,我在 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)
}
}
)
}
現(xiàn)在,我不需要為 CustomizedLogin 函數(shù)添加額外的變量,并且在調(diào)用 addtionalSteps 時(預(yù)計有檢查條件)。
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})
}
添加回答
舉報