Javascript trigger when using poltergiest/capybara outside of rails - ruby

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

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

“including Capybara::DSL in the global scope is not recommended!” want to remove it. console warning

To resolve the error visit is not found. I have included include Capybara::DSL in one of my helper module like this:
I am using ruby 2.7.0
include Capybara::DSL
module LoginHelper
def self.login_user
visit 'https://staging.have2have.it/login'
within(".container-fluid") do
fill_in("email", with: 'shinsaurab#gmail.com', :match => :prefer_exact)
fill_in("password", with: '123', :match => :prefer_exact)
end
click_button('Log In')
end
end
spec_helper.rb
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require './spec/helpers/login_helper'
Capybara.default_driver = :selenium
RSpec.configure do |config|
config.include Capybara::DSL
config.include LoginHelper
end
Can anyone please suggest if I am doing something wrong. I have tried some suggestion but didn't work for me
I was having a similar issue tried many things but what worked for me is to remove config.include Capybara::DSL from spec_helper and include the LoginHelper in a Helpers module. In your case, they may look like this:
login_helper.rb
module Helpers
module LoginHelper
def login_user
visit 'https://staging.have2have.it/login'
within(".container-fluid") do
fill_in("email", with: 'shinsaurab#gmail.com', :match => :prefer_exact)
fill_in("password", with: '123', :match => :prefer_exact)
end
click_button('Log In')
end
end
end
And spec_helper will look like this:
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require './spec/helpers/login_helper.rb'
Capybara.default_driver = :selenium
RSpec.configure do |config|
config.include Helpers::LoginHelper
end
Thanks!
Please let me know if you have any doubts

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.

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

capybara +rspec: Post "/api/albums" not working

So I am trying to create an item using POST method. But it doesn't seem to post the item.
When I do get /api/albums seems to respond fine and my test passes.
/album_spec.rb:
describe AlbumController do
before :each do
#user =FactoryGirl.create(:person)
#album = FactoryGirl.create(:album)
end
it "creates first story on login", :js => true do
login(#user)
post "/api/albums/", :format => :api_v1,:album =>#album
response.should be_success
end
it "gets album", :js=> true do
get "api/albums/"
response.should be_success
end
end
spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require "capybara-screenshot"
require "factory_girl_rails"
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
#Capybara.defualt_wait_time = 10
if ENV['BROWSER_TEST']
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
else
Capybara.javascript_driver = :selenium
end
RSpec.configure do |config|
config.include Capybara::DSL
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
config.include RequestHelpers, :type => :request
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.infer_base_class_for_anonymous_controllers = false
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end
I found the solution for myself. In the album factory, I had to add more attributes to make sure that the #user and album.user match.
#album = FactoryGirl.create(:album,person_id=>#user.id)
And then my test case looked like this
it "creates first story on login"do
login_as #user
post "/api/albums/", :format =>":api_v1",:album =>#album.attributes
response.should be_success
a = Album.last
a.person_id.should == #user.id #verifying user_ids match end
end

Resources