35
loading...
This website collects cookies to deliver better user experience
I suppose in this article that you've already used cypress before so you understand the basics.
/src/cypress/config
/test.json
/staging.json
/production.json
staging.json
:{
"baseUrl": "http://localhost:3000",
"env": {
"env": "staging",
"apiUrl": "https://staging-api.com/api/v1",
}
}
production.json
:{
"baseUrl": "http://localhost:3000",
"env": {
"env": "production",
"apiUrl": "https://api.com/api/v1",
}
}
env
objectpackage.json
to make sure you run cypress with needed config for each env:"scripts": {
"cypress:run:staging": "cypress run --env configFile=staging",
"test:e2e:staging:run": "start-server-and-test 'npm run start' http://localhost:3000 'npm run cypress:run:staging'",
"cypress:run:production": "cypress run --env configFile=production",
"test:e2e:production:run": "start-server-and-test 'npm run start' http://localhost:3000 'npm run cypress:run:production'",
}
// same approach could be used for "cypress open" command.
"start-server-and-test" module runs your app and right after that triggers cypress tests.
src/cypress/plugins/index.js
with the following code:const fs = require('fs')
const path = require('path')
function getConfigurationByFile(fileName) {
const pathToConfigFile = path.resolve(__dirname, `../config/${fileName}.json`);
let rawData;
try {
rawData = fs.readFileSync(pathToConfigFile);
} catch (e) {
console.error(e);
}
return JSON.parse(rawData);
}
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
// this value comes from *cypress run --env configFile=staging*
const configFile = getConfigurationByFile(config.env.configFile || 'test');
return { ...config, ...configFile };
};
const { apiUrl, env } = Cypress.env();
// to extract baseUrl variable you should use Cypress.config()
// const { baseUrl } = Cypress.config();
fixtures
is being used when you need to mock an API response, which is not recommended thoughid
). And you might not want to duplicate the whole fixture.describe('it should test smth', function() {
beforeEach(() => {
// user is a env variable
const { user: userEnv } = Cypress.env();
cy.fixture('user.json').then(user => {
user.id = userEnv.id; // updating user id
// use updated fixture here (e.g. `cy.intercept`)
});
});
});
beforeEach
, make sure you wrap it in describe
, so it won't affect other tests.Here is the link to fixture docs
src/cypress/support/index.js
file and add the following code:beforeEach(() => {
cy.intercept({ url: `${apiUrl}/profile*`, middleware: true }, req => {
req.reply({
fixture: 'getProfile.json',
});
});
Here is the link to itercept docs
// you can turn this piece of code
it('should fill in discount form', function() {
cy.get('input[name="email"]').type('[email protected]');
cy.get('input[name="phone"]').type('1231231212');
cy.get('div[role="checkbox"]').click({ force: true });
cy.findByText(/Save/i).click({ force: true });
// ...rest of the test
});
// into a single line
it('should fill in discount form', function() {
cy.fillDiscountForm();
// ...rest of the test
});
cy.fillDiscountForm()
command you should go over to the file at src/cypress/support/commands.js
and create a custom command there:Cypress.Commands.add('fillDiscountForm', function() {
cy.get('input[name="email"]').type('[email protected]');
cy.get('input[name="phone"]').type('1231231212');
cy.get('div[role="checkbox"]').click({ force: true });
cy.findByText(/Save/i).click({ force: true });
});
cy.fillDiscountForm()
in any test.Here is the link to custom commands
cy.wait
) for the API call to finish before doing any assertions.it('should fill in discount form', function() {
cy.intercept(`${apiUrl}/settings`).as('getSettings');
cy.visit(`/settings-page`);
// once a request to get settings responds, 'cy.wait' will resolve
cy.wait('@getSettings');
// rest of the test
// cy.contains(/edit settings/i).click({ force: true });
});
GET ${apiUrl}/settings
request, and cypress will wait till it finishes and only after that will continue the testing.Here is the link to wait docs