How do I send baseUrl as a parameter with Cypress.io - cypress

I am researching switching from Protractor to Cypress.io.
I have some tests up and running, however, I want to be able to send the baseUrl as a parameter as I can with Protractor.
I have tried:
$ npm run cypress:open --config "baseUrl=myUrl" --still uses the baseUrl from my config file.
$ npm run cypress:open --env "baseUrl=myUrl" --still uses the baseUrl from my config file.
and a host of other things, none of which work quite right.
I want to be able to pass a parameter to my command which gives me the flexibility to choose which environment I am running my tests in. I can do this with Protractor, with a command like this:
$ ng e2e --suite testSuite --baseUrl myUrl
What is the equivalent for Cypress.io?

You have to set the env variable CYPRESS_baseUrl
CYPRESS_baseUrl=[your baseUrl] npm run cypress:open should do it for you

I think the correct way to do this is:
$ npm run cypress:open -- --config "baseUrl=myUrl"
Otherwise, the config parameter is passed to npm instead of cypress. Notice the extra --
Good luck!

Brendan's answer is correct.
I would like to add that
$ npm run cypress:open --config "baseUrl=myUrl"
may not be working since you try to propagate some configuration into a command inside your package.json.
If instead you'd do for example:
$ ./node_modules/.bin/cypress run --config baseUrl=myUrl
it should work just fine.
This is good to know since it will let you use additional CLI options as well (which you do not know ahead of time).
PS: --env wouldn't work for baseUrl since baseUrl is a built-in configuration value and not a regular env variable.

What I did was simply:
export CYPRESS_BASE_URL=SOME_OTHER_URL
yarn cypress run
This will override the baseUrl configuration. See: https://docs.cypress.io/guides/guides/environment-variables.html#Overriding-Configuration

Related

Cypress custom reporter problem with --spec flag

When I run tests via npx cypress run everything works fine, but when I want to run one or more tests via the --spec flag, I can't find this tests in the config and the reporter logic breaks, since the config doesn't changed testFiles field, if you specify the testFiles field in cypress.json or set it via the CLI npx cypress run --config testFiles='examp.spec.js' then everything works fine.
Here our cypress-reporter https://www.npmjs.com/package/#zebrunner/javascript-agent-cypress

Programmatically run cypress tests on docker

I am currently running cypress tests using:
await cypress.run({config inserted here})
Wondering if there is a way to spin up one of cypress's docker containers and then point the cypress tests there using the statement above. The suggestions online are to run the tests using command line, but I'm hoping to still be able to use cypress.run() and perhaps pass in an option that tells cypress to point the tests to a container?
Cypress docker containers will invoke cypress run by default. To change that, you'll need to override the container entrypoint to invoke node instead of cypress, and then pass your script file that's invoking cypress via the module api (cypress.run()) as the container command. You could do this via the command line, but it's a bit long because of all the options you'll need to pass:
# Assumes you're running this from the Cypress project directory on the host machine
$ docker run -it --rm -v $PWD:/tests -w /tests --entrypoint "/usr/local/bin/node" cypress/included:3.2.0 "myScript.js"
Warning: you're mounting in your npm dependencies with this method, which might cause problems if any of them were compiled on a different os like Windows.

Is there difference `yarn dev` and `yarn run dev`?

To run the local server for development I normally use yarn run dev.
But it seems yarn dev provides same function. Is this command just a short alias for yarn run dev?
I couldn't find info for yarn dev in documents.
You can leave out run from this command.
Basically, not only dev command, you can directly use any scripts by name without keyword run.
So, yarn dev and yarn run dev both do the same.
From the docs:
https://classic.yarnpkg.com/en/docs/cli/run/
It’s also possible to leave out the run in this command, each script can be executed with its name
And a similar example is given for yarn 2
https://yarnpkg.com/cli/run
Same thing, but without the "run" keyword :

Gitlab CI/CD runner : mvn command not found

