Read more

Test if all your favicons exist

Arne Hartherz
July 21, 2014Software engineer at makandra GmbH

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.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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
July 21, 2014Software engineer at makandra GmbH
Posted by Arne Hartherz to makandra dev (2014-07-21 10:17)