Posted over 6 years ago. Visible to the public.

Test if all your favicons exist

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:

Copy
'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.

Copy
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

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
over 6 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more