diff --git a/lib/summarise_errors_matcher.rb b/lib/summarise_errors_matcher.rb index 1e3d78c..0cabfba 100644 --- a/lib/summarise_errors_matcher.rb +++ b/lib/summarise_errors_matcher.rb @@ -60,12 +60,21 @@ def all_error_messages_links_are_valid link = error_message_item.all(:link).first if link - link_fragment = link[:href].split('#').last + uri = URI(link[:href]) - link_target= html.all(id: link_fragment).first || html.all(:field, name: link_fragment).first + # If the link is a fragment (href=#name) link, check that + # it actually links to a field on the page. + if uri.fragment && uri.path == "" + link_target = html.all(:field, id: uri.fragment).first || html.all(:field, name: uri.fragment).first + + link_target + else + # Skip - error summary links to another page + true + end - link_target else + # No links found false end end diff --git a/spec/summarise_errors_matcher_spec.rb b/spec/summarise_errors_matcher_spec.rb index 29034e5..a14dcda 100644 --- a/spec/summarise_errors_matcher_spec.rb +++ b/spec/summarise_errors_matcher_spec.rb @@ -223,4 +223,38 @@ }.to fail_with("Error message ‘Enter your full name’ links to #full-name but no input field has this ID or name") end end + + # This allows for a less-common pattern where the error summary + # is used on a page, but to summarise errors in form fields on a + # separate page. + context "where the errors are linked to a another page" do + + let(:html) { ' +