crooked-yak-38149
11/01/2022, 4:15 PM[data-automation-id=tagging-image-<number>]
eg I may expect [data-automation-id=tagging-image-1], [data-automation-id=tagging-image-3] and ``[data-automation-id=tagging-image-5]` to exist, and I want to assert that no other [data-automation-id=tagging-image-<number>] elements exist.
The elements which should be present are in an array - how could I write the below code so that the .not() commands are built up from the array? (The 3 lines after the --> comment)
const assertTaggingPageContainsImages = (selectedImages) => {
for (var i=0; i < selectedImages.length; i++){
//cy.log(`Checking for existence of tagging-image-${selectedImages[i]}`)
// Check that this image exists
cy.getTestTag(`tagging-image-${selectedImages[i]}`)
.should('exist');
}
// ToDo: Assert that no other tagging-image- elements exist, other than the ones that should
cy
// Get list of all tagging-image elements
.get(`[data-automation-id^="tagging-image-"]`)
// Filter out ones that we SHOULD see
// --> How do I re-write the below commands to dynamically build depending on the number of elements in the array?
.not(`[data-automation-id=tagging-image-${selectedImages[0]}]`)
.not(`[data-automation-id=tagging-image-${selectedImages[1]}]`)
.not(`[data-automation-id=tagging-image-${selectedImages[2]}]`)
// Assert that no others exist
.should("not.exist");
}