Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate some static site pages after JavaScript has run using Capybara #15640

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ addons:
- cmake
script:
- xvfb-run bundle exec rake
- travis_wait 30 scripts/release.sh
- travis_wait 30 xvfb-run scripts/release.sh
- bash <(curl -fsSL https://github.com/everypolitician/ensure-regression-tests/raw/master/ensure-regression-tests)
sudo: false
rvm:
Expand Down
7 changes: 5 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ end

group :test do
gem 'bundler-audit'
gem 'capybara'
gem 'capybara-webkit'
gem 'html_validation'
gem 'minitest'
gem 'pry'
gem 'rack-test'
gem 'webmock'
end

group :test, :development do
gem 'capybara'
gem 'capybara-webkit'
end

group :quality do
gem 'flog'
gem 'reek'
Expand Down
17 changes: 17 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,21 @@ end
require 'bundler/audit/task'
Bundler::Audit::Task.new

require_relative 'lib/static_site/browser.rb'
require 'uri'
require 'pathname'
desc 'Build static site pages that rely on JavaScript'
task :generate_static_site_javascript_pages, [:base_url] do |_, args|
base_url = args.fetch(:base_url, 'http://localhost:4567')
pages_to_scrape = ["#{base_url}/needed.html"].map { |u| URI.parse(u) }
browser = StaticSite::Browser.new

pages_to_scrape.each do |url|
browser.visit(url)
file = Pathname.new(url.path[1..-1]).sub_ext('.html')
file.dirname.mkpath
file.write(browser.body_after_jquery_ajax)
end
end

task default: ['test', 'rubocop', 'bundle:audit']
33 changes: 33 additions & 0 deletions lib/static_site/browser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'capybara-webkit'

module StaticSite
class Browser < Capybara::Webkit::Browser
JQUERY_WAIT_TIME = 2

def initialize
super(Capybara::Webkit::Connection.new(server: Capybara::Webkit::Server.new))
allow_unknown_urls
end

def body_after_jquery_ajax
wait_for_jquery_ajax
restore_pre_js_page_classes
body
end

def wait_for_jquery_ajax
Timeout.timeout(JQUERY_WAIT_TIME) do
# Errors if jQuery isn't on the page - should we check for that?
loop until evaluate_script('jQuery.active').zero?
end
end

# Restores page classes modified by running JS on the page
def restore_pre_js_page_classes
execute_script("$('html').addClass('no-js')")
execute_script("$('html').removeClass('flexwrap')")
end
end
end
3 changes: 3 additions & 0 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ set -eo pipefail
build_viewer_static() {
bundle exec ruby app.rb &
while ! nc -z localhost 4567; do sleep 1; done
REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/../" && pwd )"
cd /tmp
wget -nv -m localhost:4567/status/all_countries.html || (echo "wget exited with non-zero exit code: $?" >&2 && exit 1)
cd localhost:4567
BUNDLE_GEMFILE=$REPO_DIR/Gemfile bundle exec rake -f $REPO_DIR/Rakefile generate_static_site_javascript_pages
}

deploy_viewer_static() {
Expand Down