From c259e1b81cc107769812732e96b90a031ace238c Mon Sep 17 00:00:00 2001 From: Alec Gibson Date: Tue, 31 Jan 2017 12:09:00 +0000 Subject: [PATCH 1/2] Revert "Merge pull request #228 from alphagov/revert-capybara" This reverts commit 123c54db0df782add79ad431706c4bb3d53cc2ba, reversing changes made to e1f195ebbe855a097da256f6b5fa99554e80dae3. --- Gemfile | 2 +- Gemfile.lock | 29 ++----- features/asset_manager.feature | 4 +- features/assets.feature | 2 +- features/calculators.feature | 6 +- features/calendars.feature | 4 +- features/content_api.feature | 2 +- features/frontend.feature | 4 +- features/licensing.feature | 2 +- features/private_frontend.feature | 7 +- features/step_definitions/calendar_steps.rb | 11 +-- features/step_definitions/cookie_steps.rb | 4 +- .../draft_environment_steps.rb | 8 +- features/step_definitions/efg_steps.rb | 9 +-- .../step_definitions/finder_frontend_steps.rb | 2 +- features/step_definitions/frontend_steps.rb | 4 +- features/step_definitions/licensing.rb | 2 +- features/step_definitions/search_steps.rb | 13 ++++ features/step_definitions/sitemap_steps.rb | 2 +- features/step_definitions/smokey_steps.rb | 77 +++++++++---------- .../step_definitions/travel_advice_steps.rb | 2 +- features/step_definitions/whitehall_steps.rb | 36 +++------ .../capybara_mechanize_request_tracing.rb | 17 ---- features/support/env.rb | 5 +- features/support/mechanize.rb | 9 +-- features/support/visiting_pages.rb | 26 +++++++ features/whitehall.feature | 5 +- jenkins.sh | 4 +- 28 files changed, 133 insertions(+), 165 deletions(-) create mode 100644 features/step_definitions/search_steps.rb delete mode 100644 features/support/capybara_mechanize_request_tracing.rb create mode 100644 features/support/visiting_pages.rb diff --git a/Gemfile b/Gemfile index 1f1150fb..78b344da 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,7 @@ gem "rspec", "2.11.0" gem "minitest", "5.8.4" gem "cucumber", "1.3.20" gem "capybara", "~> 2.0" -gem "capybara-mechanize" +gem "capybara-webkit", "~> 1.1" gem "rest-client", "1.6.7" gem "nokogiri", "1.5.5" diff --git a/Gemfile.lock b/Gemfile.lock index 1590997c..9ca50496 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -10,9 +10,9 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - capybara-mechanize (1.5.0) - capybara (~> 2.4, >= 2.4.4) - mechanize (~> 2.7.0) + capybara-webkit (1.1.0) + capybara (~> 2.0, >= 2.0.2) + json cucumber (1.3.20) builder (>= 2.1.2) diff-lcs (>= 1.1.3) @@ -20,29 +20,14 @@ 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) + json (2.0.3) 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) rack (2.0.1) rack-test (0.6.3) @@ -58,10 +43,6 @@ GEM rspec-expectations (2.11.3) diff-lcs (~> 1.1.3) rspec-mocks (2.11.3) - unf (0.1.4) - unf_ext - unf_ext (0.0.7.2) - webrobots (0.1.2) xpath (2.0.0) nokogiri (~> 1.3) @@ -70,7 +51,7 @@ PLATFORMS DEPENDENCIES capybara (~> 2.0) - capybara-mechanize + capybara-webkit (~> 1.1) cucumber (= 1.3.20) minitest (= 5.8.4) nokogiri (= 1.5.5) diff --git a/features/asset_manager.feature b/features/asset_manager.feature index 51e41ed8..90a82a28 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 visit "/assets/513a0efbed915d425e000002" + When I request "/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 visit "/media/513a0efbed915d425e000002/120613_Albania_Travel_Advice_WEB_Ed2_jpeg.jpg" + When I request "/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 a6c9c912..863ea485 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 visit "/__canary__" + When I request "/__canary__" Then I should get a 200 status code diff --git a/features/calculators.feature b/features/calculators.feature index 7cd714e3..aaf33e03 100644 --- a/features/calculators.feature +++ b/features/calculators.feature @@ -4,7 +4,9 @@ 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 get a 200 status code + Then I should see "Child Benefit tax calculator" + When I visit "/child-benefit-tax-calculator/main" - Then I should get a 200 status code + Then I should see "Child Benefit tax calculator" diff --git a/features/calendars.feature b/features/calendars.feature index cb4a1ad3..d8c01335 100644 --- a/features/calendars.feature +++ b/features/calendars.feature @@ -19,5 +19,5 @@ Feature: Calendars @normal Scenario: check bank holidays JSON format is consistent - Given I am testing through the full stack - Then I should see a consistent JSON format for the path "/bank-holidays.json" + When I visit "/bank-holidays.json" + Then JSON is returned diff --git a/features/content_api.feature b/features/content_api.feature index 846a51ff..7e69dd57 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 visit "/api/vehicle-tax.json" + When I request "/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 b8d352c4..81a705d8 100644 --- a/features/frontend.feature +++ b/features/frontend.feature @@ -6,7 +6,7 @@ Feature: Frontend @normal Scenario: check robots.txt - When I visit "/robots.txt" + When I request "/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 visit "/" + When I request "/" Then I should get a "Content-Type" header of "text/html; charset=utf-8" @normal diff --git a/features/licensing.feature b/features/licensing.feature index 0c8e6aec..73d9e468 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 visit "/apply-for-a-licence/forms/bury/test-licence/9999-7-1,0-1" + When I request "/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 8e2b8be4..f5a6037f 100644 --- a/features/private_frontend.feature +++ b/features/private_frontend.feature @@ -1,10 +1,7 @@ Feature: Private Frontend - Background: - Given I am testing "private-frontend" - And I am not an authenticated user - @normal Scenario: check private frontend requires auth - When I try to visit "/" + Given I am testing "private-frontend" + When I visit "/" without authentication Then I should get a 401 status code diff --git a/features/step_definitions/calendar_steps.rb b/features/step_definitions/calendar_steps.rb index 417e7f6c..20fef703 100644 --- a/features/step_definitions/calendar_steps.rb +++ b/features/step_definitions/calendar_steps.rb @@ -1,12 +1,5 @@ require 'json' -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"] +Then /^JSON is returned$/ do + JSON.parse(page.body).class.should == Hash end diff --git a/features/step_definitions/cookie_steps.rb b/features/step_definitions/cookie_steps.rb index 421e4803..142f0196 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.start_with?(cookie_name), "No cookie called #{cookie_name} is being set" + header = page.response_headers['Set-Cookie'] + assert header.to_s.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 51501092..bded78e6 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 "government/case-studies/epic-cic" + visit_path "government/case-studies/epic-cic" end When /^I attempt to visit "(.*?)"$/ do |path| - visit path + visit_path path end When /^I attempt to visit a CMA case$/ do - visit "cma-cases/japan-tobacco-international-e-lites" + visit_path "cma-cases/japan-tobacco-international-e-lites" end When /^I attempt to visit a manual$/ do - visit "guidance/content-design" + visit_path "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 cfb2a9fa..beb4ea48 100644 --- a/features/step_definitions/efg_steps.rb +++ b/features/step_definitions/efg_steps.rb @@ -1,24 +1,23 @@ Given /^I am testing in an EFG context$/ do - # 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']) + # TODO: remove me end When /^I try to access the list of lenders$/ do - visit "#{efg_base_url}/lenders" + visit_path "#{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 "#{efg_base_url}/users/sign_in" + visit_path "#{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 "#{efg_base_url}" + visit_path "#{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 100c5ee3..1f37d487 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 - @response.body.should have_field('keywords') + page.body.should have_field('keywords') end diff --git a/features/step_definitions/frontend_steps.rb b/features/step_definitions/frontend_steps.rb index 34704480..3fa8372a 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(@response.body).at_xpath("//h3[text()='#{section_name}']/../@href") + link_href = Nokogiri::HTML.parse(page.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 - @response.body.should have_field('postcode') + page.body.should have_field('postcode') end diff --git a/features/step_definitions/licensing.rb b/features/step_definitions/licensing.rb index 0fa4aeb6..ed8d91dc 100644 --- a/features/step_definitions/licensing.rb +++ b/features/step_definitions/licensing.rb @@ -1,4 +1,4 @@ When /^I login to Licensify$/ do - visit "https://licensify-admin.#{ENV['GOVUK_APP_DOMAIN']}/login" + visit_path "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 new file mode 100644 index 00000000..0310df0f --- /dev/null +++ b/features/step_definitions/search_steps.rb @@ -0,0 +1,13 @@ +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 e69de850..be8c5905 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(@response.body) + @sitemap_doc = Nokogiri.XML(page.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 97237115..5bdbca4f 100644 --- a/features/step_definitions/smokey_steps.rb +++ b/features/step_definitions/smokey_steps.rb @@ -16,25 +16,15 @@ @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| - 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 + visit_path application_base_url(app_name) end -When /^I (try to )?visit "(.*)"$/ do |attempt_only, path_or_url| +When /^I (try to )?request "(.*)"$/ do |attempt_only, path_or_url| url = if path_or_url.start_with?("http") path_or_url else @@ -44,6 +34,18 @@ @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 @@ -70,10 +72,6 @@ @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}" @@ -94,14 +92,13 @@ def should_see(text) Then /^I should be able to visit:$/ do |table| table.hashes.each do |row| - should_visit(row['Path']) + visit_path 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("/trade-tariff/sections") + visit_path "/trade-tariff/sections" fill_in("search_t", with: query) click_button("Search") @@ -111,15 +108,19 @@ def should_see(text) end end -Then /^I should get a (\d+) response when I try to visit:$/ do |status, table| +Then /^I should be redirected when I try to visit:$/ do |table| table.hashes.each do |row| - response = try_get_request("#{@host}#{row['Path']}", default_request_options) - response.code.should == status.to_i + visit_path row['Path'] + page.current_path.should_not == row['Path'] end end Then /^I should get a (\d+) status code$/ do |status| - expect(@response.code.to_i).to eq status.to_i + 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 end Then /^I should get a "(.*)" header of "(.*)"$/ do |header_name, header_value| @@ -165,42 +166,34 @@ def should_see(text) end Then /^the logo should link to the homepage$/ do - logo = Nokogiri::HTML.parse(@response.body).at_css('#logo') + logo = Nokogiri::HTML.parse(page.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(@response.body).css("nav.topics li a") + topics = Nokogiri::HTML.parse(page.body).css("nav.topics li a") random_path_selection(anchor_tags: topics).each do |path| - should_visit(path) - subtopics = Nokogiri::HTML.parse(@response.body).css("nav.topics li a") + visit_path path + + subtopics = Nokogiri::HTML.parse(page.body).css("nav.topics li a") random_path_selection(anchor_tags: subtopics).each do |path| - should_visit(path) + visit_path path end end end Then /^I should be able to navigate the browse pages$/ do - categories = Nokogiri::HTML.parse(@response.body).css(".browse-panes ul li a") + categories = Nokogiri::HTML.parse(page.body).css(".browse-panes ul li a") random_path_selection(anchor_tags: categories).each do |path| - should_visit(path) - subcategories = Nokogiri::HTML.parse(@response.body).css(".pane-inner ul li a") + visit_path path + + subcategories = Nokogiri::HTML.parse(page.body).css(".pane-inner ul li a") random_path_selection(anchor_tags: subcategories).each do |path| - should_visit(path) + visit_path path end end end diff --git a/features/step_definitions/travel_advice_steps.rb b/features/step_definitions/travel_advice_steps.rb index c6371cb0..c9fb0852 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(@response.body).at_xpath('/xmlns:feed/xmlns:entry[1]/xmlns:id') + first_entry_id = Nokogiri::XML.parse(page.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 0dcca347..f24c8cb8 100644 --- a/features/step_definitions/whitehall_steps.rb +++ b/features/step_definitions/whitehall_steps.rb @@ -1,7 +1,6 @@ Then /^I should see the departments and policies section on the homepage$/ do - html = get_request "#{@host}/", cache_bust: @bypass_varnish - doc = Nokogiri::HTML(html) - assert doc.css('#departments-and-policy') + visit_path "/" + assert page.first('#departments-and-policy') end Then /^I should be able to view policies$/ do @@ -13,37 +12,24 @@ end Then /^I should be able to view announcements$/ do - follow_link_to_first_announcement_on_announcements_page(2) + follow_link_to_first_announcement_on_announcements_page end When /^I do a whitehall search for "([^"]*)"$/ do |term| - url = "#{@host}/government/publications?keywords=#{uri_escape(term)}" - @response = get_request(url, cache_bust: @bypass_varnish) + visit_path "/government/publications?keywords=#{uri_escape(term)}" end def follow_link_to_first_policy_on_policies_page - 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) + visit_path "/government/policies" + visit_path page.first('.document a')['href'] end -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) +def follow_link_to_first_announcement_on_announcements_page + visit_path "/government/announcements?page=1" + visit_path page.first('.document-row a')['href'] end def follow_link_to_first_publication_on_publications_page - 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) + visit_path "/government/publications" + visit_path page.first('.document-row a')['href'] end diff --git a/features/support/capybara_mechanize_request_tracing.rb b/features/support/capybara_mechanize_request_tracing.rb deleted file mode 100644 index 12e2ec41..00000000 --- a/features/support/capybara_mechanize_request_tracing.rb +++ /dev/null @@ -1,17 +0,0 @@ -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 a248c16a..0b9a6c98 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,6 +1,6 @@ require 'nokogiri' require 'capybara/cucumber' -require 'capybara/mechanize/cucumber' +require 'capybara/webkit' require 'uri' require 'plek' @@ -14,6 +14,5 @@ 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.default_driver = :webkit Capybara.app_host = ENV["GOVUK_WEBSITE_ROOT"] diff --git a/features/support/mechanize.rb b/features/support/mechanize.rb index 254e1daf..fc704421 100644 --- a/features/support/mechanize.rb +++ b/features/support/mechanize.rb @@ -1,8 +1,5 @@ Before do - 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 - } + if ENV['RATE_LIMIT_TOKEN'] + page.driver.header 'Rate-Limit-Token', ENV['RATE_LIMIT_TOKEN'] + end end diff --git a/features/support/visiting_pages.rb b/features/support/visiting_pages.rb new file mode 100644 index 00000000..3c2e4333 --- /dev/null +++ b/features/support/visiting_pages.rb @@ -0,0 +1,26 @@ +require 'base64' + +def visit_path(path) + if ENV['AUTH_USERNAME'] && ENV['AUTH_PASSWORD'] + auth_string = Base64.encode64("#{ENV['AUTH_USERNAME']}:#{ENV['AUTH_PASSWORD']}").strip + page.driver.header "Authorization", "Basic #{auth_string}" + end + + if path.match(%r[\?]) + visit "#{path}&cachebust=#{rand.to_s}" + else + visit "#{path}?cachebust=#{rand.to_s}" + end + + page.driver.error_messages.should == [] +end + +def visit_without_auth(path) + if path.match(%r[\?]) + visit "#{path}&cachebust=#{rand.to_s}" + else + visit "#{path}?cachebust=#{rand.to_s}" + end + + page.driver.error_messages.should == [] +end diff --git a/features/whitehall.feature b/features/whitehall.feature index f13a32db..2b299277 100644 --- a/features/whitehall.feature +++ b/features/whitehall.feature @@ -12,7 +12,6 @@ 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 @@ -60,7 +59,7 @@ Feature: Whitehall @normal Scenario: Department short URLs redirect correctly Given I am testing through the full stack - Then I should get a 301 response when I try to visit: + Then I should be redirected when I try to visit: | Path | | /ago | | /airports-commission | @@ -114,7 +113,7 @@ Feature: Whitehall @normal Scenario: Whitehall assets are served Given I am testing through the full stack - When I visit "/government/uploads/system/uploads/attachment_data/file/32409/11-944-higher-education-students-at-heart-of-system.pdf" + When I request "/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 diff --git a/jenkins.sh b/jenkins.sh index 632f99f4..11f9fa2a 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 --quiet -RESTCLIENT_LOG="log/smokey-rest-client.log" govuk_setenv default bundle exec rake $MYTASK +bundle install --path "${HOME}/bundles/${JOB_NAME}" --deployment +RESTCLIENT_LOG="log/smokey-rest-client.log" govuk_setenv default xvfb-run -a bundle exec rake $MYTASK From d0cc7822cd99fde7c055c852a718896b04cac577 Mon Sep 17 00:00:00 2001 From: Alec Gibson Date: Wed, 1 Feb 2017 10:57:13 +0000 Subject: [PATCH 2/2] Change driver to Poltergeist --- Gemfile | 4 ++-- Gemfile.lock | 22 ++++++++++++------- features/calendars.feature | 3 ++- features/frontend.feature | 2 +- features/javascript_errors.feature | 5 +++++ features/step_definitions/ab_testing_steps.rb | 13 ++++------- features/step_definitions/calendar_steps.rb | 2 +- features/step_definitions/smokey_steps.rb | 11 +++++++++- features/support/env.rb | 4 ++-- features/support/mechanize.rb | 2 +- features/support/visiting_pages.rb | 7 +----- features/whitehall.feature | 10 --------- jenkins.sh | 2 +- 13 files changed, 44 insertions(+), 43 deletions(-) create mode 100644 features/javascript_errors.feature diff --git a/Gemfile b/Gemfile index 78b344da..cb66f4b0 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.0" -gem "capybara-webkit", "~> 1.1" +gem "capybara", "~> 2.12.0" +gem "poltergeist", "1.13.0" gem "rest-client", "1.6.7" gem "nokogiri", "1.5.5" diff --git a/Gemfile.lock b/Gemfile.lock index 9ca50496..524ffd6c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,18 +1,17 @@ GEM remote: https://rubygems.org/ specs: - addressable (2.4.0) + addressable (2.5.0) + public_suffix (~> 2.0, >= 2.0.2) builder (3.2.2) - capybara (2.10.1) + capybara (2.12.0) addressable mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - capybara-webkit (1.1.0) - capybara (~> 2.0, >= 2.0.2) - json + cliver (0.3.2) cucumber (1.3.20) builder (>= 2.1.2) diff-lcs (>= 1.1.3) @@ -22,13 +21,17 @@ GEM diff-lcs (1.1.3) gherkin (2.12.2) multi_json (~> 1.3) - json (2.0.3) mime-types (2.99.3) minitest (5.8.4) multi_json (1.12.1) multi_test (0.1.2) nokogiri (1.5.5) 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) @@ -43,6 +46,9 @@ 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) xpath (2.0.0) nokogiri (~> 1.3) @@ -50,12 +56,12 @@ PLATFORMS ruby DEPENDENCIES - capybara (~> 2.0) - capybara-webkit (~> 1.1) + capybara (~> 2.12.0) 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/calendars.feature b/features/calendars.feature index d8c01335..efd97842 100644 --- a/features/calendars.feature +++ b/features/calendars.feature @@ -19,5 +19,6 @@ Feature: Calendars @normal Scenario: check bank holidays JSON format is consistent - When I visit "/bank-holidays.json" + Given I am testing through the full stack + When I request "/bank-holidays.json" Then JSON is returned diff --git a/features/frontend.feature b/features/frontend.feature index 81a705d8..927f46e6 100644 --- a/features/frontend.feature +++ b/features/frontend.feature @@ -67,7 +67,7 @@ Feature: Frontend @normal Scenario: check campaign pages load When I visit "/workplacepensions" - Then I should get a 200 status code + Then I should be at a location path of "/workplace-pensions" @normal Scenario: check browse page load, and links diff --git a/features/javascript_errors.feature b/features/javascript_errors.feature new file mode 100644 index 00000000..1f370b33 --- /dev/null +++ b/features/javascript_errors.feature @@ -0,0 +1,5 @@ +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/step_definitions/ab_testing_steps.rb b/features/step_definitions/ab_testing_steps.rb index c4116602..ce8f5fec 100644 --- a/features/step_definitions/ab_testing_steps.rb +++ b/features/step_definitions/ab_testing_steps.rb @@ -16,22 +16,17 @@ end Then(/^I am assigned to a test bucket$/) do - 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] + ab_cookie = page.driver.cookies["ABTest-Example"] + @ab_cookie_value = ab_cookie.value assert @ab_cookie_value == "A" || @ab_cookie_value == "B", "Expected A/B cookie to have value 'A' or 'B' but got '#{@ab_cookie_value}'" - assert ab_cookie.include?("expires="), "A/B cookie has no expiry time" + refute_nil ab_cookie.expires, "A/B cookie has no expiry time" end Then(/^I can see the bucket I am assigned to$/) do - bucket = ab_bucket(@response.body) + bucket = ab_bucket(page.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 20fef703..d782c048 100644 --- a/features/step_definitions/calendar_steps.rb +++ b/features/step_definitions/calendar_steps.rb @@ -1,5 +1,5 @@ require 'json' Then /^JSON is returned$/ do - JSON.parse(page.body).class.should == Hash + JSON.parse(@response.body).class.should == Hash end diff --git a/features/step_definitions/smokey_steps.rb b/features/step_definitions/smokey_steps.rb index 5bdbca4f..e22ef318 100644 --- a/features/step_definitions/smokey_steps.rb +++ b/features/step_definitions/smokey_steps.rb @@ -152,7 +152,12 @@ def should_see(text) end Then /^I should be at a location path of "(.*)"$/ do |location_path| - @response['location'].should == "#{@host}#{location_path}" + url = "#{@host}#{location_path}" + if @response + @response['location'].should == url + else + page.current_url.should == url + end end When /^I click "(.*?)"$/ do |link_text| @@ -203,3 +208,7 @@ 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/support/env.rb b/features/support/env.rb index 0b9a6c98..666c97cc 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,6 +1,6 @@ require 'nokogiri' require 'capybara/cucumber' -require 'capybara/webkit' +require 'capybara/poltergeist' require 'uri' require 'plek' @@ -14,5 +14,5 @@ ENV["EXPECTED_GOVUK_WEBSITE_ROOT"] = 'https://www.gov.uk' end -Capybara.default_driver = :webkit Capybara.app_host = ENV["GOVUK_WEBSITE_ROOT"] +Capybara.default_driver = :poltergeist diff --git a/features/support/mechanize.rb b/features/support/mechanize.rb index fc704421..e7226984 100644 --- a/features/support/mechanize.rb +++ b/features/support/mechanize.rb @@ -1,5 +1,5 @@ Before do if ENV['RATE_LIMIT_TOKEN'] - page.driver.header 'Rate-Limit-Token', ENV['RATE_LIMIT_TOKEN'] + page.driver.add_headers 'Rate-Limit-Token' => ENV['RATE_LIMIT_TOKEN'] end end diff --git a/features/support/visiting_pages.rb b/features/support/visiting_pages.rb index 3c2e4333..7529cd8f 100644 --- a/features/support/visiting_pages.rb +++ b/features/support/visiting_pages.rb @@ -2,8 +2,7 @@ def visit_path(path) if ENV['AUTH_USERNAME'] && ENV['AUTH_PASSWORD'] - auth_string = Base64.encode64("#{ENV['AUTH_USERNAME']}:#{ENV['AUTH_PASSWORD']}").strip - page.driver.header "Authorization", "Basic #{auth_string}" + page.driver.basic_authorize(ENV['AUTH_USERNAME'], ENV['AUTH_PASSWORD']) end if path.match(%r[\?]) @@ -11,8 +10,6 @@ def visit_path(path) else visit "#{path}?cachebust=#{rand.to_s}" end - - page.driver.error_messages.should == [] end def visit_without_auth(path) @@ -21,6 +18,4 @@ def visit_without_auth(path) else visit "#{path}?cachebust=#{rand.to_s}" end - - page.driver.error_messages.should == [] end diff --git a/features/whitehall.feature b/features/whitehall.feature index 2b299277..35089260 100644 --- a/features/whitehall.feature +++ b/features/whitehall.feature @@ -124,13 +124,3 @@ 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 11f9fa2a..754c6537 100755 --- a/jenkins.sh +++ b/jenkins.sh @@ -19,4 +19,4 @@ if [ ! -f .ruby-version ]; then fi bundle install --path "${HOME}/bundles/${JOB_NAME}" --deployment -RESTCLIENT_LOG="log/smokey-rest-client.log" govuk_setenv default xvfb-run -a bundle exec rake $MYTASK +RESTCLIENT_LOG="log/smokey-rest-client.log" govuk_setenv default bundle exec rake $MYTASK