csscucumbercypressassertioncypress-xpath

Cypress / JavaScript: assertion with an URL value that contains a specific word


I'm trying to improve the following assertion:

cy.xpath('//div[@data-testid="column"]')
  .should('have.css', 'background-image', 
    'url("https://assets.website.com/folder/house.jpg")'
  );

In the test I am verifying if that div has the CSS background-image property with that url value.

I want to convert this assertion to verifying that the div has the css background-image property with a value that contains "house.jpg" instead of the entire URL.


Solution

  • You can also do it with a simple string include check

    cy.xpath('//div[@data-testid="column"]')
      .should('have.css', 'background-image')  // changes subject to css value
      .and('include', 'house.jpg')