vbv/cypress/support/commands.js

100 lines
3.5 KiB
JavaScript

// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add("login", (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
const _ = Cypress._;
Cypress.Commands.add('manageCommand', (command, preCommand = '') => {
const execCommand = `${preCommand} python server/manage.py ${command} --settings=config.settings.test_cypress`;
console.log(execCommand);
return cy.exec(execCommand);
});
Cypress.Commands.add('manageShellCommand', (command) => {
return cy.manageCommand(`shell -c '${command}'`);
});
function loadObjectJson(key, value, djangoModelPath, serializerModelPath) {
const djangoModel = _.last(djangoModelPath.split('.'));
const djangoModelImportPath = _.initial(djangoModelPath.split('.')).join('.');
const serializerModel = _.last(serializerModelPath.split('.'));
const serializerModelImportPath = _.initial(serializerModelPath.split('.')).join('.');
let filterPart = `${key}=${value}`;
if(_.isArray(key)) {
filterPart = _.zip(key, value).map(([k, v]) => {
return `${k}=${v}`;
}).join(',');
}
const command = `from ${djangoModelImportPath} import ${djangoModel};
from ${serializerModelImportPath} import ${serializerModel};
from myservice.apps.core.serializers import create_json_from_objects;
object = ${djangoModel}.objects.filter(${filterPart}).first();
print(create_json_from_objects(object, ${serializerModel}, many=False));
exit()`.replace(/(?:\r\n|\r|\n)/g, '');
return cy.manageShellCommand(command).then(result => {
const objectJson = JSON.parse(result.stdout);
console.log(objectJson);
return objectJson;
});
}
// Cypress.Commands.add('loadApiClientRequestResponseLog', (key, value) => {
// return loadObjectJson(
// key,
// value,
// 'myservice.apps.apiclient.models.ApiClientRequestResponseLog',
// 'myservice.apps.apiclient.serializers.ApiClientRequestResponseLogSerializer'
// );
// });