Running Capybara without rack produces errors when using url parameters - ruby

Here's my setup, based on this recommendation: How to get Cucumber/Capybara/Mechanize to work against external non-rails site
It works until I add parameters to the URL. Any advice on addressing this issue?
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
#test_url = "test"
RSpec.configure do |config|
config.include Capybara::DSL
end
Capybara.configure do |config|
config.run_server = false
config.current_driver = :selenium
config.app = "fake app name"
config.app_host = "http://site.com/#{#test_url}"
end
This works fine:
describe "the page, without URL parameters" do
it "shows the regular form" do
visit "/registration.aspx"
page.should have_content "YES"
end
end
But this:
describe "the page, when not fully pre-filled" do
it "shows the regular form if only passed the attendance" do
visit "/registration.aspx?r=y"
page.should have_content "YES"
end
it "shows the regular form if only passed the attendance" do
visit "/registration.aspx?f=Jim"
page.should have_content "YES"
end
end
results in
....FF
Failures:
1) the page, when not fully pre-filled shows the regular form if only passed the attendance
Failure/Error: visit "/registration.aspx?r=y"
NoMethodError:
undefined method `call' for "fake app name":String
# ./conf_spec.rb:39:in `block (2 levels) in <top (required)>'
2) the page, when not fully pre-filled shows the regular form if only passed the attendance
Failure/Error: visit "/registration.aspx?f=Jim"
NoMethodError:
undefined method `call' for "fake app name":String
# ./conf_spec.rb:44:in `block (2 levels) in <top (required)>'

You can set only one of app and app_host. So you should remove config.app from configuration.
Also you should set default_driver instead of current_driver.
As it's shown in my answer in linked question working configuration is:
Capybara.configure do |config|
config.run_server = false
config.default_driver = :selenium
config.app_host = 'https://www.google.com' # change url
end

If you're specifying an app_host instead of using rack, you need to specify default_driver instead of current_driver. For example:
Capybara.run_server = false
Capybara.default_driver = :selenium
Capybara.app_host = 'https://www.google.com'
instead of
Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'https://www.google.com'

Assuming you have a non-Rack application and are following the instructions in the second answer which references https://groups.google.com/forum/#!msg/ruby-capybara/9UFnfrc1S-s/TfQO5uBv7gMJ, it seems to me that the hack/workaround is only valid for the simplest of use cases, namely when you're not attempting to pass in parameters to the app. I assume that either Rack or Capybara is attempting to invoke your app to pass the parameters and it's failing because you app is just a String and not a callable object.

Related

How to use capybara visit without session prefix. getting undefined method `visit' for main:Object (NoMethodError)

Writing a standalone piece of code I can use
session.visit('/forms')
but how could I use
visit('/forms')
Code:
require 'webdrivers'
require 'rspec'
require 'capybara'
require 'capybara/rspec'
Capybara.configure do |config|
config.run_server = false
config.default_driver = :chrome
config.app_host = 'https://google.com'
end
options = Selenium::WebDriver::Chrome::Options.new(args: ['window-size=1200,1200'])
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
session = Capybara::Session.new(:chrome)
session.visit('/forms') # <-- this works :)
visit('/forms') # <-- but this doesn't :(
I get
undefined method `visit' for main:Object (NoMethodError)
One option is to use include Capybara::DSL, i.e.
include Capybara::DSL
session = Capybara::Session.new(:chrome)
visit('/forms')
However this does give a warning
including Capybara::DSL in the global scope is not recommended!
not sure how to get around that yet.

