Find broken images in page & image replace by another image

前端 未结 3 2086
说谎
说谎 2021-02-14 02:46

Hi I am using selenium webdriver 2.25.0 & faceing the some serious issues,

  1. how to find broken images in a page using Selenium Webdriver
  2. How to find th
3条回答
  •  萌比男神i
    2021-02-14 03:05

    Based on the other answers, the code that eventually worked for me in an angular / protractor / webdriverjs setting is:

    it('should find all images', function () {
        var allImgElts = element.all(by.tagName('img'));
    
        browser.executeAsyncScript(function (callback) {
            var imgs = document.getElementsByTagName('img'),
                loaded = 0;
            for (var i = 0; i < imgs.length; i++) {
                if (imgs[i].naturalWidth > 0) {
                    loaded = loaded + 1;
                };
            };
            callback(loaded);
         }).then(function (loadedImagesCount) {
            expect(loadedImagesCount).toBe(allImgElts.count());
        });
    });
    

    The webdriver code counts the number of img elements, and the function executed within the browser context counts the number of successfully loaded elements. These numbers should be the same.

提交回复
热议问题