From afebaa0a23ef066a83339fe4527929c3d1225a3e Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Thu, 14 Apr 2022 13:43:43 +0530 Subject: [PATCH 1/4] test: less flaky date control test --- cypress/integration/control_date.js | 58 +++++++++++++++++------------ cypress/support/commands.js | 14 +++++++ 2 files changed, 49 insertions(+), 23 deletions(-) diff --git a/cypress/integration/control_date.js b/cypress/integration/control_date.js index 35c585306c..5093c675cf 100644 --- a/cypress/integration/control_date.js +++ b/cypress/integration/control_date.js @@ -1,23 +1,27 @@ context('Date Control', () => { before(() => { cy.login(); - cy.visit('/app/doctype'); - return cy.window().its('frappe').then(frappe => { - return frappe.xcall('frappe.tests.ui_test_helpers.create_doctype', { - name: 'Test Date Control', - fields: [ - { - "label": "Date", - "fieldname": "date", - "fieldtype": "Date", - "in_list_view": 1 - }, - ] - }); - }); + cy.visit('/app'); }); + + function get_dialog(date_field_options) { + return cy.dialog({ + title: 'Date', + fields: [{ + "label": "Date", + "fieldname": "date", + "fieldtype": "Date", + "in_list_view": 1, + ...date_field_options + }] + }); + } + it('Selecting a date from the datepicker', () => { - cy.new_form('Test Date Control'); + cy.clear_dialogs(); + cy.clear_datepickers(); + + get_dialog().as('dialog'); cy.get_field('date', 'Date').click(); cy.get('.datepicker--nav-title').click(); cy.get('.datepicker--nav-title').click({force: true}); @@ -28,12 +32,16 @@ context('Date Control', () => { cy.get('.datepicker--months > .datepicker--cells > .datepicker--cell[data-month=0]').click(); cy.get('.datepicker--days > .datepicker--cells > .datepicker--cell[data-date=15]').click(); - //Verifying if the selected date is displayed in the date field - cy.get_field('date', 'Date').should('have.value', '01-15-2020'); + // Verify if the selected date is set the date field + cy.window().its('cur_dialog.fields_dict.date.value').should('be.equal', '2020-01-15'); }); it('Checking next and previous button', () => { - cy.get_field('date', 'Date').click(); + cy.clear_dialogs(); + cy.clear_datepickers(); + + get_dialog({ default: '2020-01-15' }).as('dialog'); + cy.get_field('date', 'Date').click(); //Clicking on the next button in the datepicker cy.get('.datepicker--nav-action[data-action=next]').click(); @@ -42,7 +50,7 @@ context('Date Control', () => { cy.get('.datepicker--cell[data-date=15]').click({force: true}); //Verifying if the selected date has been displayed in the date field - cy.get_field('date', 'Date').should('have.value', '02-15-2020'); + cy.window().its('cur_dialog.fields_dict.date.value').should('be.equal', '2020-02-15'); cy.wait(500); cy.get_field('date', 'Date').click(); @@ -53,19 +61,23 @@ context('Date Control', () => { cy.get('.datepicker--cell[data-date=15]').click({force: true}); //Verifying if the selected date has been displayed in the date field - cy.get_field('date', 'Date').should('have.value', '01-15-2020'); + cy.window().its('cur_dialog.fields_dict.date.value').should('be.equal', '2020-01-15'); }); it('Clicking on "Today" button gives todays date', () => { - cy.get_field('date', 'Date').click(); + cy.clear_dialogs(); + cy.clear_datepickers(); + + get_dialog({ default: '2020-01-15' }).as('dialog'); + cy.get_field('date', 'Date').click(); //Clicking on "Today" button cy.get('.datepicker--button').click(); //Picking up the todays date - const todays_date = Cypress.moment().format('MM-DD-YYYY'); + const todays_date = new Date().toJSON().split('T')[0]; //Verifying if clicking on "Today" button matches today's date - cy.get_field('date', 'Date').should('have.value', todays_date); + cy.window().its('cur_dialog.fields_dict.date.value').should('be.equal', todays_date); }); }); \ No newline at end of file diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 4847dbcf12..14ab063b59 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -258,6 +258,20 @@ Cypress.Commands.add('hide_dialog', () => { cy.get('.modal:visible').should('not.exist'); }); +Cypress.Commands.add('clear_dialogs', () => { + cy.window().then((win) => { + win.$('.modal, .modal-backdrop').remove(); + }); + cy.get('.modal').should('not.exist'); +}) + +Cypress.Commands.add('clear_datepickers', () => { + cy.window().then((win) => { + win.$('.datepicker').remove(); + }); + cy.get('.datepicker').should('not.exist'); +}) + Cypress.Commands.add('insert_doc', (doctype, args, ignore_duplicate) => { return cy .window() From 9482ac43993d11d12bd1205f88041ef3b59508be Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Thu, 14 Apr 2022 13:49:39 +0530 Subject: [PATCH 2/4] style: semicolons --- cypress/support/commands.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 14ab063b59..ec24212f01 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -263,14 +263,14 @@ Cypress.Commands.add('clear_dialogs', () => { win.$('.modal, .modal-backdrop').remove(); }); cy.get('.modal').should('not.exist'); -}) +}); Cypress.Commands.add('clear_datepickers', () => { cy.window().then((win) => { win.$('.datepicker').remove(); }); cy.get('.datepicker').should('not.exist'); -}) +}); Cypress.Commands.add('insert_doc', (doctype, args, ignore_duplicate) => { return cy From d1978d8e62e01e23aba3aec8eb21bd0b130399d2 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Tue, 19 Apr 2022 16:49:08 +0530 Subject: [PATCH 3/4] fix: check dates in frappe realm new Date in Cypress context and new Date in frappe context differs sometimes in CI --- cypress/integration/control_date.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/cypress/integration/control_date.js b/cypress/integration/control_date.js index 5093c675cf..6d9f0b9bcc 100644 --- a/cypress/integration/control_date.js +++ b/cypress/integration/control_date.js @@ -68,16 +68,15 @@ context('Date Control', () => { cy.clear_dialogs(); cy.clear_datepickers(); - get_dialog({ default: '2020-01-15' }).as('dialog'); + get_dialog().as('dialog'); cy.get_field('date', 'Date').click(); //Clicking on "Today" button cy.get('.datepicker--button').click(); - //Picking up the todays date - const todays_date = new Date().toJSON().split('T')[0]; - //Verifying if clicking on "Today" button matches today's date - cy.window().its('cur_dialog.fields_dict.date.value').should('be.equal', todays_date); + cy.window().then(win => { + expect(win.cur_dialog.fields_dict.date.value).to.be.equal(win.frappe.datetime.get_today()); + }); }); }); \ No newline at end of file From fc4e2780bd1e82f22d91920d50abbd7f11b8b379 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Tue, 19 Apr 2022 16:49:34 +0530 Subject: [PATCH 4/4] chore: pretty logging for dialog command --- cypress/support/commands.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/cypress/support/commands.js b/cypress/support/commands.js index ec24212f01..026c622e78 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -241,8 +241,20 @@ Cypress.Commands.add('clear_cache', () => { }); Cypress.Commands.add('dialog', opts => { - return cy.window().then(win => { - var d = new win.frappe.ui.Dialog(opts); + return cy.window({ log: false }).its('frappe', { log: false }).then(frappe => { + Cypress.log({ + name: "dialog", + displayName: "dialog", + message: 'frappe.ui.Dialog', + consoleProps: () => { + return { + options: opts, + dialog: d + } + } + }); + + var d = new frappe.ui.Dialog(opts); d.show(); return d; });