diff --git a/Gemfile b/Gemfile index cb66f4b0..1f1150fb 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,8 @@ gem "rake", "0.9.2.2" gem "rspec", "2.11.0" gem "minitest", "5.8.4" gem "cucumber", "1.3.20" -gem "capybara", "~> 2.12.0" -gem "poltergeist", "1.13.0" +gem "capybara", "~> 2.0" +gem "capybara-mechanize" gem "rest-client", "1.6.7" gem "nokogiri", "1.5.5" diff --git a/Gemfile.lock b/Gemfile.lock index 524ffd6c..1590997c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,17 +1,18 @@ GEM remote: https://rubygems.org/ specs: - addressable (2.5.0) - public_suffix (~> 2.0, >= 2.0.2) + addressable (2.4.0) builder (3.2.2) - capybara (2.12.0) + capybara (2.10.1) addressable mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - cliver (0.3.2) + capybara-mechanize (1.5.0) + capybara (~> 2.4, >= 2.4.4) + mechanize (~> 2.7.0) cucumber (1.3.20) builder (>= 2.1.2) diff-lcs (>= 1.1.3) @@ -19,19 +20,30 @@ GEM multi_json (>= 1.7.5, < 2.0) multi_test (>= 0.1.2) diff-lcs (1.1.3) + domain_name (0.5.20160826) + unf (>= 0.0.5, < 1.0.0) gherkin (2.12.2) multi_json (~> 1.3) + http-cookie (1.0.3) + domain_name (~> 0.5) + mechanize (2.7.3) + domain_name (~> 0.5, >= 0.5.1) + http-cookie (~> 1.0) + mime-types (~> 2.0) + net-http-digest_auth (~> 1.1, >= 1.1.1) + net-http-persistent (~> 2.5, >= 2.5.2) + nokogiri (~> 1.4) + ntlm-http (~> 0.1, >= 0.1.1) + webrobots (>= 0.0.9, < 0.2) mime-types (2.99.3) minitest (5.8.4) multi_json (1.12.1) multi_test (0.1.2) + net-http-digest_auth (1.4) + net-http-persistent (2.9.4) nokogiri (1.5.5) + ntlm-http (0.1.1) plek (1.11.0) - poltergeist (1.13.0) - capybara (~> 2.1) - cliver (~> 0.3.1) - websocket-driver (>= 0.2.0) - public_suffix (2.0.5) rack (2.0.1) rack-test (0.6.3) rack (>= 1.0) @@ -46,9 +58,10 @@ GEM rspec-expectations (2.11.3) diff-lcs (~> 1.1.3) rspec-mocks (2.11.3) - websocket-driver (0.6.5) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.2) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.2) + webrobots (0.1.2) xpath (2.0.0) nokogiri (~> 1.3) @@ -56,12 +69,12 @@ PLATFORMS ruby DEPENDENCIES - capybara (~> 2.12.0) + capybara (~> 2.0) + capybara-mechanize cucumber (= 1.3.20) minitest (= 5.8.4) nokogiri (= 1.5.5) plek (= 1.11.0) - poltergeist (= 1.13.0) rake (= 0.9.2.2) rest-client (= 1.6.7) rspec (= 2.11.0) diff --git a/features/asset_manager.feature b/features/asset_manager.feature index 90a82a28..51e41ed8 100644 --- a/features/asset_manager.feature +++ b/features/asset_manager.feature @@ -5,13 +5,13 @@ Feature: Asset Manager Scenario: check an asset can be loaded Given I am testing "asset-manager" And I am an authenticated API client - When I request "/assets/513a0efbed915d425e000002" + When I visit "/assets/513a0efbed915d425e000002" Then I should get a 200 status code And I should see "120613_Albania_Travel_Advice_WEB_Ed2_jpeg.jpg" @normal Scenario: check an asset can be served Given I am testing "static" - When I request "/media/513a0efbed915d425e000002/120613_Albania_Travel_Advice_WEB_Ed2_jpeg.jpg" + When I visit "/media/513a0efbed915d425e000002/120613_Albania_Travel_Advice_WEB_Ed2_jpeg.jpg" Then I should get a 200 status code And I should get a content length of "212880" diff --git a/features/assets.feature b/features/assets.feature index 863ea485..a6c9c912 100644 --- a/features/assets.feature +++ b/features/assets.feature @@ -3,5 +3,5 @@ Feature: Assets @high Scenario: Assets are being served Given I am testing "assets" - When I request "/__canary__" + When I visit "/__canary__" Then I should get a 200 status code diff --git a/features/calculators.feature b/features/calculators.feature index aaf33e03..7cd714e3 100644 --- a/features/calculators.feature +++ b/features/calculators.feature @@ -4,9 +4,7 @@ Feature: Calculators app Scenario: Accessing the child benefit tax calculator Given I am testing through the full stack And I force a varnish cache miss - When I visit "/child-benefit-tax-calculator" - Then I should see "Child Benefit tax calculator" - + Then I should get a 200 status code When I visit "/child-benefit-tax-calculator/main" - Then I should see "Child Benefit tax calculator" + Then I should get a 200 status code diff --git a/features/calendars.feature b/features/calendars.feature index efd97842..cb4a1ad3 100644 --- a/features/calendars.feature +++ b/features/calendars.feature @@ -20,5 +20,4 @@ Feature: Calendars @normal Scenario: check bank holidays JSON format is consistent Given I am testing through the full stack - When I request "/bank-holidays.json" - Then JSON is returned + Then I should see a consistent JSON format for the path "/bank-holidays.json" diff --git a/features/content_api.feature b/features/content_api.feature index 7e69dd57..846a51ff 100644 --- a/features/content_api.feature +++ b/features/content_api.feature @@ -4,6 +4,6 @@ Feature: Content API Scenario: (Public) Content API availability Given I am testing through the full stack And I force a varnish cache miss - When I request "/api/vehicle-tax.json" + When I visit "/api/vehicle-tax.json" Then I should get a 200 status code And I should see "Tax your vehicle" diff --git a/features/frontend.feature b/features/frontend.feature index 927f46e6..b8d352c4 100644 --- a/features/frontend.feature +++ b/features/frontend.feature @@ -6,7 +6,7 @@ Feature: Frontend @normal Scenario: check robots.txt - When I request "/robots.txt" + When I visit "/robots.txt" Then I should get a 200 status code Then I should see "User-agent:" @@ -32,7 +32,7 @@ Feature: Frontend @normal Scenario: check homepage content type & charset - When I request "/" + When I visit "/" Then I should get a "Content-Type" header of "text/html; charset=utf-8" @normal @@ -67,7 +67,7 @@ Feature: Frontend @normal Scenario: check campaign pages load When I visit "/workplacepensions" - Then I should be at a location path of "/workplace-pensions" + Then I should get a 200 status code @normal Scenario: check browse page load, and links diff --git a/features/javascript_errors.feature b/features/javascript_errors.feature deleted file mode 100644 index 1f370b33..00000000 --- a/features/javascript_errors.feature +++ /dev/null @@ -1,5 +0,0 @@ -Feature: Smokey Javascript Error detection - - @normal - Scenario: Smokey detects JS errors - When I inject a JavaScript error on the page, Smokey raises an exception \ No newline at end of file diff --git a/features/licensing.feature b/features/licensing.feature index 73d9e468..0c8e6aec 100644 --- a/features/licensing.feature +++ b/features/licensing.feature @@ -17,7 +17,7 @@ Feature: Licensing And I am benchmarking And I am testing through the full stack And I force a varnish cache miss - When I request "/apply-for-a-licence/forms/bury/test-licence/9999-7-1,0-1" + When I visit "/apply-for-a-licence/forms/bury/test-licence/9999-7-1,0-1" Then the elapsed time should be less than 10 seconds @normal @notintegration diff --git a/features/private_frontend.feature b/features/private_frontend.feature index f5a6037f..8e2b8be4 100644 --- a/features/private_frontend.feature +++ b/features/private_frontend.feature @@ -1,7 +1,10 @@ Feature: Private Frontend + Background: + Given I am testing "private-frontend" + And I am not an authenticated user + @normal Scenario: check private frontend requires auth - Given I am testing "private-frontend" - When I visit "/" without authentication + When I try to visit "/" Then I should get a 401 status code diff --git a/features/step_definitions/ab_testing_steps.rb b/features/step_definitions/ab_testing_steps.rb index ce8f5fec..c4116602 100644 --- a/features/step_definitions/ab_testing_steps.rb +++ b/features/step_definitions/ab_testing_steps.rb @@ -16,17 +16,22 @@ end Then(/^I am assigned to a test bucket$/) do - ab_cookie = page.driver.cookies["ABTest-Example"] - @ab_cookie_value = ab_cookie.value + ab_cookies = @response.headers[:set_cookie] + .select { |h| h.start_with? "ABTest-Example=" } + assert_equal 1, ab_cookies.length, + "Expected response to set exactly one A/B test cookie" + + ab_cookie = ab_cookies[0] + @ab_cookie_value = /ABTest-Example=([^;]*);/.match(ab_cookie)[1] assert @ab_cookie_value == "A" || @ab_cookie_value == "B", "Expected A/B cookie to have value 'A' or 'B' but got '#{@ab_cookie_value}'" - refute_nil ab_cookie.expires, "A/B cookie has no expiry time" + assert ab_cookie.include?("expires="), "A/B cookie has no expiry time" end Then(/^I can see the bucket I am assigned to$/) do - bucket = ab_bucket(page.body) + bucket = ab_bucket(@response.body) assert bucket == "A" || bucket == "B", "Expected A/B bucket to be 'A' or 'B', but got '#{bucket}'" diff --git a/features/step_definitions/calendar_steps.rb b/features/step_definitions/calendar_steps.rb index d782c048..417e7f6c 100644 --- a/features/step_definitions/calendar_steps.rb +++ b/features/step_definitions/calendar_steps.rb @@ -1,5 +1,12 @@ require 'json' -Then /^JSON is returned$/ do - JSON.parse(@response.body).class.should == Hash +Then /^I should see a consistent JSON format for the path "([^"]*)"$/ do |path| + json = get_request "#{@host}#{path}", cache_bust: @bypass_varnish + json = JSON.parse(json) + assert json["england-and-wales"] + assert json["england-and-wales"]["events"] + assert json["england-and-wales"]["events"].length + result = json["england-and-wales"]["events"].first + assert result["title"] + assert result["date"] end diff --git a/features/step_definitions/cookie_steps.rb b/features/step_definitions/cookie_steps.rb index 142f0196..421e4803 100644 --- a/features/step_definitions/cookie_steps.rb +++ b/features/step_definitions/cookie_steps.rb @@ -1,6 +1,6 @@ Then /^I should receive a "([a-z0-9_]+)" cookie which is "([a-zA-Z]+)"$/ do |cookie_name, cookie_property| - header = page.response_headers['Set-Cookie'] - assert header.to_s.start_with?(cookie_name), "No cookie called #{cookie_name} is being set" + header = page.response_headers['set-cookie'] + assert header.start_with?(cookie_name), "No cookie called #{cookie_name} is being set" property_matches = header.match /; #{cookie_property}(;|$)/ assert !property_matches.nil?, "The cookie #{cookie_name} does not have property #{cookie_property}" diff --git a/features/step_definitions/draft_environment_steps.rb b/features/step_definitions/draft_environment_steps.rb index bded78e6..51501092 100644 --- a/features/step_definitions/draft_environment_steps.rb +++ b/features/step_definitions/draft_environment_steps.rb @@ -1,17 +1,17 @@ When /^I attempt to go to a case study$/ do - visit_path "government/case-studies/epic-cic" + visit "government/case-studies/epic-cic" end When /^I attempt to visit "(.*?)"$/ do |path| - visit_path path + visit path end When /^I attempt to visit a CMA case$/ do - visit_path "cma-cases/japan-tobacco-international-e-lites" + visit "cma-cases/japan-tobacco-international-e-lites" end When /^I attempt to visit a manual$/ do - visit_path "guidance/content-design" + visit "guidance/content-design" end Then /^I should be prompted to log in$/ do diff --git a/features/step_definitions/efg_steps.rb b/features/step_definitions/efg_steps.rb index beb4ea48..cfb2a9fa 100644 --- a/features/step_definitions/efg_steps.rb +++ b/features/step_definitions/efg_steps.rb @@ -1,23 +1,24 @@ Given /^I am testing in an EFG context$/ do - # TODO: remove me + # Not sure if there is a better way to do this? + page.driver.browser.agent.add_auth(efg_base_url, ENV['AUTH_USERNAME'], ENV['AUTH_PASSWORD']) end When /^I try to access the list of lenders$/ do - visit_path "#{efg_base_url}/lenders" + visit "#{efg_base_url}/lenders" end When /^I try to login as a valid EFG user$/ do assert ENV["EFG_USERNAME"] && ENV["EFG_PASSWORD"], "Please ensure that the EFG user credentials are available in the environment" # Need to do it this way to comply with CSRF protection - visit_path "#{efg_base_url}/users/sign_in" + visit "#{efg_base_url}/users/sign_in" fill_in "Username", :with => ENV["EFG_USERNAME"] fill_in "Password", :with =>ENV["EFG_PASSWORD"] click_button "Sign In" end When /^I visit the EFG home page$/ do - visit_path "#{efg_base_url}" + visit "#{efg_base_url}" end Then /^I should be on the EFG home page$/ do diff --git a/features/step_definitions/finder_frontend_steps.rb b/features/step_definitions/finder_frontend_steps.rb index 1f37d487..100c5ee3 100644 --- a/features/step_definitions/finder_frontend_steps.rb +++ b/features/step_definitions/finder_frontend_steps.rb @@ -1,3 +1,3 @@ Then /^I should see an input field to search$/ do - page.body.should have_field('keywords') + @response.body.should have_field('keywords') end diff --git a/features/step_definitions/frontend_steps.rb b/features/step_definitions/frontend_steps.rb index 3fa8372a..34704480 100644 --- a/features/step_definitions/frontend_steps.rb +++ b/features/step_definitions/frontend_steps.rb @@ -1,9 +1,9 @@ When /^I click on the section "(.*?)"$/ do |section_name| - link_href = Nokogiri::HTML.parse(page.body).at_xpath("//h3[text()='#{section_name}']/../@href") + link_href = Nokogiri::HTML.parse(@response.body).at_xpath("//h3[text()='#{section_name}']/../@href") link_href.should_not == nil step "I visit \"#{link_href.value}\"" end Then /^I should see an input field for postcode$/ do - page.body.should have_field('postcode') + @response.body.should have_field('postcode') end diff --git a/features/step_definitions/licensing.rb b/features/step_definitions/licensing.rb index ed8d91dc..0fa4aeb6 100644 --- a/features/step_definitions/licensing.rb +++ b/features/step_definitions/licensing.rb @@ -1,4 +1,4 @@ When /^I login to Licensify$/ do - visit_path "https://licensify-admin.#{ENV['GOVUK_APP_DOMAIN']}/login" + visit "https://licensify-admin.#{ENV['GOVUK_APP_DOMAIN']}/login" click_button 'Login' end diff --git a/features/step_definitions/search_steps.rb b/features/step_definitions/search_steps.rb deleted file mode 100644 index 0310df0f..00000000 --- a/features/step_definitions/search_steps.rb +++ /dev/null @@ -1,13 +0,0 @@ -When /^I search for "(.*)"$/ do |term| - visit_path "/search?q=#{term}" -end - -Then /^I should see some search results$/ do - result_links = page.all(".results-list li a") - result_links.count.should >= 1 -end - -Then /^I should see organisations in the organisation filter$/ do - organisation_options = page.all("#organisations-filter input") - organisation_options.count.should >= 10 -end diff --git a/features/step_definitions/sitemap_steps.rb b/features/step_definitions/sitemap_steps.rb index be8c5905..e69de850 100644 --- a/features/step_definitions/sitemap_steps.rb +++ b/features/step_definitions/sitemap_steps.rb @@ -1,6 +1,6 @@ When /^I get the sitemap index$/ do step "I visit \"/sitemap.xml\"" - @sitemap_doc = Nokogiri.XML(page.body) + @sitemap_doc = Nokogiri.XML(@response.body) @sitemap_links = @sitemap_doc.xpath("/xmlns:sitemapindex/xmlns:sitemap/xmlns:loc") end diff --git a/features/step_definitions/smokey_steps.rb b/features/step_definitions/smokey_steps.rb index e22ef318..97237115 100644 --- a/features/step_definitions/smokey_steps.rb +++ b/features/step_definitions/smokey_steps.rb @@ -16,15 +16,25 @@ @bypass_varnish = true end +Given /^I am not an authenticated user$/ do + @authenticated = false +end + Given /^I am an authenticated API client$/ do @authenticated_as_client = true end When /^I go to the "([^"]*)" landing page$/ do |app_name| - visit_path application_base_url(app_name) + url = application_base_url(app_name) + parsed_url = URI.parse(url) + base_host = "#{parsed_url.scheme}://#{parsed_url.host}" + + page.driver.browser.agent.add_auth(base_host, ENV['AUTH_USERNAME'], ENV['AUTH_PASSWORD']) + + visit url end -When /^I (try to )?request "(.*)"$/ do |attempt_only, path_or_url| +When /^I (try to )?visit "(.*)"$/ do |attempt_only, path_or_url| url = if path_or_url.start_with?("http") path_or_url else @@ -34,18 +44,6 @@ @response = send(request_method, url, default_request_options) end -When /^I visit "(.*)"$/ do |path_or_url| - visit_path path_or_url -end - -When /^I visit "(.*)" without authentication$/ do |path_or_url| - visit_without_auth path_or_url -end - -When /^I try to visit "(.*)"$/ do |path_or_url| - visit_path path_or_url -end - When /^I visit "(.*)" without following redirects$/ do |path| @response = single_http_request("#{@host}#{path}") end @@ -72,6 +70,10 @@ @response = get_request("#{@host}/404", default_request_options.merge(return_response_on_error: true)) end +When /^I search for "(.*)"$/ do |term| + @response = get_request("#{@host}/search?q=#{term}", default_request_options) +end + When /^I request "(.*)" from Bouncer directly$/ do |url| parsed_url = URI.parse(url) bouncer_url = "#{@host}#{parsed_url.path}" @@ -92,13 +94,14 @@ def should_see(text) Then /^I should be able to visit:$/ do |table| table.hashes.each do |row| - visit_path row['Path'] + should_visit(row['Path']) end end Then /^I should be able to search the tariff and see matching results$/ do + page.driver.browser.agent.add_auth(@host, ENV['AUTH_USERNAME'], ENV['AUTH_PASSWORD']) %w(animal mineral vegetable).each do |query| - visit_path "/trade-tariff/sections" + visit("/trade-tariff/sections") fill_in("search_t", with: query) click_button("Search") @@ -108,19 +111,15 @@ def should_see(text) end end -Then /^I should be redirected when I try to visit:$/ do |table| +Then /^I should get a (\d+) response when I try to visit:$/ do |status, table| table.hashes.each do |row| - visit_path row['Path'] - page.current_path.should_not == row['Path'] + response = try_get_request("#{@host}#{row['Path']}", default_request_options) + response.code.should == status.to_i end end Then /^I should get a (\d+) status code$/ do |status| - if @response - expect(@response.code.to_i).to eq status.to_i - else - expect(page.status_code.to_i).to eq status.to_i - end + expect(@response.code.to_i).to eq status.to_i end Then /^I should get a "(.*)" header of "(.*)"$/ do |header_name, header_value| @@ -152,12 +151,7 @@ def should_see(text) end Then /^I should be at a location path of "(.*)"$/ do |location_path| - url = "#{@host}#{location_path}" - if @response - @response['location'].should == url - else - page.current_url.should == url - end + @response['location'].should == "#{@host}#{location_path}" end When /^I click "(.*?)"$/ do |link_text| @@ -171,34 +165,42 @@ def should_see(text) end Then /^the logo should link to the homepage$/ do - logo = Nokogiri::HTML.parse(page.body).at_css('#logo') + logo = Nokogiri::HTML.parse(@response.body).at_css('#logo') logo.attributes['href'].value.should == ENV['EXPECTED_GOVUK_WEBSITE_ROOT'] end +Then /^I should see some search results$/ do + result_links = Nokogiri::HTML.parse(@response.body).css(".results-list li a") + result_links.count.should >= 1 +end + +Then /^I should see organisations in the organisation filter$/ do + organisation_options = Nokogiri::HTML.parse(@response.body).css("#organisations-filter input") + organisation_options.count.should >= 10 +end + Then /^I should see Publisher's publication index$/ do page.should have_selector("#publication-list-container") end Then /^I should be able to navigate the topic hierarchy$/ do - topics = Nokogiri::HTML.parse(page.body).css("nav.topics li a") + topics = Nokogiri::HTML.parse(@response.body).css("nav.topics li a") random_path_selection(anchor_tags: topics).each do |path| - visit_path path - - subtopics = Nokogiri::HTML.parse(page.body).css("nav.topics li a") + should_visit(path) + subtopics = Nokogiri::HTML.parse(@response.body).css("nav.topics li a") random_path_selection(anchor_tags: subtopics).each do |path| - visit_path path + should_visit(path) end end end Then /^I should be able to navigate the browse pages$/ do - categories = Nokogiri::HTML.parse(page.body).css(".browse-panes ul li a") + categories = Nokogiri::HTML.parse(@response.body).css(".browse-panes ul li a") random_path_selection(anchor_tags: categories).each do |path| - visit_path path - - subcategories = Nokogiri::HTML.parse(page.body).css(".pane-inner ul li a") + should_visit(path) + subcategories = Nokogiri::HTML.parse(@response.body).css(".pane-inner ul li a") random_path_selection(anchor_tags: subcategories).each do |path| - visit_path path + should_visit(path) end end end @@ -208,7 +210,3 @@ def random_path_selection(opts={}) anchor_tags = opts[:anchor_tags] || [] anchor_tags.map { |anchor| anchor.attributes["href"].value }.sample(size) end - -When /^I inject a JavaScript error on the page, Smokey raises an exception$/ do - expect { page.driver.execute_script('1.error') }.to raise_error -end diff --git a/features/step_definitions/travel_advice_steps.rb b/features/step_definitions/travel_advice_steps.rb index c9fb0852..c6371cb0 100644 --- a/features/step_definitions/travel_advice_steps.rb +++ b/features/step_definitions/travel_advice_steps.rb @@ -1,4 +1,4 @@ Then /^the XML ID is formed from the correct URL$/ do - first_entry_id = Nokogiri::XML.parse(page.body).at_xpath('/xmlns:feed/xmlns:entry[1]/xmlns:id') + first_entry_id = Nokogiri::XML.parse(@response.body).at_xpath('/xmlns:feed/xmlns:entry[1]/xmlns:id') first_entry_id.text.should start_with("#{ENV['EXPECTED_GOVUK_WEBSITE_ROOT']}/foreign-travel-advice/ireland") end diff --git a/features/step_definitions/whitehall_steps.rb b/features/step_definitions/whitehall_steps.rb index f24c8cb8..0dcca347 100644 --- a/features/step_definitions/whitehall_steps.rb +++ b/features/step_definitions/whitehall_steps.rb @@ -1,6 +1,7 @@ Then /^I should see the departments and policies section on the homepage$/ do - visit_path "/" - assert page.first('#departments-and-policy') + html = get_request "#{@host}/", cache_bust: @bypass_varnish + doc = Nokogiri::HTML(html) + assert doc.css('#departments-and-policy') end Then /^I should be able to view policies$/ do @@ -12,24 +13,37 @@ end Then /^I should be able to view announcements$/ do - follow_link_to_first_announcement_on_announcements_page + follow_link_to_first_announcement_on_announcements_page(2) end When /^I do a whitehall search for "([^"]*)"$/ do |term| - visit_path "/government/publications?keywords=#{uri_escape(term)}" + url = "#{@host}/government/publications?keywords=#{uri_escape(term)}" + @response = get_request(url, cache_bust: @bypass_varnish) end def follow_link_to_first_policy_on_policies_page - visit_path "/government/policies" - visit_path page.first('.document a')['href'] + html = get_request("#{@host}/government/policies", cache_bust: @bypass_varnish) + doc = Nokogiri::HTML(html) + link_to_policy = doc.at('.document a') + assert ! link_to_policy.nil?, "No policy links found" + href = link_to_policy.attributes['href'].value + get_request("#{@host}#{href}", cache_bust: @bypass_varnish) end -def follow_link_to_first_announcement_on_announcements_page - visit_path "/government/announcements?page=1" - visit_path page.first('.document-row a')['href'] +def follow_link_to_first_announcement_on_announcements_page(page=1) + html = get_request("#{@host}/government/announcements?page=#{page}", cache_bust: @bypass_varnish) + doc = Nokogiri::HTML(html) + link_to_announcement = doc.at('.document-row a') + assert ! link_to_announcement.nil?, "No announcement links found" + href = link_to_announcement.attributes['href'].value + get_request("#{@host}#{href}", cache_bust: @bypass_varnish) end def follow_link_to_first_publication_on_publications_page - visit_path "/government/publications" - visit_path page.first('.document-row a')['href'] + html = get_request("#{@host}/government/publications", cache_bust: @bypass_varnish) + doc = Nokogiri::HTML(html) + link_to_publication = doc.at('.document-row a') + assert ! link_to_publication.nil?, "No publication links found" + href = link_to_publication.attributes['href'].value + get_request("#{@host}#{href}", cache_bust: @bypass_varnish) end diff --git a/features/support/capybara_mechanize_request_tracing.rb b/features/support/capybara_mechanize_request_tracing.rb new file mode 100644 index 00000000..12e2ec41 --- /dev/null +++ b/features/support/capybara_mechanize_request_tracing.rb @@ -0,0 +1,17 @@ +require 'logger' + +def activate_capybara_mechanize_request_tracing!(logger = Logger.new(STDOUT)) + Capybara::Mechanize::Browser.instance_eval do + old_process_remote_request = instance_method(:process_remote_request) + + define_method(:process_remote_request) do |method, url, attributes, headers| + logger.info "#{method.upcase} #{url}" + old_process_remote_request.bind(self).(method, url, attributes, headers) + end + end +end + +# Uncomment this line if you want to see log output for every HTTP request +# made through mechanize: +# +# activate_capybara_mechanize_request_tracing! diff --git a/features/support/env.rb b/features/support/env.rb index 666c97cc..a248c16a 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,6 +1,6 @@ require 'nokogiri' require 'capybara/cucumber' -require 'capybara/poltergeist' +require 'capybara/mechanize/cucumber' require 'uri' require 'plek' @@ -14,5 +14,6 @@ ENV["EXPECTED_GOVUK_WEBSITE_ROOT"] = 'https://www.gov.uk' end +Capybara.app = "mechanize requires any value to be set here" +Capybara.default_driver = :mechanize Capybara.app_host = ENV["GOVUK_WEBSITE_ROOT"] -Capybara.default_driver = :poltergeist diff --git a/features/support/mechanize.rb b/features/support/mechanize.rb index e7226984..254e1daf 100644 --- a/features/support/mechanize.rb +++ b/features/support/mechanize.rb @@ -1,5 +1,8 @@ Before do - if ENV['RATE_LIMIT_TOKEN'] - page.driver.add_headers 'Rate-Limit-Token' => ENV['RATE_LIMIT_TOKEN'] - end + page.driver.browser.agent.pre_connect_hooks << proc { |agent, request| + rate_limit_token = ENV['RATE_LIMIT_TOKEN'] + if rate_limit_token + request["Rate-Limit-Token"] = rate_limit_token + end + } end diff --git a/features/support/visiting_pages.rb b/features/support/visiting_pages.rb deleted file mode 100644 index 7529cd8f..00000000 --- a/features/support/visiting_pages.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'base64' - -def visit_path(path) - if ENV['AUTH_USERNAME'] && ENV['AUTH_PASSWORD'] - page.driver.basic_authorize(ENV['AUTH_USERNAME'], ENV['AUTH_PASSWORD']) - end - - if path.match(%r[\?]) - visit "#{path}&cachebust=#{rand.to_s}" - else - visit "#{path}?cachebust=#{rand.to_s}" - end -end - -def visit_without_auth(path) - if path.match(%r[\?]) - visit "#{path}&cachebust=#{rand.to_s}" - else - visit "#{path}?cachebust=#{rand.to_s}" - end -end diff --git a/features/whitehall.feature b/features/whitehall.feature index 35089260..f13a32db 100644 --- a/features/whitehall.feature +++ b/features/whitehall.feature @@ -12,6 +12,7 @@ Feature: Whitehall Scenario: There should be no authentication for Whitehall Given I am testing through the full stack And I force a varnish cache miss + And I am not an authenticated user Then I should be able to view policies And I should be able to view announcements And I should be able to view publications @@ -59,7 +60,7 @@ Feature: Whitehall @normal Scenario: Department short URLs redirect correctly Given I am testing through the full stack - Then I should be redirected when I try to visit: + Then I should get a 301 response when I try to visit: | Path | | /ago | | /airports-commission | @@ -113,7 +114,7 @@ Feature: Whitehall @normal Scenario: Whitehall assets are served Given I am testing through the full stack - When I request "/government/uploads/system/uploads/attachment_data/file/32409/11-944-higher-education-students-at-heart-of-system.pdf" + When I visit "/government/uploads/system/uploads/attachment_data/file/32409/11-944-higher-education-students-at-heart-of-system.pdf" Then I should get a 200 status code @normal @@ -124,3 +125,13 @@ Feature: Whitehall When I visit "/government/statistics/announcements" Then I should get a 200 status code And the elapsed time should be less than 2 seconds + + # Speak to David Illsley or Neil Williams before editing this test + @high + Scenario: Banknote feature works + Given I am testing through the full stack + And I force a varnish cache miss + And I am benchmarking + When I visit "/banknote" + # Redirects are transparently followed, so the end status code is 200 + Then I should get a 200 status code diff --git a/jenkins.sh b/jenkins.sh index 754c6537..632f99f4 100755 --- a/jenkins.sh +++ b/jenkins.sh @@ -18,5 +18,5 @@ if [ ! -f .ruby-version ]; then export PATH=$(printf $PATH | awk 'BEGIN { RS=":"; ORS=":" } !/rbenv/' | sed 's/:$//') fi -bundle install --path "${HOME}/bundles/${JOB_NAME}" --deployment +bundle install --path "${HOME}/bundles/${JOB_NAME}" --deployment --quiet RESTCLIENT_LOG="log/smokey-rest-client.log" govuk_setenv default bundle exec rake $MYTASK