断言该元素在赛普拉斯不可操作

Lau*_*ura 11 automated-tests chai cypress

如果一个元素在页面上不可操作(在这种情况下,由另一个元素覆盖)并且您尝试单击它,赛普拉斯将显示如下错误:

CypressError: Timed out retrying: cy.click() failed because this element:

<span>...</span>

is being covered by another element:
Run Code Online (Sandbox Code Playgroud)

大!但有没有办法断言这是这种情况,又称无法点击该元素?

这不起作用:

  • should.not.exist - 元素确实存在
  • should.be.disabled - 该元素未被禁用
  • should.not.be.visible - 元素可见(刚被另一个透明元素覆盖)
  • 使用cy.on('uncaught:exception', ...),因为这不是一个例外

Ric*_*sen 12

请参阅click_spec.coffee上的赛普拉斯测试.

it "throws when a non-descendent element is covering subject", (done) ->

  $btn = $("<button>button covered</button>")
    .attr("id", "button-covered-in-span")
    .prependTo(cy.$$("body"))

  span = $("<span>span on button</span>")
    .css(position: "absolute", 
         left: $btn.offset().left, 
         top: $btn.offset().top, 
         padding: 5, display: "inline-block", 
         backgroundColor: "yellow")
    .prependTo(cy.$$("body"))

  cy.on "fail", (err) =>
    ...
    expect(err.message).to.include "cy.click() failed because this element"
    expect(err.message).to.include "is being covered by another element"
    ...
    done()

  cy.get("#button-covered-in-span").click()
Run Code Online (Sandbox Code Playgroud)

最简单的是模仿这个测试,即使文档建议仅cy.on('fail')用于调试.

这类似于单元测试,expect().to.throw()用于检查异常是否按预期发生,因此我觉得这种模式是合理的.

为了彻底,我会打电话给click({force: true}).

it('should fail the click() because element is covered', (done) => {

  // Check that click succeeds when forced
  cy.get('button').click({ force: true })

  // Use once() binding for just this fail
  cy.once('fail', (err) => {

    // Capturing the fail event swallows it and lets the test succeed

    // Now look for the expected messages
    expect(err.message).to.include('cy.click() failed because this element');
    expect(err.message).to.include('is being covered by another element');

    done();
  });

  cy.get("#button-covered-in-span").click().then(x => {
    // Only here if click succeeds (so test fails)
    done(new Error('Expected button NOT to be clickable, but click() succeeded'));
  })

})
Run Code Online (Sandbox Code Playgroud)

作为自定义命令

我不确定如何制作你要求的chai扩展,但逻辑可以包含在自定义命令中

/cypress/support/index.js

Cypress.Commands.add("isNotActionable", function(selector, done) {
  cy.get(selector).click({ force: true })
  cy.once('fail', (err) => {
    expect(err.message).to.include('cy.click() failed because this element');
    expect(err.message).to.include('is being covered by another element');
    done();
  });
  cy.get("#button-covered-in-span").click().then(x => {
    done(new Error('Expected element NOT to be clickable, but click() succeeded'));
  })
}) 
Run Code Online (Sandbox Code Playgroud)

/cypress/integration/myTest.spec.js

it('should fail the click() because element is covered', (done) => {
  cy.isNotActionable('button', done)
});
Run Code Online (Sandbox Code Playgroud)

注意

done()当测试的前提(即按钮被覆盖)是假的时候,我期待超时.

这不会发生(原因未知),但通过链接.then()第二次单击允许done()使用错误消息进行调用.该then()点击是否成功回调才会被调用,否则cy.once('fail')回调处理单击故障(按照赛普拉斯自己的测试).