Use capybara to make javascript testing possible

This commit is contained in:
Max Meyer 2015-06-08 04:12:00 +02:00
parent f2f82a4ff9
commit 3935a7cf5f
7 changed files with 56 additions and 14 deletions

View file

@ -20,6 +20,9 @@ gem 'sinatra', '>= 1.4', require: false
gem 'redcarpet', '>= 3.1', require: false unless RUBY_ENGINE == 'jruby'
gem 'asciidoctor', '~> 0.1', require: false
# To test javascript
gem 'poltergeist', '~> 1.6.0', require: false
# For less, note there is no compatible JS runtime for windows
gem 'therubyracer', '>= 0.12', platforms: :ruby
gem 'therubyrhino', '>= 2.0', platforms: :jruby

View file

@ -0,0 +1,18 @@
Feature: Test a site with javascript included
As a software developer
I want to develop a site using javascript
I would like to have a server step rendering javascript correctly in order to test it
@javascript
Scenario: Existing app with javascript
Given the Server is running at "javascript-app"
When I go to "/index.html"
Then I should see:
"""
Local Hour
"""
And I should see:
"""
Local Minutes
"""

View file

@ -4,6 +4,9 @@ ENV["AUTOLOAD_SPROCKETS"] = "false"
require 'simplecov'
SimpleCov.root(File.expand_path(File.dirname(__FILE__) + '/../..'))
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
require 'coveralls'
Coveralls.wear!

View file

@ -0,0 +1,17 @@
<html>
<head>
<title>
Title
</title>
</head>
<body>
<script type="text/javascript" language="JavaScript">
<!--
current_date = new Date();
document.write('Now: ');
document.write(current_date.getHours() + " Local H" + "our");
document.write(current_date.getMinutes() + " Local M" + "inutes");
//-->
</script>
</body>
</html>

View file

@ -1,6 +1,7 @@
# encoding: UTF-8
require 'rack/test'
require 'rspec/expectations'
require 'capybara/cucumber'
Given /^a clean server$/ do
@initialize_commands = []
@ -57,8 +58,7 @@ Given /^the Server is running$/ do
end
end
app_rack = @server_inst.class.to_rack_app
@browser = ::Rack::Test::Session.new(::Rack::MockSession.new(app_rack))
Capybara.app = @server_inst.class.to_rack_app
end
Given /^the Server is running at "([^\"]*)"$/ do |app_path|
@ -72,60 +72,61 @@ end
When /^I go to "([^\"]*)"$/ do |url|
in_current_dir do
@browser.get(URI.encode(url))
visit(URI.encode(url).to_s)
end
end
Then /^going to "([^\"]*)" should not raise an exception$/ do |url|
in_current_dir do
expect{ @browser.get(URI.encode(url)) }.to_not raise_exception
expect{ visit(URI.encode(url).to_s) }.to_not raise_exception
end
end
Then /^the content type should be "([^\"]*)"$/ do |expected|
in_current_dir do
expect(@browser.last_response.content_type).to start_with(expected)
expect(page.response_headers['Content-Type']).to start_with expected
end
end
Then /^I should see "([^\"]*)"$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to include(expected)
expect(page.body).to include expected
end
end
Then /^I should see '([^\']*)'$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to include(expected)
expect(page.body).to include expected
end
end
Then /^I should see:$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to include(expected)
expect(page.body).to include expected
end
end
Then /^I should not see "([^\"]*)"$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to_not include(expected)
expect(page.body).not_to include expected
end
end
Then /^I should not see:$/ do |expected|
in_current_dir do
expect(@browser.last_response.body).to_not include(expected.chomp)
expect(page.body).not_to include expected
end
end
Then /^the status code should be "([^\"]*)"$/ do |expected|
in_current_dir do
expect(@browser.last_response.status).to eq expected.to_i
expect(page.status_code).to eq expected.to_i
end
end
Then /^I should see "([^\"]*)" lines$/ do |lines|
in_current_dir do
expect(@browser.last_response.body.chomp.split($/).length).to eq(lines.to_i)
expect(page.body.chomp.split($/).length).to eq lines.to_i
end
end

View file

@ -26,7 +26,7 @@ Gem::Specification.new do |s|
s.add_dependency("hooks", ["~> 0.3"])
# Builder
s.add_dependency("rack-test", ["~> 0.6.2"])
s.add_dependency("capybara", ["~> 2.4.4"])
# CLI
s.add_dependency("thor", [">= 0.15.2", "< 2.0"])