What is does this error method mean: undefined method `method name' for nil:NilClass (NoMethodError)

I am fairly new to automation testing and I am writing BDD automation test scenarios in Ruby using selenium-webdriver, when running my tests, they fail at the first step. (tumblr just as an example)
What does this error message mean and how do I fix it? Any help would be much appreciated!
In my feature file:
Feature: tumblr
#s1
Scenario: Logging in to Tumblr
Given I am on the Tumblr login page
When I enter my login details
Then I should be sent to the dashboard
In my login_page.rb:
def visit
#browser.goto "#{EnvConfig.base_url}/login"
await_on_page
end
In my login_step_defs.rb:
Given /^I am on the Odicci login page$/ do
#app.tumblr_login.visit
end
When /^I enter my login details$/ do
#app.tumblr_login.login
end
Then /^I should be sent to the dashboard$/ do
#app.tumblr_dashboard.go_to_dashboard
end
Initially when I was running 'cucumber features.feature' but the step definitions could not be located so the scenarios were finishing off as 'undefined' so running 'cucumber features.feature -r step_definitions works to run the tests but they fail because of this error message:
Scenario: Logging in to Tumblr # features.feature:4
Given I am on the Tumblr login page # step_definitions/login_step_defs.rb:2
undefined method `tumblr_login' for nil:NilClass (NoMethodError)
./step_definitions/login_step_defs.rb:3:in `/^I am on the Tumblr login page$/'
features.feature:5:in `Given I am on the Tumblr login page'
#maxpleaner
if ENV['HEADLESS']
require 'headless'
require 'selenium-webdriver'
headless = Headless.new display: '100'
headless.start
end
# Set up browser
# browser = Watir::Browser.new (ENV['BROWSER'] || 'chrome').to_sym
driver = Selenium::WebDriver.for :chrome
browser_type = ENV['BROWSER'] || 'chrome'
$setup_done = false
Before do |scenario|
#browser = browser
#app = App.new #browser
unless $setup_done
$setup_done = true
# This stuff will only run before the first scenario executed. Use it to set up data etc.
end
end
After do |scenario|
end
at_exit do
browser.quit
end

How to skip SSL certificate verification with Capybara

