Cypress 如何获取 API 响应数据的长度?
Posted
技术标签:
【中文标题】Cypress 如何获取 API 响应数据的长度?【英文标题】:Cypress How can I get length of API response data? 【发布时间】:2019-11-26 12:02:04 【问题描述】:Image Here
我想检查这个 json 格式的 api 调用响应的长度 请看上图
`
body: Array(42)
0: id: "85ae44da-d8f0-42a3-b43a-084689fe1586", displayName: "PARTIE-323-1", description: "PARTIE-323-1", tags: "PvE,PvP"
1: id: "953f1b5a-6cb1-4c9c-92c1-0bf99e527330", displayName: "This is Public Partie Number 5", description: "this is objective", tags: "PvE"
2: id: "4522cb19-27db-497f-8489-0c48f121cecd", displayName: "This is Public Partie Number 13", description: "this is objective", tags: "PvE"
3: id: "bb25ea81-8d09-4679-9c54-0fb082a96ad5", displayName: "This is Public Partie Number 98", description: "this is objective", tags: "PvE"
4: id: "a5110f81-438d-4f11-ab01-153e8a5f84a0", displayName: "This is Public Partie Number 86", description: "this is objective", tags: "PvE"
5: id: "48d0ce7a-0408-4ec8-bc08-1c1b74ec25c7", displayName: "This is Public Partie Number 410", description: "this is objective", tags: "PvE"
6: id: "f7a6db8c-2e0e-4e33-87d6-1f1339c9abec", displayName: " Edit Mesage Number 5094", description: " Edit Mesage Number 5094", tags: "PvP,Mic On"
7: id: "c78bc55c-2341-4d3b-acf1-275d457cc750", displayName: "Kashif4 Partie rrrr", description: "Kashfi4 OBJ", tags: "PvE,PvP"
8: id: "b1146736-d56e-414b-92c1-3060ca5b1c7c", displayName: "BY_DN4", description: "OBJ1", tags: "PvE"
9: id: "8feb702d-6ed2-4c2d-9008-3414da968ed2", displayName: "abcdef", description: "abc", tags: "PvE"
10: id: "5e593e21-aff3-40ce-9cac-3707943aeb01", displayName: "This is Public Partie Number 1711", description: "this is objective", tags: "PvE"
11: id: "6a9a4183-0a66-4749-9e98-55c988458a3d", displayName: "ratinfff", description: "vxvcxvc", tags: "PvE,PvP"
12: id: "74b93d2a-08bb-4568-9fd0-56f64166af4c", displayName: "patrie2000", description: "patrie2000", tags: "PvE"
13: id: "18e03e40-7fc4-4e75-a996-597190ca2387", displayName: "PARTIE-287 1", description: "PARTIE-287 1", tags: "PvE"
14: id: "e2cf2a81-4a06-4dc3-8694-59dbda0b926a", displayName: "adfasdfsadd", description: "adfasdfsadd", tags: "PvE"
15: id: "83cc93b1-57e0-490c-88e5-5a73d6d89137", displayName: "This is Partie Number 1544", description: "this is objective", tags: "PvE"
16: id: "b1157576-e658-4a30-9ced-65d9bd9d8640", displayName: "This is Public Partie Number 55", description: "this is objective", tags: "PvE"
17: id: "8ea1aaf8-07c7-4ab7-985f-717495447e8b", displayName: "New Partie", description: "abc", tags: "PvE"
18: id: "da83f044-7e11-40a8-967e-7c11593dd16f", displayName: "This is Partie Number 978", description: "this is objective", tags: "PvE"
19: id: "130607fc-c488-49aa-a262-7dd5cd81621c", displayName: "lllllklklklklk;lkl", description: "fffffffffffffff", tags: "PvE"
20: id: "4f56bd15-fb3d-457f-a003-88cd8a866eba", displayName: "rating", description: "aa", tags: "PvE,PvP"
21: id: "8cf069fc-b917-4a41-8368-94235b9c2dca", displayName: "test ", description: "abc", tags: "PvE"
22: id: "d5db750c-1eed-4662-b98f-98514224771f", displayName: "This is Public Partie Number 1505", description: "this is objective", tags: "PvE"
23: id: "186e5927-4044-4bc4-ab84-9d0284bf9e2e", displayName: "This is post with 50 characters This is post with 50 characters This is post with 50 characters This", description: "This is post with 50 characters This is post with …is post with 50 characters This is post with 50 c", tags: "PvE"
24: id: "6b5a9841-c769-47aa-81d8-a471492b4268", displayName: "This is Public Partie Number 66", description: "this is objective", tags: "PvE"
25: id: "e23918cb-4119-4ff2-bafd-a4fba328dbf5", displayName: "wret", description: "afdasf", tags: "PvP"
26: id: "d1e51372-229a-44db-9214-a8964f29fefe", displayName: "This is Public Partie Number 870", description: "this is objective", tags: "PvE"
27: id: "84b10103-a123-47af-b266-ad96f4b0967b", displayName: "aa", description: "afasd", tags: "PvE"
28: id: "15af3152-408e-4ba5-8fe5-af5ac6bb2615", displayName: "This is Public Partie Number 65", description: "this is objective", tags: "PvE"
29: id: "8b05fd09-30c3-4a1f-be10-af830de98e63", displayName: "abcdef", description: "abc", tags: "PvE,PvP"
30: id: "b35eab58-a35d-4923-89cf-b56b0b96f921", displayName: "This is Public Partie Number 1737", description: "this is objective", tags: "PvE"
31: id: "6ce5e08c-4a5b-4c12-a409-c0d620b2e823", displayName: "commences paritie toaster", description: "commences paritie toaster", tags: "PvE,PvP"
32: id: "0916cace-a136-4459-b224-c7fdc90fed36", displayName: "This is Partie Number 509", description: "this is objective", tags: "PvE"
33: id: "774b859c-7d17-4086-8739-ccf575780de2", displayName: "partie 1 for cypress test", description: "partie 1 for cypress test", tags: "PvE"
34: id: "8e648113-09dc-4cc8-9c0f-d88912bb798c", displayName: "Partie258147", description: "Partie258147", tags: "PvE"
35: id: "529d17d7-9bba-44cd-9720-df12ee596662", displayName: "This is Public Partie Number 94", description: "this is objective", tags: "PvE"
36: id: "3a3871c0-49bd-46f3-9710-e3ff36ca239a", displayName: "This is Public Partie Number 86", description: "this is objective", tags: "PvE"
37: id: "8e0712ec-ac60-4ffb-819a-eae7cd06aba8", displayName: "duplicate avatar", description: "abc", tags: "PvE"
38: id: "4cbf023d-d751-40e6-861e-ef65d4a46d2a", displayName: "This is Partie Number 1772", description: "this is objective", tags: "PvE"
39: id: "87929893-af9f-4544-98e2-f4f5bc798ad6", displayName: "This is Public Partie Number 481", description: "this is objective", tags: "PvE"
40: id: "2b9769d0-5860-483b-9482-f56d4f5dc9d6", displayName: "commences paritie toaster 2", description: "abc", tags: "PvE,PvP"
41: id: "1c365027-b86b-442f-92b8-fccf7e743155", displayName: "This is Partie Number 1653", description: "this is objective", tags: "PvE"
length: 42
`
我想检查这个 json 格式的 api 调用响应的长度 请看上图
【问题讨论】:
在javascript中,只需在对象上调用.length
。您能否向我们展示这些 console.log 是如何完成的:您的源代码是什么?
既然这只是一个数组,你应该断言它的长度为 42。你应该能够将它从 BODY 上拉下来。
如果长度增加,你的测试不会因此而失败吗?您能否向我们展示调用此 API 的代码或您当前的测试。我想提供帮助,但我需要更多信息。
@Kashif 你需要展示你的测试代码才能帮助你
到底是什么失败了?发生了很多事情......
【参考方案1】:
我终于成功了
describe("Partie Counts Profile Page Test", () =>
before(function()
cy.SignIn();
cy.fixture("vars.json").as("vars");
);
it("Partie Counts", () =>
const profileBtn = "div.actions--left > button > img";
///// I want to get lenght of the parties from the url response and want check with counts in UI
cy.wait(3000);
cy.get(profileBtn).click();
cy.wait(1000);
cy.server();
cy.route(
method: "GET",
url: "api/v1/room/getUserRooms/d23eef09-e4e1-455d-a74c-03dfc61bde11"
).as("get_jobs");
cy.wait("@get_jobs").then(xhr =>
cy.log(xhr.response.body.length);
cy.get("div:nth-child(3) > span.quick-stat-figure").should(
"contain",
xhr.response.body.length
);
);
);
);
【讨论】:
以上是关于Cypress 如何获取 API 响应数据的长度?的主要内容,如果未能解决你的问题,请参考以下文章
Cypress.io - 根据 cy.route() 中的请求参数以编程方式设置响应
如何从包含android中API响应的缓存中获取改造缓存数据