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

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.

Related

Capybara using Selenium webdriver undefined method `visit' for #<RSpec::ExampleGroups

The following is my setup
just three files to start with. no folder structure
Gemfile
gem 'capybara'
gem 'selenium-webdriver'
spec_helper.rb
require 'capybara/rspec'
require "selenium/webdriver"
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w(headless disable-gpu) }
)
Capybara::Selenium::Driver.new app,
browser: :chrome,
desired_capabilities: capabilities
end
Capybara.javascript_driver = :headless_chrome
run.rb
require_relative 'spec_helper'
describe "test process" do
it "checks google" do
visit("www.google.com")
puts "LAUNCHED"
end
end
New to testing. Any help would be appreciated.
I ran it using
rspec run.rb
By default Capybaras methods are only included in RSpec tests of type :feature and :system - https://github.com/teamcapybara/capybara/blob/master/lib/capybara/rspec.rb#L10
Tag your test with the correct type and the methods will be available
describe 'test proces', type: :feature do

uninitialized constant Selenium::WebDriver::Chrome::Options (NameError)

Needed to execute the below set of code. Please refer my code:
Capybara.register_driver :logging_selenium_chrome do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs:
{browser: 'ALL'})
browser_options = ::Selenium::WebDriver::Chrome::Options.new()
Capybara::Selenium::Driver.new(app, browser: :chrome, options:
browser_options, desired_capabilities: caps)
end
but keeps on getting
uninitialized constant Selenium::WebDriver::Chrome::Options (NameError)
Anyone having any idea what might be causing this!!!
::Selenium::WebDriver::Chrome::Options was added in selenium-webdriver 3.4.1 - Upgrade to the latest selenium-webdriver gem (3.5.2 currently)
It seems that you need to Capybara for declaring browser_options. see the below:
Capybara.register_driver :logging_selenium_chrome do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs:
{browser: 'ALL'})
browser_options = Capybara::Selenium::WebDriver::Chrome::Options.new()
Capybara::Selenium::Driver.new(app, browser: :chrome, options:
browser_options, desired_capabilities: caps)
end

Launching IE and Chrome browser using Capybara, Selenium, Ruby

I am new to Cucumber and Capybara. I am trying to launch IE and Chrome browser.
I have downloaded drivers of both and copied them to bin folder of Ruby in C drive.
I have set Path in Env var.
Below is my support/env.rb file code
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require 'selenium-webdriver'
require 'capybara/cucumber'
Capybara.run_server = false
#Set default driver as Selenium
Capybara.default_driver = :selenium
#Set default selector as css
Capybara.default_selector = :css
#Syncronization related settings
module Helpers
def without_resynchronize
page.driver.options[:resynchronize] = false
yield
page.driver.options[:resynchronize] = true
end
end
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => chrome) #Getting error at this line
end
World(Capybara::DSL, Helpers)
I am getting compilation Error at line with comment is as below.
method calls where the number of arguments passed to the method does not match the number of method parameters.
Here is my environment:
cucumber (2.4.0)
selenium-webdriver (3.0.3)
capybara (2.11.0)
rspec (3.5.0)
Ruby 2.3
Capybara::Selenium::Driver.new(app, :browser => chrome) try changing it to Capybara::Selenium::Driver.new(app, :browser => :chrome)

initialize': rack-test requires a rack application, but none was given (ArgumentError)

I keep getting this error while switching from Selenium over to PhantomJs/Poltergeist.
Anybody know what I'm doing wrong? If I switch out the driver to selenium, the script works perfectly. Whenever I comment out the default_driver = :selenium and replace with javascript_driver = :poltergeist I run into this error.
initialize': rack-test requires a rack application, but none was given (ArgumentError)
This is all in a ruby file, no rails.
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require "open-uri"
# require "date"
# require 'active_support/core_ext/integer/inflections'
require 'capybara/poltergeist'
# require 'selenium-webdriver'
require 'pry'
require 'phantomjs'
# require 'database_cleaner'
Capybara.run_server = false
Capybara.javascript_driver = :poltergeist
# Capybara.default_driver = :selenium
Capybara.app_host = 'https://www.sameplsite.com'
module MyCapybaraTest
class Test
include Capybara::DSL
def login_site
visit('https://www.sameplsite.com')
# binding.pry
click_link('Log in')
fill_in('email', :with => 'joefrank#sharklasers.com')
fill_in('password', :with => 'passwordpassword')
check('checkbox_remember')
click_button('Log in')
end
def click_right_game
click_link('Create Contest')
all('.boxed')[1].click
check('Free practice')
click_link('Create 1 Head-to-Head')
save_and_open_page
end
def output_game_link
url = URI.parse(current_url)
puts url
end
end
end
t = MyCapybaraTest::Test.new
t.login_fanduel
t.click_right_game
t.output_game_link
Capybara.javascript_driver = :poltergeist doesn't switch the driver. If you want to switch driver , use Capybara.current_driver instead.
That says : Capybara.current_driver = :poltergeist

Running Capybara without rack produces errors when using url parameters

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.

Resources