I'm trying to test a webapp that is served over HTTPS, but I'm not able to skip certificate verification:
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "capybara"
require "capybara/dsl"
require "capybara-webkit"
Capybara.run_server = false
Capybara.register_driver :webkit do |app|
Capybara::Driver::Webkit.new(app, :ignore_ssl_errors => true)
end
Capybara.current_driver = :webkit
Capybara.app_host = "https://foo.bar.com"
module Test
class Net
include Capybara::DSL
def get_results
visit('/index.jsp')
fill_in "#UserId", :with => "sheldon"
fill_in "#Pwd", :with => "cooper"
click_button "Enter"
page.save_screenshot('screenshot.png')
end
end
end
spider = Test::Net.new
spider.get_results
I get:
net.rb:10:in `initialize': wrong number of arguments (2 for 0) (ArgumentError)
from net.rb:10:in `new'
from net.rb:10:in `block in <main>'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/session.rb:69:in `call'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/session.rb:69:in `driver'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/session.rb:197:in `visit'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
from net.rb:20:in `get_results'
from net.rb:31:in `<main>'
How can I skip it?
I believe the API has changed and that you need to do:
Capybara.register_driver :webkit do |app|
Capybara::Webkit::Driver.new(app).tap {|d| d.browser.ignore_ssl_errors }
end

capybara matchers always eval to TRUE in rspec. Why?

Something strange is going on with capybara and rspec, which I'm setting up on Ruby 1.9.3, Padrino 0.10.7, rspec 2.11.0, capybara 2.0.2.
A basic Padrino project set up haml and rspec (no custom code, yet!) other than just enough to load a "/" page (which I verified does render as expected by "puts page.content" within the specs below). Here's the simple spec. "Bogus" doesn't exist, but "Home" does...note that when I puts to console, the expected true/false are CORRECT, but for some reason, the matchers aren't seeing the true/false correctly.
The one clue I have so far lies in the 2nd spec using should have_content('Bogus') which is reporting that Proc is expected...
./spec/controllers/hello_world_spec.rb
require 'spec_helper'
require 'capybara'
require 'capybara/rspec'
describe 'The HelloWorld App', :type => :feature do
context "per documentation" do
it "has bogus content" do
visit '/'
page.has_content?('Bogus')
end
it "does not have bogus content" do
visit '/'
page.should have_content("Bogus")
end
end
context "should tests" do
it "has bogus content" do
visit '/'
page.has_content?('Bogus').should == true
end
it "does not have bogus content" do
visit '/'
page.has_content?('Bogus').should == false
end
end
context "variables" do
it "has bogus content" do
visit '/'
result = page.has_content?('Bogus')
puts result
result.should == true
end
it "has Home content (expect TRUE!)" do
visit '/'
result = page.has_content?('Home')
puts result
result.should == true
end
it "does not have bogus content" do
visit '/'
result = page.has_content?('Bogus')
puts result
result.should == false
end
end
end
spec_helper.rb
PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
def app
##
# You can handle all padrino applications using instead:
Padrino.application
# Askme.tap do |app|
# end
end
RSpec.configure do |conf|
conf.include Rack::Test::Methods
Capybara.app = app
end
Output:
11:40:57:website >> bundle exec rspec spec/app/controllers/hello_world_controller_spec.rb
WARNING: Nokogiri was built against LibXML version 2.8.0, but has dynamically loaded 2.7.8
The HelloWorld App
per documentation
has bogus content
does not have bogus content (FAILED - 1)
should tests
has bogus content
does not have bogus content
variables
false
has bogus content
true
has Home content (expect TRUE!)
false
does not have bogus content
Failures:
1) The HelloWorld App per documentation does not have bogus content
Failure/Error: page.should have_content("Bogus")
TypeError:
wrong argument type Capybara::RSpecMatchers::HaveText (expected Proc)
# ./spec/app/controllers/hello_world_controller_spec.rb:16:in `block (3 levels) in <top (required)>'
Finished in 1.66 seconds
7 examples, 1 failure
Failed examples:
rspec ./spec/app/controllers/hello_world_controller_spec.rb:14 # The HelloWorld App per documentation does not have bogus content
Turns out the culprit was having both "bacon" and "rspec" in the Gemfile. Capybara was being introduced to a project that utilized bacon for the test suite and examples being tried were rspec. Once bacon was removed from the bundled gems, the capybara specs ran per documentation.
Since the bacon scripts run more or less as-is under rspec, the project decision is to remove bacon and go with rspec for the test suite and make the minor tweaks to the bacon scripts to run all specs under rspec.

Using Capybara to test pure JavaScript application

I'm having some problems using Sinatra with Capybara.
I want to test a pure javascript application. It's just a plain index.html that is being served by Sinatra.
require "sinatra"
get "/" do
File.read("public/index.html")
end
Let's say for example that I want to test this code.
$("a.link").click(function(){
$(this).replaceWith("New String");
});
Click me!
Then the test would look something like this.
describe "requests", js: true do
it "should display a message" do
visit "/"
click_link "Click me!"
page.should have_content("New String")
end
end
The problem is that nothing happens. According to Ryan Bates screencast Firefox should start and run the test if js: true is added to the describe block.
Here is my spec_helper file.
require "rspec"
require "capybara"
require "capybara/dsl"
Capybara.javascript_driver = :selenium
require_relative "./../server"
Capybara.app = Sinatra::Application
Capybara.javascript_driver = :selenium
Capybara.default_wait_time = 10
RSpec.configure do |config|
config.mock_with :rspec
config.include Capybara
end
Here is the output when running rspec rspec/request_spec.rb.
requests
should display a message (FAILED - 1)
Failures:
1) requests should display a message
Failure/Error: page.should have_content("New String")
expected #has_content?("New String") to return true, got false
# ./spec/request_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 4.38 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/request_spec.rb:2 # requests should display a message
I created an complete example project on Github that can be found here:
https://github.com/oleander/capybara-js-fails
Anyone knows why it fails?
Here is the original answer from Jonas Nicklas.
You need to require 'capybara/rspec' and set :type => :request.
See the Capybara README section on "Using Capybara with RSpec".
/Jonas
Here is a working example on Github.

Resources