Skip to content

Commit 8e64b11

Browse files
committed
fix finding duplicate action log elements on cypress tests in chapter 8
1 parent e8e3dc3 commit 8e64b11

File tree

8 files changed

+62
-29
lines changed

8 files changed

+62
-29
lines changed

chapter8/1_writing_end_to_end_tests/2_writing_your_first_tests/cypress/integration/itemSubmission.spec.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ describe("item submission", () => {
4343
.contains("Undo")
4444
.click();
4545

46-
cy.get("li").contains("cheesecake - Quantity: 10");
46+
cy.get("p")
47+
.then(p => {
48+
return Array.from(p).filter(p => {
49+
return p.innerText.contains(
50+
'The inventory has been updated - {"cheesecake":10}'
51+
);
52+
});
53+
})
54+
.should("have.length", 2);
4755
});
4856

4957
it("saves each submission to the action log", () => {
@@ -66,9 +74,16 @@ describe("item submission", () => {
6674
.click();
6775

6876
cy.get("p").contains("The inventory has been updated - {}");
69-
cy.get("p").contains('The inventory has been updated - {"cheesecake":10}');
77+
cy.get("p")
78+
.then(p => {
79+
return Array.from(p).filter(p => {
80+
return p.innerText.contains(
81+
'The inventory has been updated - {"cheesecake":10}'
82+
);
83+
});
84+
})
85+
.should("have.length", 2);
7086
cy.get("p").contains('The inventory has been updated - {"cheesecake":15}');
71-
cy.get("p").contains('The inventory has been updated - {"cheesecake":10}');
7287
});
7388

7489
describe("given a user enters an invalid item name", () => {

chapter8/1_writing_end_to_end_tests/3_sending_http_requests/cypress/integration/itemSubmission.spec.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ describe("item submission", () => {
4343
.contains("Undo")
4444
.click();
4545

46-
cy.get("li").contains("cheesecake - Quantity: 10");
46+
cy.get("p")
47+
.then(p => {
48+
return Array.from(p).filter(p => {
49+
return p.innerText.contains(
50+
'The inventory has been updated - {"cheesecake":10}'
51+
);
52+
});
53+
})
54+
.should("have.length", 2);
4755
});
4856

4957
it("saves each submission to the action log", () => {
@@ -66,9 +74,16 @@ describe("item submission", () => {
6674
.click();
6775

6876
cy.get("p").contains("The inventory has been updated - {}");
69-
cy.get("p").contains('The inventory has been updated - {"cheesecake":10}');
7077
cy.get("p").contains('The inventory has been updated - {"cheesecake":15}');
71-
cy.get("p").contains('The inventory has been updated - {"cheesecake":10}');
78+
cy.get("p")
79+
.then(p => {
80+
return Array.from(p).filter(p => {
81+
return p.innerText.contains(
82+
'The inventory has been updated - {"cheesecake":10}'
83+
);
84+
});
85+
})
86+
.should("have.length", 2);
7287
});
7388

7489
describe("given a user enters an invalid item name", () => {

chapter8/2_best_practices_for_end_to_end_tests/1_page_objects/cypress/integration/itemSubmission.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ describe("item submission", () => {
3232
InventoryManagement.findItemEntry("cheesecake", "10");
3333

3434
InventoryManagement.findAction({});
35-
InventoryManagement.findAction({ cheesecake: 10 });
35+
InventoryManagement.findAction({ cheesecake: 10 }).should("have.length", 2);
3636
InventoryManagement.findAction({ cheesecake: 15 });
37-
InventoryManagement.findAction({ cheesecake: 10 });
3837
});
3938

4039
describe("given a user enters an invalid item name", () => {

chapter8/2_best_practices_for_end_to_end_tests/1_page_objects/cypress/pageObjects/inventoryManagement.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ export class InventoryManagement {
3939
}
4040

4141
static findAction(inventoryState) {
42-
return cy
43-
.get("p:not(:nth-of-type(1))")
44-
.contains(
45-
`The inventory has been updated - ${JSON.stringify(inventoryState)}`
46-
);
42+
return cy.get("p:not(:nth-of-type(1))").then(p => {
43+
return Array.from(p).filter(p => {
44+
return p.innerText.includes(
45+
`The inventory has been updated - ${JSON.stringify(inventoryState)}`
46+
);
47+
});
48+
});
4749
}
4850
}

chapter8/2_best_practices_for_end_to_end_tests/2_application_actions/cypress/integration/itemSubmission.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ describe("item submission", () => {
4040
InventoryManagement.findItemEntry("cheesecake", "10");
4141

4242
InventoryManagement.findAction({});
43-
InventoryManagement.findAction({ cheesecake: 10 });
43+
InventoryManagement.findAction({ cheesecake: 10 }).should("have.length", 2);
4444
InventoryManagement.findAction({ cheesecake: 15 });
45-
InventoryManagement.findAction({ cheesecake: 10 });
4645
});
4746

4847
describe("given a user enters an invalid item name", () => {

chapter8/2_best_practices_for_end_to_end_tests/2_application_actions/cypress/pageObjects/inventoryManagement.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ export class InventoryManagement {
3939
}
4040

4141
static findAction(inventoryState) {
42-
return cy
43-
.get("p:not(:nth-of-type(1))")
44-
.contains(
45-
`The inventory has been updated - ${JSON.stringify(inventoryState)}`
46-
);
42+
return cy.get("p:not(:nth-of-type(1))").then(p => {
43+
return Array.from(p).filter(p => {
44+
return p.innerText.includes(
45+
`The inventory has been updated - ${JSON.stringify(inventoryState)}`
46+
);
47+
});
48+
});
4749
}
4850
}

chapter8/3_dealing_with_flakiness/1_avoiding_waiting_for_fixed_amounts_of_time/cypress/integration/itemSubmission.spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@ describe("item submission", () => {
3737
InventoryManagement.undo();
3838
InventoryManagement.findItemEntry("cheesecake", "10");
3939

40-
InventoryManagement.findAction({});
41-
InventoryManagement.findAction({ cheesecake: 10 });
42-
InventoryManagement.findAction({ cheesecake: 15 });
43-
InventoryManagement.findAction({ cheesecake: 10 });
40+
InventoryManagement.findAction({}).should("have.length", 1);
41+
InventoryManagement.findAction({ cheesecake: 10 }).should("have.length", 2);
42+
InventoryManagement.findAction({ cheesecake: 15 }).should("have.length", 1);
4443
});
4544

4645
describe("given a user enters an invalid item name", () => {

chapter8/3_dealing_with_flakiness/1_avoiding_waiting_for_fixed_amounts_of_time/cypress/pageObjects/inventoryManagement.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ export class InventoryManagement {
3939
}
4040

4141
static findAction(inventoryState) {
42-
return cy
43-
.get("p:not(:nth-of-type(1))")
44-
.contains(
45-
`The inventory has been updated - ${JSON.stringify(inventoryState)}`
46-
);
42+
return cy.get("p:not(:nth-of-type(1))").then(p => {
43+
return Array.from(p).filter(p => {
44+
return p.innerText.includes(
45+
`The inventory has been updated - ${JSON.stringify(inventoryState)}`
46+
);
47+
});
48+
});
4749
}
4850
}

0 commit comments

Comments
 (0)