Maven is well installed on my gitlab-runner server. When executing mvn clean directly on my repo it works, when running my pipeline using Gitlab UI got this error :
bash: line 60: mvn: command not found
ERROR: Job failed: exit status 1
I notice that I tried to fix the problem by adding the before_script section in the .gitlab-ci.yml file :
before_script:
- export MAVEN_HOME=/usr/local/apache-maven
I add also the line :
environment = ["MAVEN_HOME=/usr/local/apache-maven"]
on the config.toml file.
the problem still persist, my executor is : shell.
Any advice!
I managed to fix the problem using this workaround:
script:
- $MAVEN_HOME/bin/mvn clean
Just add the maven docker image, add below line as first line:
image: maven:latest or image: maven:3-jdk-10 or image: maven:3-jdk-9
refer: https://docs.gitlab.com/ee/ci/examples/artifactory_and_gitlab/
For anyone experiencing similar issues, it might be a good idea to restart the gitlab runner ".\gitlab-runner.exe restart". Especially after fiddling with environmental variables.
There is an easier way:
Making changes in ~/.bash_profile not ~/.bashrc.
According to this document:
.bashrc it is more common to use a non-login shell
This document saying:
For certain executors, the runner passes the --login flag as shown above, which also loads the shell profile.
So it should not be ~/.bashrc, you can also try ~/.profile which It can hold the same configurations, which are then also accessible by other shells
In my scenario I do following things:
1. Set gitlab-runner's user password.
passwd gitlab-runner
2. Login gitlab-runner.
su - gitlab-runner
3. Make changes in .bash_profile
Add maven to PATH:
$ export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.9
$ export M2=$M2_HOME/bin
$ export PATH=$M2:$PATH
You can include these commands in $HOME/.bashrc
I hope you had figure out your question. I met the same question when I build my ci on my server.
I use the shell as the executer for my Runner.
here are the steps to figure out.
1 check the user on the runner server
if you had install maven on the runner server successfully, maybe it just successful for the root, u can check the real user for the ci process.
job1:
stage: test
script: whoami
if my case, it print gitlab-runner, not the root
2 su the real user, check mvn again
In this time, it print error as same as the Gitlab ci UI.
3 install maven for the real user. run the pipeline again.
You can also use as per below in the .gitlab-ci.yml
before_script:
- export PATH=$PATH:/opt/apache-maven-3.8.1/bin

Add Laravel .env variable to Vue component

I would like to get access to an .env variable using Vue JS.
In my .env file I have added the 'MIX_' prefix to the var.
MIX_VAR=key
And then in the vue component, I have in the created():
console.log(process.env.MIX_VAR);
I keep getting undefined as the result.
I have tried clearing config cache, but still getting the same issue. Any ideas?
in windows :
thats worked for me without any require in webpack.mix
... just add a new variable in env file with this prefix : MIX_
MIX_API_URL=http://laravel:8000
but need to restart php artisan serve and also restart npm run watch....
let api_url = process.env.MIX_API_URL;
console.log("my env variable:");
console.log(api_url);
in linux or docker:
i didnt use them yet
You must build your JS for the env variables to be replaced. You can do this with npm or yarn
https://laravel.com/docs/5.7/mix#running-mix
Pulled from the official docs # https://laravel.com/docs/5.6/mix#environment-variables
Environment Variables
You may inject environment variables into Mix by prefixing a key in your .env file with MIX_:
MIX_SENTRY_DSN_PUBLIC=http://example.com
After the variable has been defined in your .env file, you may access via the process.env object. If the value changes while you are running a watch task, you will need to restart the task:
process.env.MIX_SENTRY_DSN_PUBLIC
The most important thing to remember is that you have to use Laravel Mix for this to work. Mix is what is injecting the environment variable.
process.env.MIX_VAR / process.env.MIX_STRIPE_KEY
It's will work without any settings. Just run this command
npm run prod
This works for me
https://laravel.com/docs/8.x/mix#environment-variables
However, you will need to restart the task if the environment variable's value changes while the task is running:
e.g If Watch is running then re-run it.
npm run watch

Resources