Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell Cypress to hide Chrome when running from the command line?

Tags:

cypress

Problem

I want to be able to run my tests in terminal, but all my tests fail due to Cypress using Electron as the default browser and I want it to utilize Chrome, but without having to specify it in the command line.

Current Outcome

I know I can use the command npx cypress run --browser chrome to tell Cypress to use Chrome instead. However, it opens the browser AND runs it in command line.

Expected Outcome

Ideally it would only require me to run npx cypress run and this would only run in the command line using Chrome.

Summary

Is there a way to set the default browser to Chrome instead of Electron so I wouldn't need to specify which browser to use?

Is there something to add to the package.json file to set the default browser to Chrome or a line to inject somewhere? I checked the docs and I couldn't really find anything other than the --browser command.

** Cypress Github Open Issue **

Here is the link to the open issue on their github regarding a headless chrome and also to change the default for cypress run

Support chrome headless and change defaults for cypress run

Photo for the debugger

Sorry for the terrible scribbles...I am under an NDA so had to go back and scratch out all the path names for my project as well as my last name.

enter image description here

like image 613
HyeEun Avatar asked May 15 '19 18:05

HyeEun


1 Answers

Cypress 3.8.1+ allows you to pass --headless to cypress run to make Chrome invisible on any operating system by using Chrome headless:

cypress run --headless --browser chrome


Outdated answer below:

There is not currently a way to hide Chrome in run mode on macOS or Windows.

We'd like to support it, but we'd have to find some kind of workaround for xvfb not being available. We can't use google-chrome --headless either because it won't allow us to install the Cypress extension.


The advice below will only work for Linux.

npx cypress run --browser chrome is the correct way to do what you're trying to do.

If you are on Linux, you can make it run Chrome in a virtual framebuffer (so it will be hidden from you) by blanking out the DISPLAY env variable:

DISPLAY= npx cypress run --browser chrome


Technical explanation:

  1. Cypress does not support running in "headless Chrome" - headless Chrome was not around when Cypress was first written
  2. So, when Cypress is running in Chrome in CI, it uses xvfb to create an X virtual framebuffer, then it uses the DISPLAY variable to tell Chrome to run in the xvfb
  3. However, if you have DISPLAY set (by default, it is set on Linux if you have a display manager), it will use that DISPLAY instead - this is why it appears even though you're doing cypress run.
  4. Adding DISPLAY= before the command nulls out DISPLAY, which means that Cypress will spawn xvfb and run it inside of there instead.
like image 67
Zach Bloomquist Avatar answered Sep 28 '22 23:09

Zach Bloomquist