https://cypress.io logo
Hi all, I am trying to write some code that assert...
# e2e-testing
c
Hi all, I am trying to write some code that asserts the following: I have a page that will contain a number of elements with
[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)
Copy code
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");
  }