From 74b8c99bc29eb2ffe52ee06bff829cc06ad673c8 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Thu, 15 Apr 2021 11:30:55 +0530 Subject: [PATCH] feat: Introduced cypress tests in erpnext --- .eslintrc | 3 + cypress.json | 11 + cypress/fixtures/example.json | 5 + cypress/integration/test_customer.js | 13 ++ cypress/plugins/index.js | 17 ++ cypress/support/commands.js | 326 +++++++++++++++++++++++++++ cypress/support/index.js | 25 ++ cypress/tsconfig.json | 12 + 8 files changed, 412 insertions(+) create mode 100644 cypress.json create mode 100644 cypress/fixtures/example.json create mode 100644 cypress/integration/test_customer.js create mode 100644 cypress/plugins/index.js create mode 100644 cypress/support/commands.js create mode 100644 cypress/support/index.js create mode 100644 cypress/tsconfig.json diff --git a/.eslintrc b/.eslintrc index e40502acd6..a5fcc1bcba 100644 --- a/.eslintrc +++ b/.eslintrc @@ -147,11 +147,14 @@ "Chart": true, "Cypress": true, "cy": true, + "describe": true, + "expect": true, "it": true, "context": true, "before": true, "beforeEach": true, "onScan": true, "extend_cscript": true + "localforage": true, } } diff --git a/cypress.json b/cypress.json new file mode 100644 index 0000000000..f7bd9d9a17 --- /dev/null +++ b/cypress.json @@ -0,0 +1,11 @@ +{ + "baseUrl": "http://test-develop:8001/", + "projectId": "92odwv", + "adminPassword": "admin", + "defaultCommandTimeout": 20000, + "pageLoadTimeout": 15000, + "retries": { + "runMode": 2, + "openMode": 2 + } +} diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json new file mode 100644 index 0000000000..da18d9352a --- /dev/null +++ b/cypress/fixtures/example.json @@ -0,0 +1,5 @@ +{ + "name": "Using fixtures to represent data", + "email": "hello@cypress.io", + "body": "Fixtures are a great way to mock data for responses to routes" +} \ No newline at end of file diff --git a/cypress/integration/test_customer.js b/cypress/integration/test_customer.js new file mode 100644 index 0000000000..3d6ed5d0d8 --- /dev/null +++ b/cypress/integration/test_customer.js @@ -0,0 +1,13 @@ + +context('Customer', () => { + before(() => { + cy.login(); + }); + it('Check Customer Group', () => { + cy.visit(`app/customer/`); + cy.get('.primary-action').click(); + cy.wait(500); + cy.get('.custom-actions > .btn').click(); + cy.get_field('customer_group', 'Link').should('have.value', 'All Customer Groups'); + }); +}); diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js new file mode 100644 index 0000000000..07d9804a73 --- /dev/null +++ b/cypress/plugins/index.js @@ -0,0 +1,17 @@ +// *********************************************************** +// This example plugins/index.js can be used to load plugins +// +// You can change the location of this file or turn off loading +// the plugins file with the 'pluginsFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/plugins-guide +// *********************************************************** + +// This function is called when a project is opened or re-opened (e.g. due to +// the project's config changing) + +module.exports = () => { + // `on` is used to hook into various events Cypress emits + // `config` is the resolved Cypress config +}; diff --git a/cypress/support/commands.js b/cypress/support/commands.js new file mode 100644 index 0000000000..1964b96d70 --- /dev/null +++ b/cypress/support/commands.js @@ -0,0 +1,326 @@ +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().wait(200); + } + + if (fieldtype === 'Select') { + cy.get('@input').select(value); + } else { + cy.get('@input').type(value, {waitForAnimations: false, force: true}); + } + 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('fill_table_field', (tablefieldname, row_idx, fieldname, value, fieldtype = 'Data') => { + cy.get_table_field(tablefieldname, row_idx, 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().wait(200); + } + + if (fieldtype === 'Select') { + cy.get('@input').select(value); + } else { + cy.get('@input').type(value, {waitForAnimations: false, force: true}); + } + return cy.get('@input'); +}); + +Cypress.Commands.add('get_table_field', (tablefieldname, row_idx, fieldname, fieldtype = 'Data') => { + let selector = `.frappe-control[data-fieldname="${tablefieldname}"]`; + selector += ` [data-idx="${row_idx}"]`; + selector += ` .form-in-grid`; + + if (fieldtype === 'Text Editor') { + selector += ` [data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]`; + } else if (fieldtype === 'Code') { + selector += ` [data-fieldname="${fieldname}"] .ace_text-input`; + } else { + selector += ` .form-control[data-fieldname="${fieldname}"]`; + } + + 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 dt_in_route = doctype.toLowerCase().replace(/ /g, '-'); + cy.visit(`/app/${dt_in_route}/new`); + cy.get('body').should('have.attr', 'data-route', `Form/${doctype}/new-${dt_in_route}-1`); + cy.get('body').should('have.attr', 'data-ajax-state', 'complete'); +}); + +Cypress.Commands.add('go_to_list', doctype => { + cy.visit(`/app/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.wait(300); + 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; + }); + }); +}); + +Cypress.Commands.add('add_filter', () => { + cy.get('.filter-section .filter-button').click(); + cy.wait(300); + cy.get('.filter-popover').should('exist'); +}); + +Cypress.Commands.add('clear_filters', () => { + cy.get('.filter-section .filter-button').click(); + cy.wait(300); + cy.get('.filter-popover').should('exist'); + cy.get('.filter-popover').find('.clear-filters').click(); + cy.get('.filter-section .filter-button').click(); + cy.window().its('cur_list').then(cur_list => { + cur_list && cur_list.filter_area && cur_list.filter_area.clear(); + }); +}); diff --git a/cypress/support/index.js b/cypress/support/index.js new file mode 100644 index 0000000000..1bee72d2ca --- /dev/null +++ b/cypress/support/index.js @@ -0,0 +1,25 @@ +// *********************************************************** +// This example support/index.js is processed and +// loaded automatically before your test files. +// +// This is a great place to put global configuration and +// behavior that modifies Cypress. +// +// You can change the location of this file or turn off +// automatically serving support files with the +// 'supportFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/configuration +// *********************************************************** + +// Import commands.js using ES2015 syntax: +import './commands'; + + +// Alternatively you can use CommonJS syntax: +// require('./commands') + +Cypress.Cookies.defaults({ + preserve: 'sid' +}); \ No newline at end of file diff --git a/cypress/tsconfig.json b/cypress/tsconfig.json new file mode 100644 index 0000000000..d90ebf6856 --- /dev/null +++ b/cypress/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "allowJs": true, + "baseUrl": "../node_modules", + "types": [ + "cypress" + ] + }, + "include": [ + "**/*.*" + ] +} \ No newline at end of file