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.
 
 
 
 
 
 

387 regels
10 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('set_value', (doctype, name, obj) => {
  137. return cy.call('frappe.client.set_value', {
  138. doctype,
  139. name,
  140. fieldname: obj
  141. });
  142. });
  143. Cypress.Commands.add('fill_field', (fieldname, value, fieldtype = 'Data') => {
  144. cy.get_field(fieldname, fieldtype).as('input');
  145. if (['Date', 'Time', 'Datetime'].includes(fieldtype)) {
  146. cy.get('@input').click().wait(200);
  147. cy.get('.datepickers-container .datepicker.active').should('exist');
  148. }
  149. if (fieldtype === 'Time') {
  150. cy.get('@input').clear().wait(200);
  151. }
  152. if (fieldtype === 'Select') {
  153. cy.get('@input').select(value);
  154. } else {
  155. cy.get('@input').type(value, {waitForAnimations: false, force: true, delay: 100});
  156. }
  157. return cy.get('@input');
  158. });
  159. Cypress.Commands.add('get_field', (fieldname, fieldtype = 'Data') => {
  160. let field_element = fieldtype === 'Select' ? 'select': 'input';
  161. let selector = `[data-fieldname="${fieldname}"] ${field_element}:visible`;
  162. if (fieldtype === 'Text Editor') {
  163. selector = `[data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]:visible`;
  164. }
  165. if (fieldtype === 'Code') {
  166. selector = `[data-fieldname="${fieldname}"] .ace_text-input`;
  167. }
  168. if (fieldtype === 'Markdown Editor') {
  169. selector = `[data-fieldname="${fieldname}"] .ace-editor-target`;
  170. }
  171. return cy.get(selector).first();
  172. });
  173. Cypress.Commands.add('fill_table_field', (tablefieldname, row_idx, fieldname, value, fieldtype = 'Data') => {
  174. cy.get_table_field(tablefieldname, row_idx, fieldname, fieldtype).as('input');
  175. if (['Date', 'Time', 'Datetime'].includes(fieldtype)) {
  176. cy.get('@input').click().wait(200);
  177. cy.get('.datepickers-container .datepicker.active').should('exist');
  178. }
  179. if (fieldtype === 'Time') {
  180. cy.get('@input').clear().wait(200);
  181. }
  182. if (fieldtype === 'Select') {
  183. cy.get('@input').select(value);
  184. } else {
  185. cy.get('@input').type(value, {waitForAnimations: false, force: true});
  186. }
  187. return cy.get('@input');
  188. });
  189. Cypress.Commands.add('get_table_field', (tablefieldname, row_idx, fieldname, fieldtype = 'Data') => {
  190. let selector = `.frappe-control[data-fieldname="${tablefieldname}"]`;
  191. selector += ` [data-idx="${row_idx}"]`;
  192. if (fieldtype === 'Text Editor') {
  193. selector += ` [data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]`;
  194. } else if (fieldtype === 'Code') {
  195. selector += ` [data-fieldname="${fieldname}"] .ace_text-input`;
  196. } else {
  197. selector += ` [data-fieldname="${fieldname}"]`;
  198. return cy.get(selector).find('.form-control:visible, .static-area:visible').first();
  199. }
  200. return cy.get(selector);
  201. });
  202. Cypress.Commands.add('awesomebar', text => {
  203. cy.get('#navbar-search').type(`${text}{downarrow}{enter}`, {delay: 700});
  204. });
  205. Cypress.Commands.add('new_form', doctype => {
  206. let dt_in_route = doctype.toLowerCase().replace(/ /g, '-');
  207. cy.visit(`/app/${dt_in_route}/new`);
  208. cy.get('body').should('have.attr', 'data-route', `Form/${doctype}/new-${dt_in_route}-1`);
  209. cy.get('body').should('have.attr', 'data-ajax-state', 'complete');
  210. });
  211. Cypress.Commands.add('go_to_list', doctype => {
  212. let dt_in_route = doctype.toLowerCase().replace(/ /g, '-');
  213. cy.visit(`/app/${dt_in_route}`);
  214. });
  215. Cypress.Commands.add('clear_cache', () => {
  216. cy.window()
  217. .its('frappe')
  218. .then(frappe => {
  219. frappe.ui.toolbar.clear_cache();
  220. });
  221. });
  222. Cypress.Commands.add('dialog', opts => {
  223. return cy.window({ log: false }).its('frappe', { log: false }).then(frappe => {
  224. Cypress.log({
  225. name: "dialog",
  226. displayName: "dialog",
  227. message: 'frappe.ui.Dialog',
  228. consoleProps: () => {
  229. return {
  230. options: opts,
  231. dialog: d
  232. }
  233. }
  234. });
  235. var d = new frappe.ui.Dialog(opts);
  236. d.show();
  237. return d;
  238. });
  239. });
  240. Cypress.Commands.add('get_open_dialog', () => {
  241. return cy.get('.modal:visible').last();
  242. });
  243. Cypress.Commands.add('hide_dialog', () => {
  244. cy.wait(300);
  245. cy.get_open_dialog().find('.btn-modal-close').click();
  246. cy.get('.modal:visible').should('not.exist');
  247. });
  248. Cypress.Commands.add('clear_dialogs', () => {
  249. cy.window().then((win) => {
  250. win.$('.modal, .modal-backdrop').remove();
  251. });
  252. cy.get('.modal').should('not.exist');
  253. });
  254. Cypress.Commands.add('clear_datepickers', () => {
  255. cy.window().then((win) => {
  256. win.$('.datepicker').remove();
  257. });
  258. cy.get('.datepicker').should('not.exist');
  259. });
  260. Cypress.Commands.add('insert_doc', (doctype, args, ignore_duplicate) => {
  261. return cy
  262. .window()
  263. .its('frappe.csrf_token')
  264. .then(csrf_token => {
  265. return cy
  266. .request({
  267. method: 'POST',
  268. url: `/api/resource/${doctype}`,
  269. body: args,
  270. headers: {
  271. Accept: 'application/json',
  272. 'Content-Type': 'application/json',
  273. 'X-Frappe-CSRF-Token': csrf_token
  274. },
  275. failOnStatusCode: !ignore_duplicate
  276. })
  277. .then(res => {
  278. let status_codes = [200];
  279. if (ignore_duplicate) {
  280. status_codes.push(409);
  281. }
  282. expect(res.status).to.be.oneOf(status_codes);
  283. return res.body.data;
  284. });
  285. });
  286. });
  287. Cypress.Commands.add('add_filter', () => {
  288. cy.get('.filter-section .filter-button').click();
  289. cy.wait(300);
  290. cy.get('.filter-popover').should('exist');
  291. });
  292. Cypress.Commands.add('clear_filters', () => {
  293. let has_filter = false;
  294. cy.intercept({
  295. method: 'POST',
  296. url: 'api/method/frappe.model.utils.user_settings.save'
  297. }).as('filter-saved');
  298. cy.get('.filter-section .filter-button').click({force: true});
  299. cy.wait(300);
  300. cy.get('.filter-popover').should('exist');
  301. cy.get('.filter-popover').then(popover => {
  302. if (popover.find('input.input-with-feedback')[0].value != '') {
  303. has_filter = true;
  304. }
  305. });
  306. cy.get('.filter-popover').find('.clear-filters').click();
  307. cy.get('.filter-section .filter-button').click();
  308. cy.window().its('cur_list').then(cur_list => {
  309. cur_list && cur_list.filter_area && cur_list.filter_area.clear();
  310. has_filter && cy.wait('@filter-saved');
  311. });
  312. });
  313. Cypress.Commands.add('click_modal_primary_button', (btn_name) => {
  314. cy.wait(400);
  315. cy.get('.modal-footer > .standard-actions > .btn-primary').contains(btn_name).click({force: true});
  316. });
  317. Cypress.Commands.add('click_sidebar_button', (btn_name) => {
  318. cy.get('.list-group-by-fields .list-link > a').contains(btn_name).click({force: true});
  319. });
  320. Cypress.Commands.add('click_listview_row_item', (row_no) => {
  321. cy.get('.list-row > .level-left > .list-subject > .level-item > .ellipsis').eq(row_no).click({force: true});
  322. });
  323. Cypress.Commands.add('click_listview_row_item_with_text', (text) => {
  324. cy.get('.list-row > .level-left > .list-subject > .level-item > .ellipsis')
  325. .contains(text)
  326. .first()
  327. .click({force: true});
  328. });
  329. Cypress.Commands.add('click_filter_button', () => {
  330. cy.get('.filter-selector > .btn').click();
  331. });
  332. Cypress.Commands.add('click_listview_primary_button', (btn_name) => {
  333. cy.get('.primary-action').contains(btn_name).click({force: true});
  334. });
  335. Cypress.Commands.add('click_doc_primary_button', (btn_name) => {
  336. cy.get('.primary-action').contains(btn_name).click({force: true});
  337. });
  338. Cypress.Commands.add('click_timeline_action_btn', (btn_name) => {
  339. cy.get('.timeline-message-box .actions .action-btn').contains(btn_name).click();
  340. });
  341. Cypress.Commands.add('select_listview_row_checkbox', (row_no) => {
  342. cy.get('.frappe-list .select-like > .list-row-checkbox').eq(row_no).click();
  343. });
  344. Cypress.Commands.add('click_form_section', (section_name) => {
  345. cy.get('.section-head').contains(section_name).click();
  346. });