Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get window size with Cypress?

Tags:

cypress

I use "cypress": "2.1.0" for website functional test.

Also I have condition in tests:

if (WIDTH < 1025 && WIDTH > 480) {
  it('Open search sidebar', function () {
    cy.wait(500);
    cy.get('.FilterStroke_resultsLabel_7gBlM').safeClick();
  });
}

My question is how to get screen size (WIDTH, HEIGHT)?

like image 886
Edgaras Karka Avatar asked Mar 19 '18 13:03

Edgaras Karka


People also ask

How do you change the size of a window in Cypress?

You can change the size of the viewport height and width for the remainder of the tests by setting the new values for viewportHeight or viewportWidth within Cypress. config().

How do you get windows in Cypress?

To get the global window object, use the cy. window() command. You can use the yielded window object to call the methods on that window directly. For example, if the application code adds a method getAppName to the window object, you can execute the method from the spec.

What is viewport Cypress?

In cypress, we can control the screen size/users' visible area on a web page. Cypress default viewport: Cypress sets the width to 1000px and the height to 660px by default. Overriding the default viewport: cy.viewport() command is used to override.

How do you test a new window in Cypress?

Use cy. visit() in a new test to navigate to the URL already opened in the new window, then fill in the fields and click the buttons as you would in a Cypress test.


1 Answers

It depends on how you are using viewPorts. If you are using cy.viewport(...) command, then you are easily able to store arguments to a variable.

However, I'm assuming, that you are trying to get width and height of the browser without explicitly setting viewport before the test. In this way, cypress takes the default windows width and height from the configuration.

You are able to access the configuration trough the global Cypress variable, e.g.:

Cypress.config("viewportWidth") // => 800

OR

Cypress.config().viewportWidth // => 800
like image 97
Lukas Klusis Avatar answered Jan 04 '23 02:01

Lukas Klusis