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.
 
 
 
 
 
 

341 lines
9.1 KiB

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