commands.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import 'cypress-file-upload';
  18. import '@4tw/cypress-drag-drop';
  19. Cypress.Commands.add('login', () => {
  20. const { SERVE_URL } = Cypress.env();
  21. cy.request('POST', `${SERVE_URL}/apisix/admin/user/login`, {
  22. username: 'user',
  23. password: 'user',
  24. }).then((res) => {
  25. expect(res.body.code).to.equal(0);
  26. localStorage.setItem('token', res.body.data.token);
  27. // set default language
  28. localStorage.setItem('umi_locale', 'en-US');
  29. });
  30. });
  31. const timeout = 1000;
  32. const domSelector = {
  33. nameGen: (name) => `[data-cy-plugin-name="${name}"]`,
  34. parents: '.ant-card-bordered',
  35. drawer_wrap: '.ant-drawer-content-wrapper',
  36. drawer: '.ant-drawer-content',
  37. switch: 'button.ant-switch#disable',
  38. close: '.anticon-close',
  39. selectDropdown: '.ant-select-dropdown',
  40. monacoMode: '[data-cy="monaco-mode"] > .ant-select-selector',
  41. selectJSON: '[label=JSON]',
  42. monacoViewZones: 'div.view-zones',
  43. notification: '.ant-notification-notice-message',
  44. };
  45. Cypress.Commands.add('configurePlugin', ({ name, content }) => {
  46. const { shouldValid, data } = content;
  47. cy.get('main.ant-layout-content')
  48. .should('exist')
  49. .find(domSelector.nameGen(name))
  50. .then(function (card) {
  51. card.parents(domSelector.parents).find('button').trigger('click', {
  52. force: true,
  53. });
  54. // NOTE: wait for the Drawer to appear on the DOM
  55. cy.get(domSelector.drawer)
  56. .should('exist')
  57. .within(() => {
  58. cy.wait(timeout);
  59. // trick for stable
  60. cy.contains('Enable').should('exist');
  61. cy.get(domSelector.switch, { timeout }).should('exist').click({
  62. force: true,
  63. });
  64. cy.get(domSelector.monacoMode)
  65. .should('be.visible')
  66. .then(($btn) => {
  67. if ($btn.text() === 'Form') {
  68. cy.get(domSelector.monacoMode)
  69. .should('exist')
  70. .click({ force: true })
  71. .then(() => {
  72. cy.root()
  73. .closest('body')
  74. .find(domSelector.selectDropdown)
  75. .should('be.visible')
  76. .find(domSelector.selectJSON)
  77. .click();
  78. });
  79. }
  80. });
  81. // edit monaco
  82. cy.wait(timeout * 2);
  83. cy.get(domSelector.monacoViewZones).should('exist').click({ force: true });
  84. cy.window()
  85. .then((win) => {
  86. if (data) win.monacoEditor.setValue(JSON.stringify(data));
  87. })
  88. .then(() => {
  89. cy.contains('Submit').click({
  90. force: true,
  91. });
  92. });
  93. });
  94. if (shouldValid) {
  95. cy.wait(timeout);
  96. cy.get(domSelector.drawer, { timeout }).should('not.exist');
  97. } else {
  98. cy.get(domSelector.notification).should('contain', 'Invalid plugin data');
  99. cy.get(domSelector.close).should('be.visible').click({
  100. force: true,
  101. multiple: true,
  102. });
  103. cy.wait(timeout);
  104. cy.get(domSelector.drawer)
  105. .invoke('show')
  106. .within(() => {
  107. cy.contains('Cancel').click({
  108. force: true,
  109. });
  110. });
  111. }
  112. });
  113. });
  114. Cypress.Commands.add('requestWithToken', ({ method, url, payload }) => {
  115. const { SERVE_URL } = Cypress.env();
  116. // Make sure the request is synchronous
  117. cy.request({
  118. method,
  119. url: SERVE_URL + url,
  120. body: payload,
  121. headers: { Authorization: localStorage.getItem('token') },
  122. }).then((res) => {
  123. expect(res.body.code).to.equal(0);
  124. return res;
  125. });
  126. });