您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

186 行
4.7 KiB

  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. Cypress.Commands.add('call', (method, args) => {
  44. return cy.window().its('frappe.csrf_token').then(csrf_token => {
  45. return cy.request({
  46. url: `/api/method/${method}`,
  47. method: 'POST',
  48. body: args,
  49. headers: {
  50. 'Accept': 'application/json',
  51. 'Content-Type': 'application/json',
  52. 'X-Frappe-CSRF-Token': csrf_token
  53. }
  54. }).then(res => {
  55. expect(res.status).eq(200);
  56. return res.body;
  57. });
  58. });
  59. });
  60. Cypress.Commands.add('get_list', (doctype, fields=[], filters=[]) => {
  61. return cy.window().its('frappe.csrf_token').then(csrf_token => {
  62. return cy.request({
  63. method: 'GET',
  64. url: `/api/resource/${doctype}?fields=${JSON.stringify(fields)}&filters=${JSON.stringify(filters)}`,
  65. headers: {
  66. 'Accept': 'application/json',
  67. 'X-Frappe-CSRF-Token': csrf_token
  68. }
  69. }).then(res => {
  70. expect(res.status).eq(200);
  71. return res.body;
  72. });
  73. });
  74. });
  75. Cypress.Commands.add('get_doc', (doctype, name) => {
  76. return cy.window().its('frappe.csrf_token').then(csrf_token => {
  77. return cy.request({
  78. method: 'GET',
  79. url: `/api/resource/${doctype}/${name}`,
  80. headers: {
  81. 'Accept': 'application/json',
  82. 'X-Frappe-CSRF-Token': csrf_token
  83. }
  84. }).then(res => {
  85. expect(res.status).eq(200);
  86. return res.body;
  87. });
  88. });
  89. });
  90. Cypress.Commands.add('create_doc', (doctype, args) => {
  91. return cy.window().its('frappe.csrf_token').then(csrf_token => {
  92. return cy.request({
  93. method: 'POST',
  94. url: `/api/resource/${doctype}`,
  95. body: args,
  96. headers: {
  97. 'Accept': 'application/json',
  98. 'Content-Type': 'application/json',
  99. 'X-Frappe-CSRF-Token': csrf_token
  100. }
  101. }).then(res => {
  102. expect(res.status).eq(200);
  103. return res.body;
  104. });
  105. });
  106. });
  107. Cypress.Commands.add('remove_doc', (doctype, name) => {
  108. return cy.window().its('frappe.csrf_token').then(csrf_token => {
  109. return cy.request({
  110. method: 'DELETE',
  111. url: `/api/resource/${doctype}/${name}`,
  112. headers: {
  113. 'Accept': 'application/json',
  114. 'X-Frappe-CSRF-Token': csrf_token
  115. }
  116. }).then(res => {
  117. expect(res.status).eq(202);
  118. return res.body;
  119. });
  120. });
  121. });
  122. Cypress.Commands.add('create_records', (doc) => {
  123. return cy.call('frappe.tests.ui_test_helpers.create_if_not_exists', { doc })
  124. .then(r => r.message);
  125. });
  126. Cypress.Commands.add('fill_field', (fieldname, value, fieldtype='Data') => {
  127. let selector = `.form-control[data-fieldname="${fieldname}"]`;
  128. if (fieldtype === 'Text Editor') {
  129. selector = `[data-fieldname="${fieldname}"] .ql-editor[contenteditable=true]`;
  130. }
  131. if (fieldtype === 'Code') {
  132. selector = `[data-fieldname="${fieldname}"] .ace_text-input`;
  133. }
  134. cy.get(selector).as('input');
  135. if (fieldtype === 'Select') {
  136. return cy.get('@input').select(value);
  137. } else {
  138. return cy.get('@input').type(value, {waitForAnimations: false});
  139. }
  140. });
  141. Cypress.Commands.add('awesomebar', (text) => {
  142. cy.get('#navbar-search').type(`${text}{downarrow}{enter}`, { delay: 100 });
  143. });
  144. Cypress.Commands.add('new_form', (doctype) => {
  145. cy.visit(`/desk#Form/${doctype}/New ${doctype} 1`);
  146. });
  147. Cypress.Commands.add('go_to_list', (doctype) => {
  148. cy.visit(`/desk#List/${doctype}/List`);
  149. });
  150. Cypress.Commands.add('clear_cache', () => {
  151. cy.window().its('frappe').then(frappe => {
  152. frappe.ui.toolbar.clear_cache();
  153. });
  154. });
  155. Cypress.Commands.add('dialog', (opts) => {
  156. return cy.window().then(win => {
  157. var d = new win.frappe.ui.Dialog(opts);
  158. d.show();
  159. return d;
  160. });
  161. });
  162. Cypress.Commands.add('get_open_dialog', () => {
  163. return cy.get('.modal:visible').last();
  164. });
  165. Cypress.Commands.add('hide_dialog', () => {
  166. cy.get_open_dialog().find('.btn-modal-close').click();
  167. cy.get('.modal:visible').should('not.exist');
  168. });