浏览代码

优化cypress网络代理

Michael Wang 2 年之前
父节点
当前提交
df7cf6ff5f
共有 5 个文件被更改,包括 65 次插入23 次删除
  1. 2 0
      cypress.config.ts
  2. 8 0
      cypress/cy.d.ts
  3. 8 21
      cypress/e2e/mark-header.spec.cy.ts
  4. 7 1
      cypress/e2e/markbody-container.spec.cy.ts
  5. 40 1
      cypress/support/commands.ts

+ 2 - 0
cypress.config.ts

@@ -4,5 +4,7 @@ export default defineConfig({
   e2e: {
     baseUrl: "http://localhost:9000",
     // setupNodeEvents(on, config) {},
+    viewportWidth: 1280,
+    viewportHeight: 660,
   },
 });

+ 8 - 0
cypress/cy.d.ts

@@ -0,0 +1,8 @@
+declare global {
+  namespace Cypress {
+    interface Chainable {
+      networkStub(): Chainable<void>;
+    }
+  }
+}
+export {};

+ 8 - 21
cypress/e2e/mark-header.spec.cy.ts

@@ -1,41 +1,28 @@
 describe("评卷页面", () => {
   beforeEach(() => {
-    cy.intercept("POST", "/mark/getSetting", {
-      fixture: "settings-1.json",
-    });
-    cy.intercept("GET", "/slice/1/374/46208374-1.jpg", {
-      fixture: "assets/46208374-1.jpg,null",
-    });
-    cy.intercept("GET", "/slice/1/374/46208374-2.jpg", {
-      fixture: "assets/46208374-2.jpg,null",
-    });
-    cy.intercept("GET", "/slice/1/374/46208374-3.jpg", {
-      fixture: "assets/46208374-3.jpg,null",
-    });
-    cy.intercept("GET", "/slice/1/374/46208374-4.jpg", {
-      fixture: "assets/46208374-4.jpg,null",
-    });
-    // FIXME: need better way to do it
-    cy.intercept("GET", "/slice/**", { fixture: "assets/46208374-1.jpg,null" });
+    cy.networkStub();
   });
 
   it("成功加载", () => {
-    cy.visit("/"); // change URL to match your dev URL
+    cy.visit("/");
+    cy.wait(1000);
   });
 
   it("科目代码和名称显示", () => {
     cy.visit("/");
-    cy.get(".header-container a").should("contain", "431-金融学综合");
+    cy.wait(1000)
+      .get(".header-container a")
+      .should("contain", "431-金融学综合");
   });
 
   it("用户姓名显示", () => {
     cy.visit("/");
-    cy.get(".header-container").should("contain", "王小兰");
+    cy.wait(1000).get(".header-container").should("contain", "王小兰");
   });
 
   it("小助手显示", () => {
     cy.visit("/");
-    cy.get(".header-container .assistant-text").click();
+    cy.wait(1000).get(".header-container .assistant-text").click();
     cy.get(".assistant-table").should("contain", "全卷");
   });
 });

+ 7 - 1
cypress/e2e/markbody-container.spec.cy.ts

@@ -1,7 +1,13 @@
 describe("评卷页面-裁切图", () => {
+  beforeEach(() => {
+    cy.networkStub();
+  });
+
   it("裁切图显示", () => {
     cy.visit("/");
-    cy.get(".mark-body-container .single-image-container").should("exist");
+    cy.wait(4000)
+      .get(".mark-body-container .single-image-container")
+      .should("exist");
   });
 
   it("键盘选择鼠标分数成功", () => {

+ 40 - 1
cypress/support/commands.ts

@@ -34,4 +34,43 @@
 //       visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
 //     }
 //   }
-// }
+// }
+
+Cypress.Commands.add("networkStub", () => {
+  cy.intercept("POST", "/login", "true");
+  cy.intercept("POST", "/mark/clear", "true");
+  cy.intercept("POST", "/mark/updateSetting", "true");
+  cy.intercept("GET", "/mark/subject-select", "true");
+  cy.intercept("POST", "/mark/subject-select", "true");
+  cy.intercept(
+    "POST",
+    "/mark/subject/query",
+    `[
+                {
+                  "id": 426,
+                  "subjectCode": "431",
+                  "subjectName": "金融学综合"
+                }
+              ]`
+  );
+  cy.intercept("POST", "/mark/getSetting", {
+    fixture: "settings-1.json",
+  });
+  cy.intercept("POST", "/mark/getStatus", {
+    fixture: "status-1.json",
+  });
+  cy.intercept("POST", "/mark/getGroup", {
+    fixture: "groups-1.json",
+  });
+  cy.intercept("POST", "/mark/getTask", {
+    fixture: "task-1.json",
+  });
+  cy.intercept("GET", "/slice/**", (req) => {
+    function getFixtureFilenameForUrl(url: string) {
+      return "assets/" + url.split("/").pop() + ",null";
+    }
+
+    const fixtureFilename = getFixtureFilenameForUrl(req.url);
+    req.reply({ fixture: fixtureFilename });
+  });
+});