사이프러스 테스트. 유용하다고 생각할 수 있는 5가지 팁.
목차:
I suppose in this article that you've already used cypress before so you understand the basics.
환경 변수
Today mostly while building a web app we all try to use at least two environments. It helps us to ensure that all new changes deployed from previous environments are working as intended before they will be pushed to production. And you probably have different databases, api endpoints and variables for each environment. So,
When it comes to cypress, you can also have a separate config file for each environment.
File structure and file names are optional:
/src/cypress/config
/test.json
/staging.json
/production.json
Let's take a look at staging.json
:
{
"baseUrl": "http://localhost:3000",
"env": {
"env": "staging",
"apiUrl": "https://staging-api.com/api/v1",
}
}
And production.json
:
{
"baseUrl": "http://localhost:3000",
"env": {
"env": "production",
"apiUrl": "https://api.com/api/v1",
}
}
(!)Make sure you store your env variables inside env
object
Then update cypress scripts in package.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.
Then update 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 };
};
Great! So now when we have our cypress up and running with needed env config, we can use values from that configs in our tests..
If you've done everything correct, then you are able to extract the variables by doing the following:
const { apiUrl, env } = Cypress.env();
// to extract baseUrl variable you should use Cypress.config()
// const { baseUrl } = Cypress.config();
환경에 따라 즉석에서 조명기의 값 변경
Basically fixtures
is being used when you need to mock an API response, which is not recommended though
But when you have a few environments sooner or later you will face an issue when the same requests return the same data for each env except a few values(e.g. id
). And you might not want to duplicate the whole fixture.
In this case all you need to do is to extract a fixture and your env variable; then update needed value on the fly within a test case:
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`)
});
});
});
(!)If you use beforeEach
, make sure you wrap it in describe
, so it won't affect other tests.
전 세계적으로 모의 API 응답
To stub network request globally, you should open 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',
});
});
사용자 지정 명령
Custom commands in Cypress prevent you from having to add boilerplate code to your tests.
Take a look at this file:
// 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
});
To create 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 });
});
That's it! Now you can use cy.fillDiscountForm()
in any test.
요청 대기 중
Before your app displays any data, it will probably get it from the server. What if you have poor internet connection and all your tests are failing due to unfinished API requests, and lack of the data to display? In this case, and probably every time you make an API call, you should wait ( 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 });
});
All we need to do is to register the intercept before visiting the settings page. Once we visit the settings page, it will trigger GET ${apiUrl}/settings
request, and cypress will wait till it finishes and only after that will continue the testing.
Furthermore, if the API call fails for some reason, then Cypress will display an error and it will be much easier to debug.
표지 이미지 Tianyi Ma
Reference
이 문제에 관하여(사이프러스 테스트. 유용하다고 생각할 수 있는 5가지 팁.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/ryabinin/cypress-testing-5-tips-you-may-find-useful-2i1j텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)