Nabin Hait | 74b8c99 | 2021-04-15 11:30:55 +0530 | [diff] [blame^] | 1 | import 'cypress-file-upload'; |
| 2 | // *********************************************** |
| 3 | // This example commands.js shows you how to |
| 4 | // create various custom commands and overwrite |
| 5 | // existing commands. |
| 6 | // |
| 7 | // For more comprehensive examples of custom |
| 8 | // commands please read more here: |
| 9 | // https://on.cypress.io/custom-commands |
| 10 | // *********************************************** |
| 11 | // |
| 12 | // |
| 13 | // -- This is a parent command -- |
| 14 | // Cypress.Commands.add("login", (email, password) => { ... }); |
| 15 | // |
| 16 | // |
| 17 | // -- This is a child command -- |
| 18 | // Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... }); |
| 19 | // |
| 20 | // |
| 21 | // -- This is a dual command -- |
| 22 | // Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... }); |
| 23 | // |
| 24 | // |
| 25 | // -- This is will overwrite an existing command -- |
| 26 | // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }); |
| 27 | Cypress.Commands.add('login', (email, password) => { |
| 28 | if (!email) { |
| 29 | email = 'Administrator'; |
| 30 | } |
| 31 | if (!password) { |
| 32 | password = Cypress.config('adminPassword'); |
| 33 | } |
| 34 | cy.request({ |
| 35 | url: '/api/method/login', |
| 36 | method: 'POST', |
| 37 | body: { |
| 38 | usr: email, |
| 39 | pwd: password |
| 40 | } |
| 41 | }); |
| 42 | }); |
| 43 | |
| 44 | Cypress.Commands.add('call', (method, args) => { |
| 45 | return cy |
| 46 | .window() |
| 47 | .its('frappe.csrf_token') |
| 48 | .then(csrf_token => { |
| 49 | return cy |
| 50 | .request({ |
| 51 | url: `/api/method/${method}`, |
| 52 | method: 'POST', |
| 53 | body: args, |
| 54 | headers: { |
| 55 | Accept: 'application/json', |
| 56 | 'Content-Type': 'application/json', |
| 57 | 'X-Frappe-CSRF-Token': csrf_token |
| 58 | } |
| 59 | }) |
| 60 | .then(res => { |
| 61 | expect(res.status).eq(200); |
| 62 | return res.body; |
| 63 | }); |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | Cypress.Commands.add('get_list', (doctype, fields = [], filters = []) => { |
| 68 | filters = JSON.stringify(filters); |
| 69 | fields = JSON.stringify(fields); |
| 70 | let url = `/api/resource/${doctype}?fields=${fields}&filters=${filters}`; |
| 71 | return cy |
| 72 | .window() |
| 73 | .its('frappe.csrf_token') |
| 74 | .then(csrf_token => { |
| 75 | return cy |
| 76 | .request({ |
| 77 | method: 'GET', |
| 78 | url, |
| 79 | headers: { |
| 80 | Accept: 'application/json', |
| 81 | 'X-Frappe-CSRF-Token': csrf_token |
| 82 | } |
| 83 | }) |
| 84 | .then(res => { |
| 85 | expect(res.status).eq(200); |
| 86 | return res.body; |
| 87 | }); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | Cypress.Commands.add('get_doc', (doctype, name) => { |
| 92 | return cy |
| 93 | .window() |
| 94 | .its('frappe.csrf_token') |
| 95 | .then(csrf_token => { |
| 96 | return cy |
| 97 | .request({ |
| 98 | method: 'GET', |
| 99 | url: `/api/resource/${doctype}/${name}`, |
| 100 | headers: { |
| 101 | Accept: 'application/json', |
| 102 | 'X-Frappe-CSRF-Token': csrf_token |
| 103 | } |
| 104 | }) |
| 105 | .then(res => { |
| 106 | expect(res.status).eq(200); |
| 107 | return res.body; |
| 108 | }); |
| 109 | }); |
| 110 | }); |
| 111 | |
| 112 | Cypress.Commands.add('insert_doc', (doctype, args, ignore_duplicate) => { |
| 113 | return cy |
| 114 | .window() |
| 115 | .its('frappe.csrf_token') |
| 116 | .then(csrf_token => { |
| 117 | return cy |
| 118 | .request({ |
| 119 | method: 'POST', |
| 120 | url: `/api/resource/${doctype}`, |
| 121 | body: args, |
| 122 | headers: { |
| 123 | Accept: 'application/json', |
| 124 | 'Content-Type': 'application/json', |
| 125 | 'X-Frappe-CSRF-Token': csrf_token |
| 126 | }, |
| 127 | failOnStatusCode: !ignore_duplicate |
| 128 | }) |
| 129 | .then(res => { |
| 130 | let status_codes = [200]; |
| 131 | if (ignore_duplicate) { |
| 132 | status_codes.push(409); |
| 133 | } |
| 134 | expect(res.status).to.be.oneOf(status_codes); |
| 135 | return res.body; |
| 136 | }); |
| 137 | }); |
| 138 | }); |
| 139 | |
| 140 | Cypress.Commands.add('remove_doc', (doctype, name) => { |
| 141 | return cy |
| 142 | .window() |
| 143 | .its('frappe.csrf_token') |
| 144 | .then(csrf_token => { |
| 145 | return cy |
| 146 | .request({ |
| 147 | method: 'DELETE', |
| 148 | url: `/api/resource/${doctype}/${name}`, |
| 149 | headers: { |
| 150 | Accept: 'application/json', |
| 151 | 'X-Frappe-CSRF-Token': csrf_token |
| 152 | } |
| 153 | }) |
| 154 | .then(res => { |
| 155 | expect(res.status).eq(202); |
| 156 | return res.body; |
| 157 | }); |
| 158 | }); |
| 159 | }); |
| 160 | |
| 161 | Cypress.Commands.add('create_records', doc => { |
| 162 | return cy |
| 163 | .call('frappe.tests.ui_test_helpers.create_if_not_exists', {doc}) |
| 164 | .then(r => r.message); |
| 165 | }); |
| 166 | |
| 167 | Cypress.Commands.add('set_value', (doctype, name, obj) => { |
| 168 | return cy.call('frappe.client.set_value', { |
| 169 | doctype, |
| 170 | name, |
| 171 | fieldname: obj |
| 172 | }); |
| 173 | }); |
| 174 | |
| 175 | Cypress.Commands.add('fill_field', (fieldname, value, fieldtype = 'Data') => { |
| 176 | cy.get_field(fieldname, fieldtype).as('input'); |
| 177 | |
| 178 | if (['Date', 'Time', 'Datetime'].includes(fieldtype)) { |
| 179 | cy.get('@input').click().wait(200); |
| 180 | cy.get('.datepickers-container .datepicker.active').should('exist'); |
| 181 | } |
| 182 | if (fieldtype === 'Time') { |
| 183 | cy.get('@input').clear().wait(200); |
| 184 | } |
| 185 | |
| 186 | if (fieldtype === 'Select') { |
| 187 | cy.get('@input').select(value); |
| 188 | } else { |
| 189 | cy.get('@input').type(value, {waitForAnimations: false, force: true}); |
| 190 | } |
| 191 | return cy.get('@input'); |
| 192 | }); |
| 193 | |
| 194 | Cypress.Commands.add('get_field', (fieldname, fieldtype = 'Data') => { |
| 195 | let selector = `.form-control[data-fieldname="${fieldname}"]`; |
| 196 | |
| 197 | if (fieldtype === 'Text Editor') { |
| 198 | selector = `[data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]`; |
| 199 | } |
| 200 | if (fieldtype === 'Code') { |
| 201 | selector = `[data-fieldname="${fieldname}"] .ace_text-input`; |
| 202 | } |
| 203 | |
| 204 | return cy.get(selector); |
| 205 | }); |
| 206 | |
| 207 | Cypress.Commands.add('fill_table_field', (tablefieldname, row_idx, fieldname, value, fieldtype = 'Data') => { |
| 208 | cy.get_table_field(tablefieldname, row_idx, fieldname, fieldtype).as('input'); |
| 209 | |
| 210 | if (['Date', 'Time', 'Datetime'].includes(fieldtype)) { |
| 211 | cy.get('@input').click().wait(200); |
| 212 | cy.get('.datepickers-container .datepicker.active').should('exist'); |
| 213 | } |
| 214 | if (fieldtype === 'Time') { |
| 215 | cy.get('@input').clear().wait(200); |
| 216 | } |
| 217 | |
| 218 | if (fieldtype === 'Select') { |
| 219 | cy.get('@input').select(value); |
| 220 | } else { |
| 221 | cy.get('@input').type(value, {waitForAnimations: false, force: true}); |
| 222 | } |
| 223 | return cy.get('@input'); |
| 224 | }); |
| 225 | |
| 226 | Cypress.Commands.add('get_table_field', (tablefieldname, row_idx, fieldname, fieldtype = 'Data') => { |
| 227 | let selector = `.frappe-control[data-fieldname="${tablefieldname}"]`; |
| 228 | selector += ` [data-idx="${row_idx}"]`; |
| 229 | selector += ` .form-in-grid`; |
| 230 | |
| 231 | if (fieldtype === 'Text Editor') { |
| 232 | selector += ` [data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]`; |
| 233 | } else if (fieldtype === 'Code') { |
| 234 | selector += ` [data-fieldname="${fieldname}"] .ace_text-input`; |
| 235 | } else { |
| 236 | selector += ` .form-control[data-fieldname="${fieldname}"]`; |
| 237 | } |
| 238 | |
| 239 | return cy.get(selector); |
| 240 | }); |
| 241 | |
| 242 | Cypress.Commands.add('awesomebar', text => { |
| 243 | cy.get('#navbar-search').type(`${text}{downarrow}{enter}`, {delay: 100}); |
| 244 | }); |
| 245 | |
| 246 | Cypress.Commands.add('new_form', doctype => { |
| 247 | let dt_in_route = doctype.toLowerCase().replace(/ /g, '-'); |
| 248 | cy.visit(`/app/${dt_in_route}/new`); |
| 249 | cy.get('body').should('have.attr', 'data-route', `Form/${doctype}/new-${dt_in_route}-1`); |
| 250 | cy.get('body').should('have.attr', 'data-ajax-state', 'complete'); |
| 251 | }); |
| 252 | |
| 253 | Cypress.Commands.add('go_to_list', doctype => { |
| 254 | cy.visit(`/app/list/${doctype}/list`); |
| 255 | }); |
| 256 | |
| 257 | Cypress.Commands.add('clear_cache', () => { |
| 258 | cy.window() |
| 259 | .its('frappe') |
| 260 | .then(frappe => { |
| 261 | frappe.ui.toolbar.clear_cache(); |
| 262 | }); |
| 263 | }); |
| 264 | |
| 265 | Cypress.Commands.add('dialog', opts => { |
| 266 | return cy.window().then(win => { |
| 267 | var d = new win.frappe.ui.Dialog(opts); |
| 268 | d.show(); |
| 269 | return d; |
| 270 | }); |
| 271 | }); |
| 272 | |
| 273 | Cypress.Commands.add('get_open_dialog', () => { |
| 274 | return cy.get('.modal:visible').last(); |
| 275 | }); |
| 276 | |
| 277 | Cypress.Commands.add('hide_dialog', () => { |
| 278 | cy.wait(300); |
| 279 | cy.get_open_dialog().find('.btn-modal-close').click(); |
| 280 | cy.get('.modal:visible').should('not.exist'); |
| 281 | }); |
| 282 | |
| 283 | Cypress.Commands.add('insert_doc', (doctype, args, ignore_duplicate) => { |
| 284 | return cy |
| 285 | .window() |
| 286 | .its('frappe.csrf_token') |
| 287 | .then(csrf_token => { |
| 288 | return cy |
| 289 | .request({ |
| 290 | method: 'POST', |
| 291 | url: `/api/resource/${doctype}`, |
| 292 | body: args, |
| 293 | headers: { |
| 294 | Accept: 'application/json', |
| 295 | 'Content-Type': 'application/json', |
| 296 | 'X-Frappe-CSRF-Token': csrf_token |
| 297 | }, |
| 298 | failOnStatusCode: !ignore_duplicate |
| 299 | }) |
| 300 | .then(res => { |
| 301 | let status_codes = [200]; |
| 302 | if (ignore_duplicate) { |
| 303 | status_codes.push(409); |
| 304 | } |
| 305 | expect(res.status).to.be.oneOf(status_codes); |
| 306 | return res.body.data; |
| 307 | }); |
| 308 | }); |
| 309 | }); |
| 310 | |
| 311 | Cypress.Commands.add('add_filter', () => { |
| 312 | cy.get('.filter-section .filter-button').click(); |
| 313 | cy.wait(300); |
| 314 | cy.get('.filter-popover').should('exist'); |
| 315 | }); |
| 316 | |
| 317 | Cypress.Commands.add('clear_filters', () => { |
| 318 | cy.get('.filter-section .filter-button').click(); |
| 319 | cy.wait(300); |
| 320 | cy.get('.filter-popover').should('exist'); |
| 321 | cy.get('.filter-popover').find('.clear-filters').click(); |
| 322 | cy.get('.filter-section .filter-button').click(); |
| 323 | cy.window().its('cur_list').then(cur_list => { |
| 324 | cur_list && cur_list.filter_area && cur_list.filter_area.clear(); |
| 325 | }); |
| 326 | }); |