最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to test relative positioning of elements in cypress? - Stack Overflow

programmeradmin0浏览0评论

I am currently new to cypress and wants to test that Forgot Password should be below Login button in Facebook page? Is there a way to do that?

Is there a way to test relative positioning of elements in cypress?

I am currently new to cypress and wants to test that Forgot Password should be below Login button in Facebook page? Is there a way to do that?

Is there a way to test relative positioning of elements in cypress?

Share Improve this question edited Mar 3, 2022 at 3:14 user16695029 4,5701 gold badge10 silver badges31 bronze badges asked Mar 2, 2022 at 3:10 NalinNalin 873 silver badges13 bronze badges 3
  • It is saying it does not found any element next to it. – Nalin Commented Mar 2, 2022 at 6:12
  • Don't use .next() it only checks the HTML order, not the screen position. – Dizzy Al Commented Mar 2, 2022 at 6:18
  • okay. yeah got it. – Nalin Commented Mar 2, 2022 at 6:42
Add a ment  | 

2 Answers 2

Reset to default 4

I think you can use jQuery .position()

cy.get('#element1')
  .then($el => $el.position().top)  // get 1st top value
  .then(top1 => {
    cy.get('#element2')
      .then($el => $el.position().top)  // get 2nd top value
      .then(top2 => {
        expect(top1).to.be.gt(top2)
      })
  })

Notes

Cypress use jQuery to find elements. Chaining .then($el => ... exposes the jQuery object containing the element, so now you can apply other jQuery functions that are not part of the Cypress mands.

In fact, any other Javascript functions you want.

You can also make reusable functions

const getTop = ($el) = $el.position().top;

cy.get('#element1').then(getTop)  
  .then(top1 => {
    cy.get('#element2').then(getTop)
      .then(top2 => {
        expect(top1).to.be.gt(top2)
      })
  })

You can use the cypress method next() to determine the element next to Log in button like this. next() gets the immediately following sibling of each DOM element within a set of DOM elements.

cy.get('div[type="submit"]').next().should('have.text', 'Forgot password?')
发布评论

评论列表(0)

  1. 暂无评论