I need to test the properties that I have in the below JSON by using Cypress. I think I have a problem with reaching to each property and verify them.
describe('POST method', () => {
it('get a list of faceted aggregations', () => {
cy.request({
method: 'POST',
url:"host endpoints",
headers:{
"Authorization":"xxxxxxx",
"content-type": "application/json"
},
body: {
"search": {
"auctionType": [
"basic_auction"
],
"statuses": [
"ongoing"
],
"sites": [
"autobid.de"
],
"urls": [
"yyyyyyy"
],
"ahIds": [
53825-62-2014918
],
"biddingPlatforms": [
"aaaaaaa"
],
"winningPlatforms": [
"aaaaa"
],
"losingPlatforms": [
"aaaaaa"
],
"hasBids": true,
"hasVat": true,
"query": "string",
"range": [
{
"auctionEndDatetime": {
"min": "string",
"max": "string"
}
},
{
"auctionEndDatetime": {
"min": "string",
"max": "string"
}
},
{
"auctionStartDatetime": {
"min": "string",
"max": "string"
}
},
{
"createdDatetime": {
"min": "string",
"max": "string"
}
},
{
"updatedDatetime": {
"min": "string",
"max": "string"
}
},
{
"price": {
"min": "string",
"max": "string"
}
}
],
"exists": [
"string"
],
"filters": {
"vehicles": [
{
"brand": "string",
"modelIds": 0,
"model": "string"
}
}
}
My Response Body:
{"data": {
"count": 252655,
"aggregations": {
"filters.bodyTypes": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Station wagon",
"doc_count": 45778
},
{
"key": "SUV",
"doc_count": 30188
},
{
"key": "Hatchback",
"doc_count": 25059
},
{
"key": "Saloon",
"doc_count": 13636
},
{
"key": "Van",
"doc_count": 11266
},
{
"key": "MPV",
"doc_count": 10158
},
{
"key": "Estate",
"doc_count": 9700
},
{
"key": "Estate, Number of doors 5",
"doc_count": 6800
},
{
"key": "Berline avec hayon",
"doc_count": 6276
},
{
"key": "Saloon, Number of doors 5",
"doc_count": 5238
},
{
"key": "berline hayon - VP",
"doc_count": 3298
},
{
"key": "All-terrain vehicle, Number of doors 5, All-wheel drive",
"doc_count": 2783
},
{
"key": "Stationwagon",
"doc_count": 2727
},
{
"key": "Saloon, Number of doors 4",
"doc_count": 2466
},
{
"key": "",
"doc_count": 2270
},
{
"key": "Van / minibus, Number of doors 5",
"doc_count": 2058
},
{
"key": "Other",
"doc_count": 1989
},
{
"key": "Berlina",
"doc_count": 1901
},
{
"key": "H",
"doc_count": 1867
},
{
"key": "Crossover",
"doc_count": 1860
},
{
"key": "Panel van",
"doc_count": 1833
},
{
"key": "HA -BERLINA CON PORTON",
"doc_count": 1796
},
{
"key": "SUV VP - VP",
"doc_count": 1771
},
{
"key": "Estate, Number of doors 5, All-wheel drive",
"doc_count": 1760
},
{
"key": "Higher Mid-Range Cars",
"doc_count": 1748
},
{
"key": "Station Wagon",
"doc_count": 1673
},
{
"key": "Monovolume",
"doc_count": 1603
},
{
"key": "Saloon, Number of doors 3",
"doc_count": 1547
},
{
"key": "W",
"doc_count": 1513
},
{
"key": "Mid-range Cars",
"doc_count": 1492
},
{
"key": "All-terrain vehicle, Number of doors 5",
"doc_count": 1453
},
{
"key": "Wagon",
"doc_count": 1210
},
{
"key": "break - VP",
"doc_count": 1201
},
{
"key": "ES -FAMILIAR",
"doc_count": 1158
},
{
"key": "SW",
"doc_count": 1064
},
{
"key": "Berline 5 portes",
"doc_count": 1017
},
{
"key": "Personal car",
"doc_count": 994
},
{
"key": "Sedan",
"doc_count": 994
},
{
"key": "SUV Medium",
"doc_count": 949
},
{
"key": "SUV4x2",
"doc_count": 946
},
{
"key": "berline VU - VS",
"doc_count": 938
},
{
"key": "F",
"doc_count": 896
},
{
"key": "Convertible / Roadster, Number of doors 2",
"doc_count": 894
},
{
"key": "CI",
"doc_count": 867
},
{
"key": "Stationwagen",
"doc_count": 835
},
{
"key": "Monovolume 4x4",
"doc_count": 805
},
{
"key": "mini MS - VP",
"doc_count": 802
},
{
"key": "Light duty truck, Number of doors 4",
"doc_count": 738
},
{
"key": "BREAK",
"doc_count": 717
},
{
"key": "Mixed car",
"doc_count": 702
},
{
"key": "VU1",
"doc_count": 677
},
{
"key": "M",
"doc_count": 673
},
{
"key": "Coupe",
"doc_count": 665
},
{
"key": "MONO-M",
"doc_count": 655
},
{
"key": "Saloon, Number of doors 4, All-wheel drive",
"doc_count": 634
},
{
"key": "Mini MPV",
"doc_count": 588
},
{
"key": "crossover - VP",
"doc_count": 584
},
{
"key": "OD -TODOTERRENO",
"doc_count": 536
},
{
"key": "CV -FURGON DERIVADO DE TURISMO",
"doc_count": 530
},
{
"key": "Compact cars",
"doc_count": 529
},
{
"key": "4x4",
"doc_count": 527
},
{
"key": "fourgonnette - VU",
"doc_count": 524
},
{
"key": "Estate, Number of doors 4",
"doc_count": 511
},
{
"key": "Break VP",
"doc_count": 504
},
{
"key": "Coupé",
"doc_count": 503
},
{
"key": "Kombi",
"doc_count": 490
},
{
"key": "Saloon, Number of doors 5, All-wheel drive",
"doc_count": 481
},
{
"key": "Fourgonnette",
"doc_count": 454
},
{
"key": "Berline VU 5 portes",
"doc_count": 452
},
{
"key": "KOM",
"doc_count": 437
},
{
"key": "S",
"doc_count": 427
},
{
"key": "Sports car / Coupé, Number of doors 2",
"doc_count": 426
},
{
"key": "coupe - VP",
"doc_count": 426
},
{
"key": "SA -SEDAN",
"doc_count": 414
},
{
"key": "MM -MONOVOLUMEN COMPACTO",
"doc_count": 413
},
{
"key": "Light duty truck",
"doc_count": 390
},
{
"key": "Fourgon lourd",
"doc_count": 384
},
{
"key": "SUV 4x4",
"doc_count": 382
},
{
"key": "Furgon largo",
"doc_count": 356
},
{
"key": "MONO-S",
"doc_count": 352
},
{
"key": "businessclass Cars",
"doc_count": 352
},
{
"key": "berline - VP",
"doc_count": 349
},
{
"key": "Mini monospace",
"doc_count": 332
},
{
"key": "Minivan",
"doc_count": 326
},
{
"key": "fourgon tôle - VU",
"doc_count": 319
},
{
"key": "Van / minibus, Number of doors 4",
"doc_count": 315
},
{
"key": "Fourgon leger",
"doc_count": 307
},
{
"key": "Kasten",
"doc_count": 284
},
{
"key": "Convertible",
"doc_count": 266
},
{
"key": "MPV (multi-purpose vehicle)",
"doc_count": 252
}
]
}
}
}
}
I made a change base on Mihi's comment.
describe('POST method', () => {
it('get a list of faceted aggregations', () => {
cy.request({
method: 'POST',
url:"host endpoint",
headers:{
"Authorization":"xxxx",
"content-type": "application/json"
},
body: {
"search": {
"auctionType": [
"basic_auction"
],
"statuses": [
"ongoing"
],
"sites": [
"autobid.de"
],
"urls": [
"https://autobid.de?action=car&show=details&id=2014918&L=1"
]
}
},
failOnStatusCode: false
})
.then((res) => {
return response.body.data.aggregations['filters.bodyTypes'].buckets
})
.then(bodyTypeObjects => {
expect(bodyTypeObjects.length).to.eq(90)
const bodyTypeCounts = bodyTypeObjects.reduce((acc, bodyTypeObject) => {
acc[bodyTypeObject.key] = bodyTypeObject.doc_count
return acc
}, {})
return bodyTypeCounts
})
.then(bodyTypeCounts => {
expect(res.status).to.eq(200)
expect(bodyTypeCounts).to.have.property('Station Wagon')
expect(bodyTypeCounts['Station Wagon']).to.be.gt(1000)
And my error after running is like below:
request POST 200 https:xxxx
2 then function(){}
ReferenceError
response is not defined
cypress/integration/specs/API-Testing/auctionAggregations.spec.js:41:15
39 | .then((res) => {
40 |
> 41 | return response.body.data.aggregations['filters.bodyTypes'].buckets
| ^
42 | })
43 | .then(bodyTypeObjects => {
44 |
CodePudding user response:
For me, it is better to transform the big json before testing.
The response has this nesting
{
data: {
count: 252655,
aggregations: {
"filters.bodyTypes": {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [
{
key: "Station wagon",
doc_count: 45778,
},
but that makes the test code harder to figure out.
Start by pulling out the aggregations with this function
(response) => {
return response.body.data.aggregations['filters.bodyTypes'].buckets
}
You can then check things like the number of bodyTypes
cy.request({
method: 'POST',
...
})
.then(response => {
return response.body.data.aggregations['filters.bodyTypes'].buckets
})
.then(bodyTypeObjects => {
expect(bodyTypeObjects.length).to.eq(90)
})
To check individual items, you need to deconstruct the bodyTypeObjects, e.g
const match = Cypress._.filter(bodyTypeObjects, Cypress._.matches({key: 'Station Wagon'}))
expect(match).to.have.length(1)
but to make the test code simpler, do another transform from this shape
[{key: 'Station wagon', doc_count: 45778}, {key: 'SUV', doc_count: 30188}, ...]
to this shape
{ Station wagon: 45778, SUV: 30188, ... }
Then the tests become a lot simpler
Full test
cy.request({
method: 'POST',
...
})
.then(response => {
return response.body.data.aggregations['filters.bodyTypes'].buckets
})
.then(bodyTypeObjects => {
expect(bodyTypeObjects.length).to.eq(90)
const bodyTypeCounts = bodyTypeObjects.reduce((acc, bodyTypeObject) => {
acc[bodyTypeObject.key] = bodyTypeObject.doc_count
return acc
}, {})
return bodyTypeCounts
})
.then(bodyTypeCounts => {
expect(bodyTypeCounts).to.have.property('Station Wagon')
expect(bodyTypeCounts['Station Wagon']).to.be.gt(1000)
// OR
cy.wrap(bodyTypeCounts)
.should('have.property', 'Station Wagon', 1673)
})
CodePudding user response:
Response validation by fixture
This was previously shown, but has since been remove.
For completeness, I'll add it here
cy.fixture('faceted-aggregations-response.json').then(expectedResponse => {
cy.request({
method: 'POST',
url:"host endpoints",
...
}).then(response => {
// use deep.eq to check all nested sub-properties
expect(response).to.deep.eq(expectedResponse)
})
})
If the order of aggregations can change each time, you will need to install an additional package
yarn add deep-equal-in-any-order
Test
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
chai.use(deepEqualInAnyOrder);
cy.fixture('faceted-aggregations-response.json').then(expectedResponse => {
cy.request({
method: 'POST',
url:"host endpoints",
...
}).then(response => {
expect(response).to.deep.equalInAnyOrder(expectedResponse)
})
})