seitime-frappe/cypress/support/commands.js
Andrew McLeod 07cedc581d feat: Optionally remove seconds from datetime (#8531)
* fix: Add updated datepicker; fixed seconds formatting bug.
Seconds between 0 and 9 were not zero-padded.

* feat: Add framework for time format

* feat: datetime server-side formatters.

* tests: Added server-side datetime formatter tests

* feat: Update client-side datetime formatters

* tests: Add Cypress client-side formatting tests.

* fix: JSON errors

* fix: Update to not hard-code admin password

* fix: Change to using bulk_update rather than the REST API

* tests: Use Custom doctype for testing, not Standard

* fix: Codacy style fixes

* fix: Commonify update_datetime_picker in date.js, datetime.js, time.js
Fix order of time_format in System Settings
Restore get_user_fmt in utils/datetime.js

* feat: Drastically reduce scale of Cypress testing (to make tests faster)
Full testing is possible by setting 'fast_mode' to false in the spec file.

* fix: Fix issues with datepicker/timepicker expansion

* fix: typo

* style: Various style fixes as requested by DeppSource: Python

* fix: Timepicker not hiding on 'now' button. Force hiding on click.

* style: Codacy style fixes.

* fix: Use datepicker from node_modules

* test: Refactor Datetime UI tests

- cy.get_field
- cy.set_value
- cy.insert_doc with ignore_duplicate
- Nominal datetime tests to cover most formats
- Formatting with prettier

* test: Datetime UI tests; wait for cur_frm.doc.datetime to update

* tests: Add whitespace to typed input

- Clear input only for Time field

* test: Wait timeout 200

* test: Fix form test

Co-authored-by: Faris Ansari <netchampfaris@users.noreply.github.com>
2019-12-25 14:54:28 +05:30

275 lines
No EOL
6.4 KiB
JavaScript

import 'cypress-file-upload';
// ***********************************************
// 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) => { ... });
Cypress.Commands.add('login', (email, password) => {
if (!email) {
email = 'Administrator';
}
if (!password) {
password = Cypress.config('adminPassword');
}
cy.request({
url: '/api/method/login',
method: 'POST',
body: {
usr: email,
pwd: password
}
});
});
Cypress.Commands.add('call', (method, args) => {
return cy
.window()
.its('frappe.csrf_token')
.then(csrf_token => {
return cy
.request({
url: `/api/method/${method}`,
method: 'POST',
body: args,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Frappe-CSRF-Token': csrf_token
}
})
.then(res => {
expect(res.status).eq(200);
return res.body;
});
});
});
Cypress.Commands.add('get_list', (doctype, fields = [], filters = []) => {
filters = JSON.stringify(filters);
fields = JSON.stringify(fields);
let url = `/api/resource/${doctype}?fields=${fields}&filters=${filters}`;
return cy
.window()
.its('frappe.csrf_token')
.then(csrf_token => {
return cy
.request({
method: 'GET',
url,
headers: {
Accept: 'application/json',
'X-Frappe-CSRF-Token': csrf_token
}
})
.then(res => {
expect(res.status).eq(200);
return res.body;
});
});
});
Cypress.Commands.add('get_doc', (doctype, name) => {
return cy
.window()
.its('frappe.csrf_token')
.then(csrf_token => {
return cy
.request({
method: 'GET',
url: `/api/resource/${doctype}/${name}`,
headers: {
Accept: 'application/json',
'X-Frappe-CSRF-Token': csrf_token
}
})
.then(res => {
expect(res.status).eq(200);
return res.body;
});
});
});
Cypress.Commands.add('insert_doc', (doctype, args, ignore_duplicate) => {
return cy
.window()
.its('frappe.csrf_token')
.then(csrf_token => {
return cy
.request({
method: 'POST',
url: `/api/resource/${doctype}`,
body: args,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Frappe-CSRF-Token': csrf_token
},
failOnStatusCode: !ignore_duplicate
})
.then(res => {
let status_codes = [200];
if (ignore_duplicate) {
status_codes.push(409);
}
expect(res.status).to.be.oneOf(status_codes);
return res.body;
});
});
});
Cypress.Commands.add('remove_doc', (doctype, name) => {
return cy
.window()
.its('frappe.csrf_token')
.then(csrf_token => {
return cy
.request({
method: 'DELETE',
url: `/api/resource/${doctype}/${name}`,
headers: {
Accept: 'application/json',
'X-Frappe-CSRF-Token': csrf_token
}
})
.then(res => {
expect(res.status).eq(202);
return res.body;
});
});
});
Cypress.Commands.add('create_records', doc => {
return cy
.call('frappe.tests.ui_test_helpers.create_if_not_exists', { doc })
.then(r => r.message);
});
Cypress.Commands.add('set_value', (doctype, name, obj) => {
return cy.call('frappe.client.set_value', {
doctype,
name,
fieldname: obj
});
});
Cypress.Commands.add('fill_field', (fieldname, value, fieldtype = 'Data') => {
cy.get_field(fieldname, fieldtype).as('input');
if (['Date', 'Time', 'Datetime'].includes(fieldtype)) {
cy.get('@input').click().wait(200);
cy.get('.datepickers-container .datepicker.active').should('exist');
}
if (fieldtype === 'Time') {
cy.get('@input').clear();
}
if (fieldtype === 'Select') {
cy.get('@input').select(value);
} else {
cy.get('@input').type(value, { waitForAnimations: false });
}
return cy.get('@input');
});
Cypress.Commands.add('get_field', (fieldname, fieldtype = 'Data') => {
let selector = `.form-control[data-fieldname="${fieldname}"]`;
if (fieldtype === 'Text Editor') {
selector = `[data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]`;
}
if (fieldtype === 'Code') {
selector = `[data-fieldname="${fieldname}"] .ace_text-input`;
}
return cy.get(selector);
});
Cypress.Commands.add('awesomebar', text => {
cy.get('#navbar-search').type(`${text}{downarrow}{enter}`, { delay: 100 });
});
Cypress.Commands.add('new_form', doctype => {
let route = `Form/${doctype}/New ${doctype} 1`;
cy.visit(`/desk#${route}`);
cy.get('body').should('have.attr', 'data-route', route);
cy.get('body').should('have.attr', 'data-ajax-state', 'complete');
});
Cypress.Commands.add('go_to_list', doctype => {
cy.visit(`/desk#List/${doctype}/List`);
});
Cypress.Commands.add('clear_cache', () => {
cy.window()
.its('frappe')
.then(frappe => {
frappe.ui.toolbar.clear_cache();
});
});
Cypress.Commands.add('dialog', opts => {
return cy.window().then(win => {
var d = new win.frappe.ui.Dialog(opts);
d.show();
return d;
});
});
Cypress.Commands.add('get_open_dialog', () => {
return cy.get('.modal:visible').last();
});
Cypress.Commands.add('hide_dialog', () => {
cy.get_open_dialog()
.find('.btn-modal-close')
.click();
cy.get('.modal:visible').should('not.exist');
});
Cypress.Commands.add('insert_doc', (doctype, args, ignore_duplicate) => {
return cy
.window()
.its('frappe.csrf_token')
.then(csrf_token => {
return cy
.request({
method: 'POST',
url: `/api/resource/${doctype}`,
body: args,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Frappe-CSRF-Token': csrf_token
},
failOnStatusCode: !ignore_duplicate
})
.then(res => {
let status_codes = [200];
if (ignore_duplicate) {
status_codes.push(409);
}
expect(res.status).to.be.oneOf(status_codes);
return res.body.data;
});
});
});