Test if all your favicons exist

Updated . Posted . Visible to the public.

When you don't only have a favicon.ico in your project but also PNGs of different sizes and backgrounds, you should test if all those files are actually reachable.

Here are a few selectors to get you started:

    'link[rel~="icon"]' # regular ones, matches "shortcut icon" and "icon"
    'link[rel="apple-touch-icon"]' # iOS
    'meta[content][name="msapplication-TileImage"]' # IE11
    'meta[content][name^="msapplication-square"]' # IE11

A simple integration spec should be enough. Just visit each icon's URL and check the response code. Remember to pass visible: false for each selector so Capybara can find them.

Example

The following test was taken from a project using ActionDispatch::IntegrationTest. Though we usually use RSpec, this should give you the general idea.

    context 'favicons' do

      def link_selectors
        [
          ['link[href][rel="shortcut icon"]', visible: false],
          ['link[href][rel="icon"]', visible: false],
          ['link[href][rel="apple-touch-icon"]', visible: false]
        ]
      end

      def meta_selectors
        [
          ['meta[content][name="msapplication-TileImage"]', visible: false],
          ['meta[content][name^="msapplication-square"]', visible: false]
        ]
      end

      setup do
        visit home_path
      end

      should 'be present' do
        (link_selectors + meta_selectors).each do |selector|
          assert page.has_css?(*selector), "Could not find #{selector.first}"
        end
      end

      should 'all be accessible' do
        urls = []
        link_selectors.each do |selector|
          urls += page.all(*selector).map { |link| link['href'] }
        end
        meta_selectors.each do |selector|
          urls += page.all(*selector).map { |meta| meta['content'] }
        end

        urls.each do |url|
          visit url
          assert_equal 200, page.status_code
        end
      end

    end
Arne Hartherz
Last edit
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2014-07-21 08:17)