简体   繁体   English

如何检查Cypress e2e测试中元素是否从不可见?

[英]How to check if element is never visible in Cypress e2e testing?

Is there any way to assert that an element is never visible at any point when routing within Cypress? 在赛普拉斯内布线时,是否有任何方法可以断言某个元素永远不可见?

I have a server-rendered web app that is sometimes showing a "loading" state when it shouldn't. 我有一个服务器渲染的Web应用程序,该应用程序有时不应该显示“正在加载”状态。 So when I navigate between pages, a "loading" indicator is showing for a few seconds and then disappearing. 因此,当我在页面之间导航时,“正在加载”指示器会显示几秒钟,然后消失。

I know that Cypress's assertions will sometimes "wait", but in this case it's causing my assertion to fail because the loading indicator goes away and that makes the test think that it has passed. 我知道赛普拉斯的断言有时会“等待”,但在这种情况下,这会导致我的断言失败,因为加载指示器消失了,这使测试认为它已通过。

I'm using these two assertions: 我正在使用以下两个断言:

cy.get('[data-test="loading"]').should('not.exist');

cy.get('[data-test="loading"]').should('not.be.visible');

But both of them are passing because the loading indicator goes away. 但是他们两个都通过了,因为加载指示器消失了。

I've checked through all the documentation but there doesn't seem to be some kind of method for checking that an element is never visible. 我已经检查了所有文档,但似乎没有某种方法可以检查元素从不可见。 Is there some method I'm missing or some hack to test this a different way? 是否有某种我缺少的方法或一些技巧可以用不同的方式进行测试?

I might be crazy, and i have not tested this yet, but I wanted to throw this out there 我可能疯了,我还没有测试过,但是我想把它扔出去

I assume you are testing that there should NEVER be a loading indicator and it is waiting the default 4 seconds and the indicator goes away, and thus your test pass. 我假设您正在测试,永远不要有加载指示器,它正在等待默认的4秒钟,指示器消失,因此您的测试通过了。 So below I set the wait to zero, so it does not wait. 因此,在下面,我将等待时间设置为零,因此不等待。 I am also confused as to why you don't fix the actual code so you don't see the indicator if you are not supposed to. 我也对为什么不修复实际代码感到困惑,如果您不应该这样做,就看不到指示器。 Perhaps you don't have access to the code.. 也许您无权访问该代码。

cy.get('[data-test="loading"]',{ timeout: 0 }).should('not.exist');

cy.get('[data-test="loading"]',{ timeout: 0 }).should('not.be.visible');

Cypress has a lite version of jQuery, so we can watch for changes to the parent of the element that should not exist. 赛普拉斯有精简版的jQu​​ery,因此我们可以观察对不应该存在的元素的父元素的更改。

@Maccurt's tests are applied whenever a change occurs. 每当发生更改时,都会应用@Maccurt的测试。

You want to keep watch firing to a minimum, so find the immediate (or nearest) parent of tested element. 您希望将手表触发次数降至最低,因此找到被测元素的直接(或最近)父元素。

Note this covers exists tests, but should not be necessary for visible tests if the element is present all the time but is just not visible. 请注意,这涵盖了exists测试,但是如果元素一直存在但不可见,则对于visible测试来说并不必要。


In this example a button is added to body . 在此示例中,将按钮添加到body
The first test watches for a span (which is never added so the test succeeds). 第一个测试监视span (从不添加,因此测试成功)。
The 2nd test watches for the button and fails. 第二项测试监视该button并失败。

describe('watching for an element to not appear', () => {

  const watchAndTest = function(parentSelector, assert) {
    Cypress.$(parentSelector).bind('DOMNodeInserted', function(event) {
      assert()
    });
  }

  it('should succeed because "span" does not exist', () => {
    const parentSelector = 'body'
    const watchForSelector = 'span'
    watchAndTest(parentSelector, 
      () => {
        // Place your 'assert' code here
        cy.get(`${parentSelector} ${watchForSelector}`,{ timeout: 0 })
          .should('not.exist');
      }
    )

    // Place your 'act' code here
    cy.get(parentSelector).then(parent => {
      var newElement = document.createElement('button');
      parent[0].appendChild(newElement)
    })
    Cypress.$(parentSelector).unbind('DOMNodeInserted')
  })

  it('should fail because "button" exists', () => {
    const parentSelector = 'body'
    const watchForSelector = 'button'
    watchAndTest(parentSelector, 
      () => {
        // Place your 'assert' code here
        cy.get(`${parentSelector} ${watchForSelector}`,{ timeout: 0 })
          .should('not.exist');
      }
    )

    // Place your 'act' code here
    cy.get(parentSelector).then(parent => {
      var newElement = document.createElement('button');
      parent[0].appendChild(newElement)
    })
    Cypress.$(parentSelector).unbind('DOMNodeInserted')
  })

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 赛普拉斯E2E在基本身份验证上失败 - Cypress E2E fails on basic auth 如何有效地填充 MongoDB 数据库以进行 e2e 测试 - How to populate a MongoDB database for e2e testing effectively 如何在stenciljs E2E测试中消除焦点 - How to remove focus in stenciljs E2E testing 如何在AngularJS e2e测试中测试拖放功能 - How to test drag & drop functionality in AngularJS e2e testing AngularJs E2E如何测试DOM中是否存在元素 - AngularJs E2E how to test if an element exists in DOM 在元素存在时执行,但在元素不存在时不失败(赛普拉斯 E2E 测试) - Act when element exists, but don't fail when it doesn't (Cypress E2E tests) AngularJS:使用量角器进行e2e测试 - AngularJS: e2e testing with protractor 端到端测试-WebdriverJS,Selenium和Jasmine - E2E Testing - WebdriverJS, Selenium and Jasmine 赛普拉斯 e2e 测试:如何从动态下拉列表中获取所有元素并应使用测试数据进行验证(超过 200 项) - Cypress e2e testing: How to get all the elements from dynamic dropdown and should verify using test data(more than 200 items) 量角器 - 检查字符串的最佳方法在e2e测试中不是空的 - Protractor - what best way to check a string IS NOT empty in e2e testing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM