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

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

Related

How do I reference the Capybara driver for creating my pageobjects/specs.

I am attempting to use pageobjects along with with my Capybara specs but can't seem to properly reference the driver. Basically I want to be able to use the PageObjects to define the fields on the page (this is login_page.rb), but when I try to create the object in the spec, it is throwing errors with saying that the object is nil.
spec_helper.rb:
# frozen-string-literal: true
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'page-object'
# loading page object files
page_paths = File.join(Dir.pwd, 'spec', 'pages', '**', '*.rb')
puts 'foo'
Dir.glob(page_paths).each { |file| puts file}
Dir.glob(page_paths).each { |file| require file }
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.default_driver = :firefox
Capybara.app_host = *********** #redacted
Capybara.default_max_wait_time = 5
RSpec.configure do |config|
config.before(:all) do
#browser = Capybara::Selenium::Driver
end
config.before(:each) do
config.include Capybara::DSL
end
end
login_page.rb
class LoginPage
include Capybara::DSL
include PageObject
text_field(:username, id: 'username')
text_field(:password, id: 'password')
button(:login, id: 'loginButton')
def initialize(driver)
#driver = driver
end
end
login_spec.rb
require 'spec_helper'
describe 'On Login page' do
context 'using proper password' do
before(:each) do
visit('/')
end
it 'logs in as foo' do
login_page = LoginPage.new(#browser)
login_page.username = 'foo'
login_page.password = 'bar'
login_page.login
end
end
end
Assuming you're talking about the page-object gem - https://github.com/cheezy/page-object - it doesn't support Capybara, it supports watir-webdriver/watir and selenium-webdriver. Additionally Capybara::Selenium::Driver is a class not an object instance. As shown in the page-object readme you need to pass an object instance into your page objects constructor
#browser = Selenium::WebDriver.for :firefox
If you want a page object framework that supports Capybara you may want to look at something like site-prism instead.

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)

Javascript trigger when using poltergiest/capybara outside of rails

I'm using Capybara, rspec and poltergeist, outside of rails, to run some headless integrations tests. The scenario is that, there are 2 select fields. If I select a value in the first select field, the 2nd select field is populated based on the value of the first select field. If I run my spec using poltergeist in Mac OSX, the spec works. But in ubuntu, it fails, it seems that the 2nd select field is not populated. I also have js: true on my specs.
Here's my spec_helper.rb:
require 'capybara/poltergeist'
require 'capybara'
require 'capybara/rspec'
require 'pry'
require 'support/session_helper'
RSpec.configure do |config|
config.include Capybara::DSL
config.include Capybara::Poltergeist
config.include SessionHelper
Capybara.run_server = false
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.app_host = "http://vps-staging.dropmysite.com"
options = { js_errors: false }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
EDIT Adding failing spec
require 'spec_helper'
feature 'vps-staging', js: true do
background do
visit '/'
end
let(:timestamp) { Time.now.strftime('%Y-%m-%d_%H-%M-%S') }
feature 'create private server' do
background do
sign_in 'blahblah#blahblah.com'
end
it 'successfully creates server' do
find(:xpath, "//a[#href='/en/private_servers/new']").click
directory_field = all(".tagit-new")[0].find("input")
select 'Ubuntu', from: 'distribution'
select '15.04', from: 'version'
fill_in 'private_server_profile_name', with: "Auto Test #{timestamp}"
directory_field.set "/home"
click_button 'Save'
visit '/en/dashboard'
expect(page.text).to have_content "Auto Test #{timestamp}"
end
end
end
EDIT another thing I found out is that, this bug only happens on phantomjs version 1.9.8 and below. 2.0 works fine.
As Tom Walpole pointed out, updating poltergeist from 1.6.0 to 1.7.0 fixed most of my problem but I still need to tweak a bit to fully solve my problem. What I did is to create a js file to hold the polyfill and edit my spec_helper.rb and tell poltergeist to use the options, extensions and include the polyfill.js thus changing this code from
options = { js_errors: false }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
to
options = { js_errors: false, extensions: ["spec/support/polyfill.js"] }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end

First test suite failing with allure-rspec

I'm working on integrating allure into my tests but have come across a problem with the first test suite failing and returning a undefined method `find_element' for nil:NilClass error with every test in the suite. The tests run as expected when I'm not using allure and the same thing happens regardless of what suite is run.
I've had a look into it and it seems the before and after hooks in my spec helper aren't being used in the failed tests. Any ideas what could be causing this?
require 'rspec'
require 'selenium-webdriver'
require 'allure-rspec'
require 'nokogiri'
require 'uuid'
require 'pathname'
RSpec.configure do |c|
c.include AllureRSpec::Adaptor
c.expect_with :rspec do |c|
### Enable both should and expect syntax ###
c.syntax = [:should, :expect]
end
c.before(:all) do
#Start the Phantom-js driver before running any headless tests
case ENV['browser']
when 'headless'
## Run in command line before a headless test: phantomjs --webdriver=8001
system('start cmd /k phantomjs --webdriver=8001')
sleep 3
end
end
c.before(:each) do
#Find the browser to the used and set the driver to the appropriate one
case ENV['browser']
when 'chrome'
#driver = Selenium::WebDriver.for :chrome
##driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :chrome)
when 'ie'
#driver = Selenium::WebDriver.for :internet_explorer
##driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :ie)
when 'firefox'
#driver = Selenium::WebDriver.for :firefox
##driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :firefox)
when 'headless'
#driver = Selenium::WebDriver.for :remote, url: 'http://localhost:8001'
end
#Maximize the browser
#driver.manage.window.maximize
#driver.get ENV['base_url']
end
c.after(:each) do |c|
#Takes a screen shot if an exception is thrown and attaches it to the allure XML when running Rake tests
if c.exception != nil
$failure = true
c.attach_file("screenshot", File.new(#driver.save_screenshot(File.join(Dir.pwd, "test_reports/allure/#{UUID.new.generate}.png"))))
end
#driver.quit
end
end
AllureRSpec.configure do |c|
#Outputs the Allure XML
c.output_dir = "test_reports/allure"
end

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

Resources