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)
Related
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
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.
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
It appears I'm retracing the steps taken in the SO post: Capybara, Poltergeist and Phantomjs and giving an empty response in body. (Mark this as a duplicate if you want, but I'm including a minimal standalone test case and version numbers.)
questions
Am I doing anything obviously wrong? Is there another minimal test I can run that might help isolate the problem?
file: pgtest.rb
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
module PGTest
include Capybara::DSL
extend self
def test
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app)
end
Capybara.current_driver = :poltergeist
session = Capybara::Session.new(:poltergeist)
visit "http://www.google.com"
sleep 5
puts session.html
end
end
PGTest.test
When invoked as follows, it prints an empty page:
$ ruby pgtest.rb
<html><head></head><body></body></html>
environment
OS X 10.8.5 (12F45)
ruby 2.0.0p247 (2013-06-27) [x86_64-darwin12.4.0]
phantomjs 1.9.2
capybara (2.1.0)
poltergeist (1.4.1)
absolving phantomjs
It's worth noting that I can use phantomjs extract the html from www.google.com:
file: pjs_dump.js
var page = require('webpage').create();
page.open("http://www.google.com", function () {
var html = page.evaluate(function () {
return document.documentElement.outerHTML;
});
console.log(html);
phantom.exit();
});
When I run 'phantomjs pjs_dump.js', it prints the html from www.google.com, so phantomjs appears to be working properly.
This does the trick for me:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
require 'capybara/poltergeist'
Capybara.run_server = false
Capybara.current_driver = :poltergeist
class PGTest
include Capybara::DSL
def test
visit "http://www.google.com"
puts page.body
end
end
PGTest.new.test
I had similar issue when testing rails application with rspec, capybara and poltergeist. Spec failed when trying to find element when the body was empty.
It turned out, that the problem was in my rails code. It caused an error, the page did not render, and capybara received an empty page. And I had nothing in stack trace.
I'm trying to run a test against a remote server. i.e:
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.default_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
include Capybara
def test_google
visit('/')
end
end
question is, how do you run it?
Save
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.run_server = false
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
module MyCapybaraTest
class Test
include Capybara::DSL
def test_google
visit('/')
end
end
end
t = MyCapybaraTest::Test.new
t.test_google
to test.rb and simply: ruby test.rb
I found this standalone cucumber thingy using the selenium driver a few days ago and got it up and running in a few minutes:
https://github.com/thuss/standalone-cucumber
I had to make a few mods:
My Gemfile is
source "http://rubygems.org"
group(:test) do
gem 'cucumber'
gem 'capybara'
gem 'rspec'
gem 'selenium-webdriver', '2.5.0'
end
And this is my env.rb
begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end
require 'capybara'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'selenium-webdriver'
Capybara.default_driver = :selenium
Capybara.app_host = 'http://something'
World(Capybara)