From 8affe5308ff742edff85293e193bd238090744f9 Mon Sep 17 00:00:00 2001 From: Andrew Garner Date: Wed, 18 Jul 2018 14:21:34 +0100 Subject: [PATCH 1/7] Migrate testing from PhantomJS to Chrome PhantomJS development has now been abandoned and there are some outstanding bugs that will not be fixed. See https://docs.publishing.service.gov.uk/manual/migrate-testing-from-phantomjs-to-selenium-chrome.html --- Gemfile | 4 +++- Gemfile.lock | 28 +++++++++++++--------------- test/support/poltergeist.rb | 5 ----- test/test_helper.rb | 13 ++++++++----- 4 files changed, 24 insertions(+), 26 deletions(-) delete mode 100644 test/support/poltergeist.rb diff --git a/Gemfile b/Gemfile index b40a65f17..25a250c9f 100644 --- a/Gemfile +++ b/Gemfile @@ -36,8 +36,10 @@ end group :test do gem 'capybara' + gem 'chromedriver-helper' gem 'faker' gem 'mocha' - gem 'poltergeist', require: false + gem 'puma' + gem 'selenium-webdriver' gem 'webmock', '~> 3.4.2', require: false end diff --git a/Gemfile.lock b/Gemfile.lock index c9cad68d7..2d0fea183 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -63,19 +63,18 @@ GEM debug_inspector (>= 0.0.1) builder (3.2.3) byebug (10.0.1) - capybara (2.18.0) + capybara (3.3.1) addressable mini_mime (>= 0.1.3) - nokogiri (>= 1.3.3) - rack (>= 1.0.0) - rack-test (>= 0.5.4) - xpath (>= 2.0, < 4.0) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + xpath (~> 3.1) childprocess (0.9.0) ffi (~> 1.0, >= 1.0.11) chromedriver-helper (1.2.0) archive-zip (~> 0.10) nokogiri (~> 1.8) - cliver (0.3.2) coderay (1.1.2) commander (4.4.5) highline (~> 1.7.2) @@ -145,7 +144,7 @@ GEM domain_name (~> 0.5) i18n (0.9.5) concurrent-ruby (~> 1.0) - image_size (1.5.0) + image_size (2.0.0) io-like (0.3.0) jasmine-core (2.99.2) jasmine-rails (0.14.7) @@ -201,10 +200,6 @@ GEM ast (~> 2.4.0) phantomjs (2.1.1.0) plek (2.1.1) - poltergeist (1.17.0) - capybara (~> 2.1) - cliver (~> 0.3.1) - websocket-driver (>= 0.2.0) powerpack (0.1.1) pry (0.11.3) coderay (~> 1.1.0) @@ -213,6 +208,7 @@ GEM byebug (~> 10.0) pry (~> 0.10) public_suffix (3.0.2) + puma (3.12.0) rack (2.0.5) rack-cache (1.8.0) rack (>= 0.4) @@ -297,7 +293,7 @@ GEM scss_lint (0.57.0) rake (>= 0.9, < 13) sass (~> 3.5.5) - selenium-webdriver (3.11.0) + selenium-webdriver (3.13.0) childprocess (~> 0.5) rubyzip (~> 1.2) sentry-raven (2.7.4) @@ -350,7 +346,7 @@ GEM robotex selenium-webdriver (~> 3.5) thor - xpath (3.0.0) + xpath (3.1.0) nokogiri (~> 1.8) PLATFORMS @@ -361,6 +357,7 @@ DEPENDENCIES better_errors binding_of_caller capybara + chromedriver-helper dalli faker gds-api-adapters (~> 52.6) @@ -374,14 +371,15 @@ DEPENDENCIES jasmine-rails mocha plek (~> 2.1) - poltergeist pry-byebug + puma rack_strip_client_ip (~> 0.0.2) rails (~> 5.2.0) rails-controller-testing (~> 1.0) rails-i18n (>= 4.0.4) rails_translation_manager (~> 0.0.2) sass-rails (~> 5.0) + selenium-webdriver slimmer (~> 13.0) uglifier (>= 1.3.0) webmock (~> 3.4.2) @@ -391,4 +389,4 @@ RUBY VERSION ruby 2.5.1p57 BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/test/support/poltergeist.rb b/test/support/poltergeist.rb deleted file mode 100644 index 99be1b8a5..000000000 --- a/test/support/poltergeist.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'capybara/poltergeist' -require 'phantomjs/poltergeist' - -Capybara.default_driver = :poltergeist -Capybara.javascript_driver = :poltergeist diff --git a/test/test_helper.rb b/test/test_helper.rb index 55a27a77e..723cd4f7b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,15 +6,13 @@ require 'rails/test_help' require 'capybara/rails' require 'mocha/mini_test' +require 'capybara/minitest' require 'faker' Dir[Rails.root.join('test/support/*.rb')].each { |f| require f } -class Minitest::Test - def teardown - Capybara.current_session.driver.clear_memory_cache - end -end +Capybara.default_driver = :selenium_chrome_headless +Capybara.javascript_driver = :selenium_chrome_headless GovukAbTesting.configure do |config| config.acceptance_test_framework = :active_support @@ -38,6 +36,11 @@ class ActionDispatch::IntegrationTest # Make the Capybara DSL available in all integration tests include Capybara::DSL + def teardown + Capybara.reset_sessions! + Capybara.use_default_driver + end + def assert_has_component_metadata_pair(label, value) assert page.has_content?(label) assert page.has_content?(value) From 10d7de958de7eeaeb30b72cda887400e1379a360 Mon Sep 17 00:00:00 2001 From: Andrew Garner Date: Thu, 19 Jul 2018 14:57:13 +0100 Subject: [PATCH 2/7] Fix test failures caused by browser caching Using Chrome instead of PhantomJS means cache headers are being correctly observed. A number of test cases share the same content item fixture, but make changes to the body of the content item for the specific test case. We therefore need to add a cachebust string to the URL as the content item base path remains the same. --- test/integration/corporate_information_page_test.rb | 4 ++-- test/integration/document_collection_test.rb | 2 +- test/integration/html_publication_test.rb | 4 ++-- test/integration/meta_tags_test.rb | 6 +++--- test/integration/phase_label_test.rb | 4 ++-- test/integration/statistical_data_set_test.rb | 2 +- test/integration/topical_event_about_page_test.rb | 4 ++-- test/integration/working_group_test.rb | 2 +- test/test_helper.rb | 7 +++++++ 9 files changed, 21 insertions(+), 14 deletions(-) diff --git a/test/integration/corporate_information_page_test.rb b/test/integration/corporate_information_page_test.rb index 3212678a4..e0009f353 100644 --- a/test/integration/corporate_information_page_test.rb +++ b/test/integration/corporate_information_page_test.rb @@ -25,7 +25,7 @@ class CorporateInformationPageTest < ActionDispatch::IntegrationTest " content_store_has_item(item["base_path"], item.to_json) - visit(item["base_path"]) + visit_with_cachebust(item["base_path"]) refute page.has_css?(".gem-c-contents-list") end @@ -81,7 +81,7 @@ class CorporateInformationPageTest < ActionDispatch::IntegrationTest } content_store_has_item("/government/organisations/department-of-health/about", content_item.to_json) - visit "/government/organisations/department-of-health/about" + visit_with_cachebust "/government/organisations/department-of-health/about" assert page.has_css?(".gem-c-notice__title", text: "This information page was withdrawn on 9 August 2014") assert page.has_css?(".gem-c-notice", text: "This is out of date") diff --git a/test/integration/document_collection_test.rb b/test/integration/document_collection_test.rb index 43e9bb991..b0b464413 100644 --- a/test/integration/document_collection_test.rb +++ b/test/integration/document_collection_test.rb @@ -54,7 +54,7 @@ class DocumentCollectionTest < ActionDispatch::IntegrationTest }, ] content_store_has_item(item["base_path"], item.to_json) - visit(item["base_path"]) + visit_with_cachebust(item["base_path"]) refute page.has_css?(".gem-c-contents-list") end diff --git a/test/integration/html_publication_test.rb b/test/integration/html_publication_test.rb index 7b075a073..33bb23e88 100644 --- a/test/integration/html_publication_test.rb +++ b/test/integration/html_publication_test.rb @@ -87,7 +87,7 @@ def assert_has_component_organisation_logo_with_brand(brand, index = 1) } content_store_has_item("/government/publications/canada-united-kingdom-joint-declaration/canada-united-kingdom-joint-declaration", content_item.to_json) - visit "/government/publications/canada-united-kingdom-joint-declaration/canada-united-kingdom-joint-declaration" + visit_with_cachebust "/government/publications/canada-united-kingdom-joint-declaration/canada-united-kingdom-joint-declaration" assert page.has_css?(".gem-c-notice__title", text: "This policy paper was withdrawn on 9 August 2014") assert page.has_css?(".gem-c-notice", text: "This is out of date") @@ -102,7 +102,7 @@ def assert_has_component_organisation_logo_with_brand(brand, index = 1) } content_store_has_item("/government/publications/canada-united-kingdom-joint-declaration/canada-united-kingdom-joint-declaration", content_item.to_json) - visit "/government/publications/canada-united-kingdom-joint-declaration/canada-united-kingdom-joint-declaration" + visit_with_cachebust "/government/publications/canada-united-kingdom-joint-declaration/canada-united-kingdom-joint-declaration" assert page.has_css?(".gem-c-notice__title", text: "This publication was withdrawn on 9 August 2014") end diff --git a/test/integration/meta_tags_test.rb b/test/integration/meta_tags_test.rb index e9fda47d9..91d5b9a14 100644 --- a/test/integration/meta_tags_test.rb +++ b/test/integration/meta_tags_test.rb @@ -11,7 +11,7 @@ class MetaTagsTest < ActionDispatch::IntegrationTest content_store_has_item("/some-page", case_study.to_json) - visit "/some-page" + visit_with_cachebust "/some-page" assert page.has_css?("meta[property='og:title'][content='Zhe title']", visible: false) end @@ -24,7 +24,7 @@ class MetaTagsTest < ActionDispatch::IntegrationTest content_store_has_item("/some-page", case_study.to_json) - visit "/some-page" + visit_with_cachebust "/some-page" assert page.has_css?("meta[name='twitter:card'][content='summary']", visible: false) end @@ -43,7 +43,7 @@ class MetaTagsTest < ActionDispatch::IntegrationTest content_store_has_item("/some-page", case_study.to_json) - visit "/some-page" + visit_with_cachebust "/some-page" assert page.has_css?("meta[name='twitter:card'][content='summary_large_image']", visible: false) assert page.has_css?("meta[name='twitter:image'][content='https://example.org/image.jpg']", visible: false) diff --git a/test/integration/phase_label_test.rb b/test/integration/phase_label_test.rb index b5e147ad1..328f17ce9 100644 --- a/test/integration/phase_label_test.rb +++ b/test/integration/phase_label_test.rb @@ -7,7 +7,7 @@ class PhaseLabelTest < ActionDispatch::IntegrationTest content_store_has_item("/government/case-studies/get-britain-building-carlisle-park", case_study.to_json) - visit "/government/case-studies/get-britain-building-carlisle-park" + visit_with_cachebust "/government/case-studies/get-britain-building-carlisle-park" assert page.has_text?("alpha") end @@ -17,7 +17,7 @@ class PhaseLabelTest < ActionDispatch::IntegrationTest content_item.delete("phase") content_store_has_item("/government/case-studies/get-britain-building-carlisle-park", content_item.to_json) - visit "/government/case-studies/get-britain-building-carlisle-park" + visit_with_cachebust "/government/case-studies/get-britain-building-carlisle-park" refute page.has_text?("alpha") end diff --git a/test/integration/statistical_data_set_test.rb b/test/integration/statistical_data_set_test.rb index 5b79a598a..7f178531b 100644 --- a/test/integration/statistical_data_set_test.rb +++ b/test/integration/statistical_data_set_test.rb @@ -63,7 +63,7 @@ class StatisticalDataSetTest < ActionDispatch::IntegrationTest " content_store_has_item(item["base_path"], item.to_json) - visit(item["base_path"]) + visit_with_cachebust(item["base_path"]) refute page.has_css?(".gem-c-contents-list") end diff --git a/test/integration/topical_event_about_page_test.rb b/test/integration/topical_event_about_page_test.rb index 269ef0fd5..9640c7dda 100644 --- a/test/integration/topical_event_about_page_test.rb +++ b/test/integration/topical_event_about_page_test.rb @@ -28,7 +28,7 @@ class TopicalEventAboutPageTest < ActionDispatch::IntegrationTest content_store_has_item(@content_item["base_path"], @content_item.to_json) - visit @content_item["base_path"] + visit_with_cachebust @content_item["base_path"] refute page.has_css?(".gem-c-contents-list") end @@ -38,7 +38,7 @@ class TopicalEventAboutPageTest < ActionDispatch::IntegrationTest content_store_has_item(@content_item["base_path"], @content_item.to_json) - visit @content_item["base_path"] + visit_with_cachebust @content_item["base_path"] assert page.has_css?(".gem-c-contents-list") end diff --git a/test/integration/working_group_test.rb b/test/integration/working_group_test.rb index 1dc14530b..be99e07f2 100644 --- a/test/integration/working_group_test.rb +++ b/test/integration/working_group_test.rb @@ -50,7 +50,7 @@ class WorkingGroupTest < ActionDispatch::IntegrationTest " content_store_has_item(item["base_path"], item.to_json) - visit(item["base_path"]) + visit_with_cachebust(item["base_path"]) refute page.has_css?(".gem-c-contents-list") end diff --git a/test/test_helper.rb b/test/test_helper.rb index 723cd4f7b..d610d3ffc 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -161,4 +161,11 @@ def get_content_example_by_schema_and_name(schema_type, name) def schema_type self.class.to_s.gsub('Test', '').underscore end + + def visit_with_cachebust(visit_uri) + uri = Addressable::URI.parse(visit_uri) + uri.query_values = uri.query_values.yield_self { |values| (values || {}).merge(cachebust: rand) } + + visit(uri) + end end From 8551c1fff81290f8e05723e66cf0126e49dace9d Mon Sep 17 00:00:00 2001 From: Andrew Garner Date: Thu, 19 Jul 2018 15:00:03 +0100 Subject: [PATCH 3/7] Fix test failures caused by browser whitespace Using Chrome instead of PhantomJS means that whitespace is interpreted differently. Our assertions ignore things like new line breaks which are returned by Capybara. --- test/integration/consultation_test.rb | 4 ++-- test/integration/fatality_notice_test.rb | 4 ++-- test/integration/html_publication_test.rb | 2 +- test/integration/speech_test.rb | 2 +- test/integration/statistics_announcement_test.rb | 4 ++-- test/integration/travel_advice_print_test.rb | 2 +- test/integration/travel_advice_test.rb | 2 +- test/integration/world_location_news_article_test.rb | 2 +- 8 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/integration/consultation_test.rb b/test/integration/consultation_test.rb index 548faf25a..a24d273d7 100644 --- a/test/integration/consultation_test.rb +++ b/test/integration/consultation_test.rb @@ -40,7 +40,7 @@ class ConsultationTest < ActionDispatch::IntegrationTest setup_and_visit_content_item('open_consultation') assert page.has_text?("Open consultation") - assert page.has_text?("closes at 3pm on 16 December 2216") + assert page.has_text?(:all, "closes at 3pm on 16 December 2216") end test "unopened consultation" do @@ -51,7 +51,7 @@ class ConsultationTest < ActionDispatch::IntegrationTest # There’s no daylight savings after 2037 # http://timezonesjl.readthedocs.io/en/stable/faq/#far-future-zoneddatetime-with-variabletimezone assert page.has_css?('.gem-c-notice', text: "This consultation opens at 1pm on 5 October 2200") - assert page.has_text?("It closes at 4pm on 31 October 2210") + assert page.has_text?(:all, "It closes at 4pm on 31 October 2210") end test "closed consultation pending outcome" do diff --git a/test/integration/fatality_notice_test.rb b/test/integration/fatality_notice_test.rb index 0cd12e80b..149ce16f0 100644 --- a/test/integration/fatality_notice_test.rb +++ b/test/integration/fatality_notice_test.rb @@ -49,11 +49,11 @@ class FatalityNoticeTest < ActionDispatch::IntegrationTest within(".app-c-published-dates__change-history") do within(".app-c-published-dates__change-item:first-child") do - assert page.has_content?("14 September 2016 Updated information.") + assert page.has_content?(:all, "14 September 2016 Updated information.") end within(".app-c-published-dates__change-item:last-child") do - assert page.has_content?("27 February 1881 First published.") + assert page.has_content?(:all, "27 February 1881 First published.") end end end diff --git a/test/integration/html_publication_test.rb b/test/integration/html_publication_test.rb index 33bb23e88..29b403006 100644 --- a/test/integration/html_publication_test.rb +++ b/test/integration/html_publication_test.rb @@ -45,7 +45,7 @@ class HtmlPublicationTest < ActionDispatch::IntegrationTest assert page.has_text?("© Crown copyright #{@content_item['details']['public_timestamp'].to_date.year}") assert page.has_text?("Any enquiries regarding this publication should be sent to us at:") - assert page.has_text?((@content_item['details']['print_meta_data_contact_address']).to_s) + assert page.has_text?(:all, @content_item['details']['print_meta_data_contact_address'].squish) assert page.has_text?("Print ISBN: #{@content_item['details']['isbn']}") end end diff --git a/test/integration/speech_test.rb b/test/integration/speech_test.rb index fa3fdc719..0d8e38231 100644 --- a/test/integration/speech_test.rb +++ b/test/integration/speech_test.rb @@ -6,7 +6,7 @@ class SpeechTest < ActionDispatch::IntegrationTest assert_has_component_title(@content_item["title"]) assert page.has_text?(@content_item["description"]) - assert page.has_text?("These subjects are of course crucial. But in the UK, only 1 woman to every 7 men works in science, technology, engineering and maths. We need to get more girls interested and excited about STEM subjects.") + assert page.has_text?("These subjects are of course crucial. But in the UK, only 1 woman to every 7 men works in science, technology, engineering and maths. We need to get more girls interested and excited about STEM subjects.") end test "translated speech" do diff --git a/test/integration/statistics_announcement_test.rb b/test/integration/statistics_announcement_test.rb index 020941dba..169c58d0d 100644 --- a/test/integration/statistics_announcement_test.rb +++ b/test/integration/statistics_announcement_test.rb @@ -18,7 +18,7 @@ class StatisticsAnnouncementTest < ActionDispatch::IntegrationTest assert page.has_css?('img[alt="National Statistics"]') within '.app-c-important-metadata' do - assert page.has_text?("Release date: January 2016 (provisional)") + assert page.has_text?(:all, "Release date: January 2016 (provisional)") end end @@ -43,7 +43,7 @@ class StatisticsAnnouncementTest < ActionDispatch::IntegrationTest assert_has_component_title(@content_item["title"]) assert page.has_text?(@content_item["description"]) - assert page.has_text?("Release date: 20 January 2016 9:30am (confirmed)") + assert page.has_text?(:all, "Release date: 20 January 2016 9:30am (confirmed)") within '.release-date-changed .app-c-important-metadata' do assert page.has_text?("The release date has been changed") diff --git a/test/integration/travel_advice_print_test.rb b/test/integration/travel_advice_print_test.rb index 400f379d6..db5971efb 100644 --- a/test/integration/travel_advice_print_test.rb +++ b/test/integration/travel_advice_print_test.rb @@ -19,7 +19,7 @@ class TravelAdvicePrint < ActionDispatch::IntegrationTest assert_has_component_metadata_pair("Updated", Date.parse(@content_item["details"]["reviewed_at"]).strftime("%-d %B %Y")) within ".gem-c-metadata" do - assert page.has_content?(@content_item['details']['change_description'].gsub('Latest update: ', '')) + assert page.has_content?(@content_item['details']['change_description'].gsub('Latest update: ', '').strip) end assert page.has_css?("h1", text: 'Summary') diff --git a/test/integration/travel_advice_test.rb b/test/integration/travel_advice_test.rb index c0602724a..24b3427ee 100644 --- a/test/integration/travel_advice_test.rb +++ b/test/integration/travel_advice_test.rb @@ -37,7 +37,7 @@ class TravelAdviceTest < ActionDispatch::IntegrationTest assert_has_component_metadata_pair("Updated", Date.parse(@content_item["details"]["reviewed_at"]).strftime("%-d %B %Y")) within ".gem-c-metadata" do - assert page.has_content?(@content_item['details']['change_description'].gsub('Latest update: ', '')) + assert page.has_content?(@content_item['details']['change_description'].gsub('Latest update: ', '').strip) end assert page.has_css?(".map img[src=\"#{@content_item['details']['image']['url']}\"]") diff --git a/test/integration/world_location_news_article_test.rb b/test/integration/world_location_news_article_test.rb index 03d2177a5..11fc9a5e6 100644 --- a/test/integration/world_location_news_article_test.rb +++ b/test/integration/world_location_news_article_test.rb @@ -27,7 +27,7 @@ class WorldLocationNewsArticleTest < ActionDispatch::IntegrationTest assert page.has_css?(".gem-c-translation-nav") assert page.has_css?(".gem-c-translation-nav__list-item") - assert page.has_content?("English हिंदी 日本語 中文 中文") + assert page.has_content?(:all, "English हिंदी 日本語 中文 中文") refute page.has_link?("English") From 4ce9f74436eaa73703de8b4d0fb25839ca626634 Mon Sep 17 00:00:00 2001 From: Andrew Garner Date: Thu, 19 Jul 2018 15:01:25 +0100 Subject: [PATCH 4/7] Fix test failures caused by browser restrictions Using Chrome instead of PhantomJS means we can no longer manipulate the browser by executing a script on the page. This is not something a user would typically do therefore Selenium prevents it. --- test/integration/service_sign_in/choose_sign_in_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/service_sign_in/choose_sign_in_test.rb b/test/integration/service_sign_in/choose_sign_in_test.rb index cd76a7462..71ca85ea9 100644 --- a/test/integration/service_sign_in/choose_sign_in_test.rb +++ b/test/integration/service_sign_in/choose_sign_in_test.rb @@ -50,7 +50,7 @@ class ChooseSignInTest < ActionDispatch::IntegrationTest test "renders errors correctly" do setup_and_visit_choose_sign_in_page("service_sign_in", "/choose-sign-in") - page.execute_script('document.querySelector(\'form\').submit()') + click_on 'Continue' assert page.has_css?(".gem-c-error-summary") assert page.has_css?(".gem-c-error-summary__title", text: 'You haven’t selected an option') From fbdac0e1c364d86ad81a0844da1332bb85d6585c Mon Sep 17 00:00:00 2001 From: Andrew Garner Date: Thu, 19 Jul 2018 15:03:49 +0100 Subject: [PATCH 5/7] Fix test failures caused by status code assertion Using Chrome instead of PhantomJS means we can no access the status code. This is not something a user would typically do therefore Selenium prevents it. Instead we look for a `govuk-content-id` meta tag with the correct content attribute that will be rendered if the page is successfully rendered. --- test/test_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index d610d3ffc..dd06deca2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -140,13 +140,15 @@ def setup_and_visit_random_content_item(document_type: nil) payload.merge('document_type' => document_type) unless document_type.nil? payload end + + content_id = content_item["content_id"] path = content_item["base_path"] stub_request(:get, %r{#{path}}) .to_return(status: 200, body: content_item.to_json, headers: {}) visit path - assert_equal 200, page.status_code + assert_selector %{meta[name="govuk:content-id"][content="#{content_id}"}, visible: false end def get_content_example(name) From 2c7c9dcf82578fd2a9eed48d7af8eb09130d99c7 Mon Sep 17 00:00:00 2001 From: Andrew Garner Date: Thu, 19 Jul 2018 15:06:32 +0100 Subject: [PATCH 6/7] Remove line break assertion MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using Chrome instead of PhantomJS means we can no do this. This isn’t part of user acceptance so I’ve removed the assertion. --- test/integration/consultation_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/integration/consultation_test.rb b/test/integration/consultation_test.rb index a24d273d7..84259d4a5 100644 --- a/test/integration/consultation_test.rb +++ b/test/integration/consultation_test.rb @@ -126,7 +126,6 @@ class ConsultationTest < ActionDispatch::IntegrationTest within '.consultation-ways-to-respond' do assert page.has_css?(".contact .content p", text: '2016 Post Office Network Consultation') - assert page.has_css?(".contact .content p br") end end From cabafae7933723e4ac1b78fd0cd258fc29e7380b Mon Sep 17 00:00:00 2001 From: Andrew Garner Date: Thu, 19 Jul 2018 15:08:10 +0100 Subject: [PATCH 7/7] Fix atom feed test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Requesting the atom feed through Chrome gives us a HTML response. Instead we make a direct request and parse it using Ruby’s RSS library. --- .../travel_advice_atom_feed_test.rb | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/test/integration/travel_advice_atom_feed_test.rb b/test/integration/travel_advice_atom_feed_test.rb index 4d21a4a4b..4d80ebf8a 100644 --- a/test/integration/travel_advice_atom_feed_test.rb +++ b/test/integration/travel_advice_atom_feed_test.rb @@ -1,41 +1,52 @@ require 'test_helper' +require 'nokogiri/html' +require 'open-uri' +require 'rss' + class TravelAdviceAtomFeed < ActionDispatch::IntegrationTest setup do - setup_and_visit_travel_advice_atom_feed('full-country') + setup_and_parse_travel_advice_atom_feed('full-country') @base_path = @content_item['base_path'] @updated_at = DateTime.parse(@content_item["public_updated_at"]).rfc3339 end test "it sets the alternative link correctly" do - assert page.has_css?("feed>link[rel='alternate'][href$='#{@base_path}']") + alternate_link = @feed.links.find { |link| link.rel == 'alternate' } + assert alternate_link.href.ends_with?(@base_path) end test "it sets the entry's id to the url concatenated with updated_at" do - id = page.find("feed>entry>id").text(:all) + id = @feed.items.first.id.content assert id.end_with?("#{@base_path}##{@updated_at}") end test "it sets the entry's title correctly" do - title = page.find("feed>entry>title").text(:all) + title = @feed.items.first.title.content assert_equal title, @content_item['title'] end test "it sets the entry's summary correctly" do - summary = page.find("feed>entry>summary").text(:all) - assert_equal summary, @content_item['details']['change_description'].strip + summary = Nokogiri::HTML::fragment(@feed.items.first.summary.content) + assert_equal summary.text.strip, @content_item['details']['change_description'].strip end test "it sets the entry's updated correctly" do - updated = page.find("feed>entry>updated").text(:all) + updated = @feed.items.first.updated.content assert_equal updated, @updated_at end - def setup_and_visit_travel_advice_atom_feed(name) - example = get_content_example_by_schema_and_name('travel_advice', name) - @content_item = example.tap do |item| - content_store_has_item(item["base_path"], item.to_json) - visit "#{item['base_path']}.atom" - end + def setup_and_parse_travel_advice_atom_feed(name) + @content_item = get_content_example_by_schema_and_name('travel_advice', name) + + uri = URI::HTTP.build( + host: Capybara.current_session.server.host, + port: Capybara.current_session.server.port, + path: "#{@content_item['base_path']}.atom" + ) + + content_store_has_item(@content_item["base_path"], @content_item.to_json) + + @feed = RSS::Parser.parse(uri.read) end end