How to check whether image exist using chai or mocha?

by elisha_langworth , in category: Javascript , 3 months ago

How to check whether image exist using chai or mocha?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jerad , 2 months ago

@elisha_langworth 

To check whether an image exists using Chai or Mocha, you can first check if the image element exists in the DOM by using a selector or ID and then assert that the image's src attribute is not empty. Here is an example using Chai and Mocha:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const { expect } = require('chai');

describe('Image Existence Test', () => {
  it('should check if image exists', () => {
    const imageElement = document.querySelector('img'); // Assuming the image element exists in the DOM
    expect(imageElement).to.exist;
    
    const imageUrl = imageElement.getAttribute('src');
    expect(imageUrl).to.not.be.empty;
  });
});


This test will check if the image element exists in the DOM and if the src attribute of the image is not empty. If both conditions are met, the test will pass.