You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

408 lines
11 KiB

  1. import 'cypress-file-upload';
  2. import '@testing-library/cypress/add-commands';
  3. import '@4tw/cypress-drag-drop';
  4. import "cypress-real-events/support";
  5. // ***********************************************
  6. // This example commands.js shows you how to
  7. // create various custom commands and overwrite
  8. // existing commands.
  9. //
  10. // For more comprehensive examples of custom
  11. // commands please read more here:
  12. // https://on.cypress.io/custom-commands
  13. // ***********************************************
  14. //
  15. //
  16. // -- This is a parent command --
  17. // Cypress.Commands.add("login", (email, password) => { ... });
  18. //
  19. //
  20. // -- This is a child command --
  21. // Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... });
  22. //
  23. //
  24. // -- This is a dual command --
  25. // Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... });
  26. //
  27. //
  28. // -- This is will overwrite an existing command --
  29. // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... });
  30. Cypress.Commands.add('login', (email, password) => {
  31. if (!email) {
  32. email = 'Administrator';
  33. }
  34. if (!password) {
  35. password = Cypress.env('adminPassword');
  36. }
  37. cy.request({
  38. url: '/api/method/login',
  39. method: 'POST',
  40. body: {
  41. usr: email,
  42. pwd: password
  43. }
  44. });
  45. });
  46. Cypress.Commands.add('call', (method, args) => {
  47. return cy
  48. .window()
  49. .its('frappe.csrf_token')
  50. .then(csrf_token => {
  51. return cy
  52. .request({
  53. url: `/api/method/${method}`,
  54. method: 'POST',
  55. body: args,
  56. headers: {
  57. Accept: 'application/json',
  58. 'Content-Type': 'application/json',
  59. 'X-Frappe-CSRF-Token': csrf_token
  60. }
  61. })
  62. .then(res => {
  63. expect(res.status).eq(200);
  64. return res.body;
  65. });
  66. });
  67. });
  68. Cypress.Commands.add('get_list', (doctype, fields = [], filters = []) => {
  69. filters = JSON.stringify(filters);
  70. fields = JSON.stringify(fields);
  71. let url = `/api/resource/${doctype}?fields=${fields}&filters=${filters}`;
  72. return cy
  73. .window()
  74. .its('frappe.csrf_token')
  75. .then(csrf_token => {
  76. return cy
  77. .request({
  78. method: 'GET',
  79. url,
  80. headers: {
  81. Accept: 'application/json',
  82. 'X-Frappe-CSRF-Token': csrf_token
  83. }
  84. })
  85. .then(res => {
  86. expect(res.status).eq(200);
  87. return res.body;
  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. Cypress.Commands.add('remove_doc', (doctype, name) => {
  112. return cy
  113. .window()
  114. .its('frappe.csrf_token')
  115. .then(csrf_token => {
  116. return cy
  117. .request({
  118. method: 'DELETE',
  119. url: `/api/resource/${doctype}/${name}`,
  120. headers: {
  121. Accept: 'application/json',
  122. 'X-Frappe-CSRF-Token': csrf_token
  123. }
  124. })
  125. .then(res => {
  126. expect(res.status).eq(202);
  127. return res.body;
  128. });
  129. });
  130. });
  131. Cypress.Commands.add('create_records', doc => {
  132. return cy
  133. .call('frappe.tests.ui_test_helpers.create_if_not_exists', {doc: JSON.stringify(doc)})
  134. .then(r => r.message);
  135. });
  136. Cypress.Commands.add('open_doc', (doctype, name) => {
  137. cy.visit(`/app/${doctype}/${name}`)
  138. });
  139. Cypress.Commands.add('set_value', (doctype, name, obj) => {
  140. return cy.call('frappe.client.set_value', {
  141. doctype,
  142. name,
  143. fieldname: obj
  144. });
  145. });
  146. Cypress.Commands.add('fill_field', (fieldname, value, fieldtype = 'Data') => {
  147. cy.get_field(fieldname, fieldtype).as('input');
  148. if (['Date', 'Time', 'Datetime'].includes(fieldtype)) {
  149. cy.get('@input').click().wait(200);
  150. cy.get('.datepickers-container .datepicker.active').should('exist');
  151. }
  152. if (fieldtype === 'Time') {
  153. cy.get('@input').clear().wait(200);
  154. }
  155. if (fieldtype === 'Select') {
  156. cy.get('@input').select(value);
  157. } else {
  158. cy.get('@input').type(value, {waitForAnimations: false, force: true, delay: 100});
  159. }
  160. return cy.get('@input');
  161. });
  162. Cypress.Commands.add('get_field', (fieldname, fieldtype = 'Data') => {
  163. let field_element = fieldtype === 'Select' ? 'select': 'input';
  164. let selector = `[data-fieldname="${fieldname}"] ${field_element}:visible`;
  165. if (fieldtype === 'Text Editor') {
  166. selector = `[data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]:visible`;
  167. }
  168. if (fieldtype === 'Code') {
  169. selector = `[data-fieldname="${fieldname}"] .ace_text-input`;
  170. }
  171. if (fieldtype === 'Markdown Editor') {
  172. selector = `[data-fieldname="${fieldname}"] .ace-editor-target`;
  173. }
  174. return cy.get(selector).first();
  175. });
  176. Cypress.Commands.add('fill_table_field', (tablefieldname, row_idx, fieldname, value, fieldtype = 'Data') => {
  177. cy.get_table_field(tablefieldname, row_idx, fieldname, fieldtype).as('input');
  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. if (fieldtype === 'Select') {
  186. cy.get('@input').select(value);
  187. } else {
  188. cy.get('@input').type(value, {waitForAnimations: false, force: true});
  189. }
  190. return cy.get('@input');
  191. });
  192. Cypress.Commands.add('get_table_field', (tablefieldname, row_idx, fieldname, fieldtype = 'Data') => {
  193. let selector = `.frappe-control[data-fieldname="${tablefieldname}"]`;
  194. selector += ` [data-idx="${row_idx}"]`;
  195. if (fieldtype === 'Text Editor') {
  196. selector += ` [data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]`;
  197. } else if (fieldtype === 'Code') {
  198. selector += ` [data-fieldname="${fieldname}"] .ace_text-input`;
  199. } else {
  200. selector += ` [data-fieldname="${fieldname}"]`;
  201. return cy.get(selector).find('.form-control:visible, .static-area:visible').first();
  202. }
  203. return cy.get(selector);
  204. });
  205. Cypress.Commands.add('awesomebar', text => {
  206. cy.get('#navbar-search').type(`${text}{downarrow}{enter}`, {delay: 700});
  207. });
  208. Cypress.Commands.add('new_form', doctype => {
  209. let dt_in_route = doctype.toLowerCase().replace(/ /g, '-');
  210. cy.visit(`/app/${dt_in_route}/new`);
  211. cy.get('body').should('have.attr', 'data-route', `Form/${doctype}/new-${dt_in_route}-1`);
  212. cy.get('body').should('have.attr', 'data-ajax-state', 'complete');
  213. });
  214. Cypress.Commands.add('go_to_list', doctype => {
  215. let dt_in_route = doctype.toLowerCase().replace(/ /g, '-');
  216. cy.visit(`/app/${dt_in_route}`);
  217. });
  218. Cypress.Commands.add('clear_cache', () => {
  219. cy.window()
  220. .its('frappe')
  221. .then(frappe => {
  222. frappe.ui.toolbar.clear_cache();
  223. });
  224. });
  225. Cypress.Commands.add('dialog', opts => {
  226. return cy.window({ log: false }).its('frappe', { log: false }).then(frappe => {
  227. Cypress.log({
  228. name: "dialog",
  229. displayName: "dialog",
  230. message: 'frappe.ui.Dialog',
  231. consoleProps: () => {
  232. return {
  233. options: opts,
  234. dialog: d
  235. }
  236. }
  237. });
  238. var d = new frappe.ui.Dialog(opts);
  239. d.show();
  240. return d;
  241. });
  242. });
  243. Cypress.Commands.add('get_open_dialog', () => {
  244. return cy.get('.modal:visible').last();
  245. });
  246. Cypress.Commands.add('save', () => {
  247. cy.intercept('/api').as('api');
  248. cy.get(`button[data-label="Save"]:visible`).click({scrollBehavior: false, force:true});
  249. cy.wait('@api');
  250. });
  251. Cypress.Commands.add('hide_dialog', () => {
  252. cy.wait(400);
  253. cy.get_open_dialog().find('.btn-modal-close').click({force:true});
  254. cy.get('.modal:visible').should('not.exist');
  255. });
  256. Cypress.Commands.add('clear_dialogs', () => {
  257. cy.window().then((win) => {
  258. win.$('.modal, .modal-backdrop').remove();
  259. });
  260. cy.get('.modal').should('not.exist');
  261. });
  262. Cypress.Commands.add('clear_datepickers', () => {
  263. cy.window().then((win) => {
  264. win.$('.datepicker').remove();
  265. });
  266. cy.get('.datepicker').should('not.exist');
  267. });
  268. Cypress.Commands.add('insert_doc', (doctype, args, ignore_duplicate) => {
  269. return cy
  270. .window()
  271. .its('frappe.csrf_token')
  272. .then(csrf_token => {
  273. return cy
  274. .request({
  275. method: 'POST',
  276. url: `/api/resource/${doctype}`,
  277. body: args,
  278. headers: {
  279. Accept: 'application/json',
  280. 'Content-Type': 'application/json',
  281. 'X-Frappe-CSRF-Token': csrf_token
  282. },
  283. failOnStatusCode: !ignore_duplicate
  284. })
  285. .then(res => {
  286. let status_codes = [200];
  287. if (ignore_duplicate) {
  288. status_codes.push(409);
  289. }
  290. expect(res.status).to.be.oneOf(status_codes);
  291. return res.body.data;
  292. });
  293. });
  294. });
  295. Cypress.Commands.add('open_list_filter', () => {
  296. cy.get('.filter-section .filter-button').click();
  297. cy.wait(300);
  298. cy.get('.filter-popover').should('exist');
  299. });
  300. Cypress.Commands.add('click_action_button', (name) => {
  301. cy.findByRole('button', {name: 'Actions'}).click();
  302. cy.get(`.actions-btn-group [data-label="${encodeURIComponent(name)}"]`).click();
  303. });
  304. Cypress.Commands.add('click_menu_button', (name) => {
  305. cy.get('.standard-actions .menu-btn-group > .btn').click();
  306. cy.get(`.menu-btn-group [data-label="${encodeURIComponent(name)}"]`).click();
  307. });
  308. Cypress.Commands.add('clear_filters', () => {
  309. let has_filter = false;
  310. cy.intercept({
  311. method: 'POST',
  312. url: 'api/method/frappe.model.utils.user_settings.save'
  313. }).as('filter-saved');
  314. cy.get('.filter-section .filter-button').click({force: true});
  315. cy.wait(300);
  316. cy.get('.filter-popover').should('exist');
  317. cy.get('.filter-popover').then(popover => {
  318. if (popover.find('input.input-with-feedback')[0].value != '') {
  319. has_filter = true;
  320. }
  321. });
  322. cy.get('.filter-popover').find('.clear-filters').click();
  323. cy.get('.filter-section .filter-button').click();
  324. cy.window().its('cur_list').then(cur_list => {
  325. cur_list && cur_list.filter_area && cur_list.filter_area.clear();
  326. has_filter && cy.wait('@filter-saved');
  327. });
  328. });
  329. Cypress.Commands.add('click_modal_primary_button', (btn_name) => {
  330. cy.wait(400);
  331. cy.get('.modal-footer > .standard-actions > .btn-primary').contains(btn_name).click({force: true});
  332. });
  333. Cypress.Commands.add('click_sidebar_button', (btn_name) => {
  334. cy.get('.list-group-by-fields .list-link > a').contains(btn_name).click({force: true});
  335. });
  336. Cypress.Commands.add('click_listview_row_item', (row_no) => {
  337. cy.get('.list-row > .level-left > .list-subject > .level-item > .ellipsis').eq(row_no).click({force: true});
  338. });
  339. Cypress.Commands.add('click_listview_row_item_with_text', (text) => {
  340. cy.get('.list-row > .level-left > .list-subject > .level-item > .ellipsis')
  341. .contains(text)
  342. .first()
  343. .click({force: true});
  344. });
  345. Cypress.Commands.add('click_filter_button', () => {
  346. cy.get('.filter-selector > .btn').click();
  347. });
  348. Cypress.Commands.add('click_listview_primary_button', (btn_name) => {
  349. cy.get('.primary-action').contains(btn_name).click({force: true});
  350. });
  351. Cypress.Commands.add('click_doc_primary_button', (btn_name) => {
  352. cy.get('.primary-action').contains(btn_name).click({force: true});
  353. });
  354. Cypress.Commands.add('click_timeline_action_btn', (btn_name) => {
  355. cy.get('.timeline-message-box .actions .action-btn').contains(btn_name).click();
  356. });
  357. Cypress.Commands.add('select_listview_row_checkbox', (row_no) => {
  358. cy.get('.frappe-list .select-like > .list-row-checkbox').eq(row_no).click();
  359. });
  360. Cypress.Commands.add('click_form_section', (section_name) => {
  361. cy.get('.section-head').contains(section_name).click();
  362. });