How do I pass a Bitbucket repository variable into the Cypress.io test script that I am running with npm run
in a pipeline?
The pipeline and tests work, but I am unable to get a Bitbucket variable into the test file iteself. I can access the respository variable from bitbucket-pipeline.yml
following the instructions provided by Bitbucket on the repository variable page, but I cannot access the variable inside of cypress/integration/example.js
. I want to store credentials the test scripts use as Bitbucket repository variables.
Here's my code...
bitbucket-pipeline.yml
image: cypress/included:3.2.0
pipelines:
custom:
robot:
- step:
script:
- printenv
- npm ci
- npm run e2e
- uses an image provided by Cypress
- I can see my repository variables via
printenv
package.json
{
...
"scripts": {
"e2e": "cypress run"
},
...
"dependencies": {
"cypress": "^9.4.1"
}
}
cypress/integration/example.js
describe('A', () => {
it('should B', () => {
cy.visit('https://google.com');
});
});
- I want to use the Bitbucket repository variable inside of the
it('should B' ...)
method.
Thanks in advance for your help.
CodePudding user response:
I found the Cypress.io documentation on environment variables: https://docs.cypress.io/guides/guides/environment-variables
bitbucket-pipeline.yml
image: cypress/included:3.2.0
pipelines:
custom:
robot:
- step:
script:
- npm ci
- export CYPRESS_example=$example
- export CYPRESS_whatever=$whatever
- npm run e2e
Where example is the name of the Bitbucket repo variable. Might be case sensitive.
Add $ to reference the Bitbucket repository variable in the bitbucket-pipeline.yml
file.
Use CYPRESS_ to identify it as a Cypress environment variable. https://docs.cypress.io/guides/guides/environment-variables#Option-3-CYPRESS_
Then you can use it in the test spec via Cypress.env("example")
I actually had to typecast because Bitbucket was providing unexpected data types...
cy.get('[name="password"]').clear().type(Cypress.env("example").toString());
CodePudding user response:
You can use the plugin to have a profiles for each deployment and when run the script you can set which env to run the script for example below would work if plugin setup properly inside the pipeline.
npx cypress run --env version="qa" npx cypress run --env version="prod"