English 中文(简体)
自定义命令和最终未定义的额外数据
原标题:Custom command and extra data that ends up as undefined
  • 时间:2023-07-14 00:49:06
  •  标签:
  • cypress

我试图覆盖方法中的Cypress,以便为回调提供一个额外的参数。

Ultimately, my goal is to be able to go cy.customComm和ThatSetsAValue().within((subject, extra) => {})

这个问题似乎是时间问题。。。每当我试图传递一个非硬编码的值时,它都会以未定义的形式传递。我已经尝试了许多排列,似乎即使在.then()回调中设置变量时,当它到达自定义命令时,它也是未定义的。

这是我的超控:

function within<Subject>(
    originalWithinFn: Cypress.Comm和OriginalFnWithSubject< within , Subject>,
    prevSubject: Subject, 
    optionsOrFn: Partial<Cypress.Loggable> | WithinCallback,
    fnOrUndefined?: WithinCallback,
){
    let fn: WithinCallback;
    let options: Partial<Cypress.Loggable> | undefined;
    if (optionsOrFn instanceof Function && fnOrUndefined === undefined) {
        fn = optionsOrFn;
    } else if (fnOrUndefined instanceof Function) {
        options = optionsOrFn;
        fn = fnOrUndefined;
    } else {
        throw new Error( Invalid arguments provided to cy.within );
    }
    return getMostRecentNavigationShorth和().then(mostRecentShorth和 => originalWithinFn(prevSubject, options!, (subject) => fn(subject, mostRecentShorth和)));
}

Cypress.Comm和s.overwrite( within , within);

customComm和ThatSetsAValue()的简化版本

return cy.get( foo ).then(() => {mostRecentShorth和 = anObject});

function getMostRecentNavigationShorth和() {
    return new Cypress.Promise((resolve, reject) => resolve(mostRecentShorth和));
}

我已尝试将中的返回更改为

return cy.then(() => 
    getMostRecentNavigationShorth和()
        .then(mostRecentShorth和 => 
            originalWithinFn(prevSubject, options!, (subject) => fn(subject, mostRecentShorth和)
)));

以及尝试将cy.then包装在promise周围(或者使用它来代替promise)。无论我做什么,它在实际执行时仍然是未定义的。

知道我是如何/为什么一直没有定义或如何修复它吗?谢谢

问题回答

如果我把所有的代码都放到一个测试中,它就通过了。不知道anObject是什么,所以我只使用了{abc:123}

let mostRecentShorthand = null

function getMostRecentNavigationShorthand() {
  return new Cypress.Promise((resolve, reject) => resolve(mostRecentShorthand));
}

function within(originalWithinFn, prevSubject, optionsOrFn, fnOrUndefined) {

  // parameter resolving

  return getMostRecentNavigationShorthand()
    .then(mostRecentShorthand => {
      return originalWithinFn(prevSubject, options, (subject) => 
        fn(subject, mostRecentShorthand)
      )
    })
}
Cypress.Commands.overwrite( within , within)

Cypress.Commands.add( customCommandThatSetsAValue , () => {
  return cy.get( h1 ).then(() => {mostRecentShorthand = {abc: 123}})
})

it( gives an extra parameter to within , () => {
  cy.visit( https://example.com );
  cy.customCommandThatSetsAValue().within((subject, extra) => {
    expect(extra).to.deep.eq({abc: 123})
  })
})

您不需要Cypress.Promise块,因为您可以同步设置值,立即解析,并且从不拒绝。但它不会导致额外的参数被<code>未定义anObject的值未定义时,才会发生这种情况。





相关问题
Cypress 在自定义命令上无法工作。

我在support/header.js下创建一个新文件,用于重复检查头文件,我还将它导入到e2e.js中。 header.js Cypress.Commands.add(checkHeader, () => {     //* FAVICON     it(Check Favicon 1,(...

value of hidden element is not changed

I have the following checkbox element <div _ngcontent-qcu-c225="" class="checkbox-section terms-of-sale"><div _ngcontent-qcu-c225="" class="checkbox-wrapper&...