Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress check element position

Tags:

cypress

Using Cypress.io, is there a way to check an element's position? I have an issue where in certain cases, there is a bug where scrollbars are added and removed continuously, causing the edge of the page to "jitter", and I'd like to be able to write a test to see if that's happening by asserting that the position of an element is not changing after showing up. I haven't seen anything in the documentation about such an assertion.

like image 401
Turner Hayes Avatar asked Apr 13 '18 19:04

Turner Hayes


People also ask

How do you find the position of an element in Cypress?

let initialPosition; cy. get('button'). then( ($button) => { initialPosition = $button. position(); } ); cy.

How do you check the visibility of an element in Cypress?

Check visibility I'll check the visibility of my board with following code: it('has a board', () => { cy . visit('/'); cy . get('[data-cy=board-item]') .

How do you wait for a particular element in Cypress?

Wait for API response Cypress works great with http requests. If you are waiting for some resources to be loaded in your app, you can intercept a request and then create an alias for it. That alias will then be used with . wait() command.


2 Answers

I think I figured it out; something like:

let initialPosition;

cy.get('button').then(
  ($button) => {
    initialPosition = $button.position();
  }
);

cy.wait(100);

cy.get('button')
  .should(
    ($button) => {
      expect($button.position()).deep.equal(initialPosition);
    }
  );
});

Doesn't really work, but it's the test I want.

like image 151
Turner Hayes Avatar answered Oct 11 '22 19:10

Turner Hayes


Yes, you can check CSS Values By Cypress

Examples:

.should('have.css', 'float', 'right');

.should('have.css', 'right', '10px');

.should('have.css', 'top', '50%'); 
like image 2
Hussam AlHunaiti Avatar answered Oct 11 '22 19:10

Hussam AlHunaiti