自定义cypress命令在顶点页面准备好后返回

问题描述 投票:1回答:2

我正在尝试让cypress为Oracle apex工作。但是,从我的自定义命令返回时,似乎没有完全加载页面。

我想只在触发了顶点事件“apexreadyend”时才进行return w.apex;(这是oracle apex页面加载中的最后一个事件)。

我怎样才能做到这一点?或者也许这可以在每次加载页面后调用?

我设法制作了这个自定义命令:

Cypress.Commands.add("apex", () => {

    cy.window().then((w) => {
        return w.apex;
    });
});

更新: 我认为事件'apexreadyend'已经发生在这一点上,使这无用。相反,我去检查身体:

cy.get('body').should('not.have.class','no-anim')

但是,自定义主题可能不使用此类。所以这不是一个很好的解决方案。

javascript oracle-apex cypress dom-events
2个回答
0
投票

您可以将您的活动包装在Promise中。赛普拉斯的文档有一个关于waiting for a Promise to complete的例子。对于您的活动,它看起来像这样:

Cypress.Commands.add("apex", () => {
  const EVENT_NAME = "apexreadyend"
  return cy.window() // get a handle for the window
  .then($win => {
    return new Cypress.Promise(resolve => {
      const onReady = () => {
        $win.removeEventListener(EVENT_NAME, onReady) // cleanup
        resolve() // resolve and allow Cypress to continue
      }
      $win.addEventListener(EVENT_NAME, onReady)
    })
  })
})

然后像这样的东西会起作用:

cy.apex() // wait for `apexreadyend` to fire
// do some assertions here that will only pass after apexreadyend

0
投票

你想等到window.apex存在吗?由于赛普拉斯的重试逻辑,您实际上不需要听取此功能的事件。一旦定义了属性,您就可以使用cy.its()来获取属性的值:

Cypress.Commands.add("apex", () => {
  return cy.window()
  .its('apex') // will automatically retry until `window.apex` exists
               // or until the default timeout occurs
})

然后你应该能够像这样使用它:

it('something apex', function() {
  cy.apex().then(apex => {
    // do whatever you want with the apex object
  })
  // or, since it's wrapped, can also just do assertions like this:
  cy.apex().its('some-property').should('equal', 'abc123')
})
© www.soinside.com 2019 - 2024. All rights reserved.