Capybara Selenium Phantomjs Browser Initialization - phantomjs

I want to use capybara for headless browser but i want to use this driver: Selenium::WebDriver::Remote::Http::Default.new
How to use this driver for capybara? Need to know the browser initialization using that driver not poltergeist or webkit.
Here's the example for chrome initialization in capybara:
Capybara::Selenium::Driver.new(app, :browser => :chrome)

Selenium::WebDriver::Remote::Http::Default.new isn't a driver - it's an http_client that can be used by drivers - I think what you're asking for is to use an instance of Selenium::WebDriver::Remote::Bridge which can be done using
Capybara::Selenium::Driver.new(app, browser: :remote, ...)
where the ... includes other options like :http_client, :desired_capabilites, :url (url for the remote server that will control the actual browser)
The title of this questions mentions phantomjs but never mentions it in the actual question. If thats what you really want then it's
Capybara::Selenium::Driver.new(app, browser: :phantomjs, ...)
where there are similar options http_client, desired_capabilities, url, args, port

For Capybara, you can use Poltergeist driver on the top of Phantomjs. To use it you need to install it by gem install poltergeist or add this gem "poltergeist" to your Gemfile and run bundle install. Then add poltergeist option to your env.rb and change your Capybara.javascript_driver = :poltergeist. See the example below:
require 'capybara/poltergeist'
Capybara.register_driver :poltergeist do |app|
options = {
:js_errors => false ,
# :timeout => 120,
# :debug => true,
# :inspector => true,
# :window_size => [1280, 1024],
# :logger => false,
# :inspector => false,
# :visible => false,
:js => true,
:timeout => 10000,
:phantomjs_options => %w[--load-images=no]
}
Capybara::Poltergeist::Driver.new(app, options)
end
Capybara.javascript_driver = :poltergeist

Related

Poltergeist/PhantomJS not honoring client-side redirect

Most of my automated integration test suite relies on a third-party authentication system that uses a client-side redirect, over which I have no control:
if (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://example.com/?authtoken=MYTOKEN&more_stuff=etc'); }
else if (window.location.replace){
window.location.replace('https://example.com/?authtoken=MYTOKEN&more_stuff=etc');
} else {;
window.location.href ='https://example.com/?authtoken=MYTOKEN&more_stuff=etc';
}
For some reason, Poltergeist/PhantomJS is stopping here and not honoring the redirect code. Is this because it's trying to use window.location.replace or window.location.href instead of window.location directly?
Here's my Poltergeist configuration in RSpec:
Capybara.register_driver :poltergeist_tls do |app|
poltergeist_options = {
js_errors: true,
phantomjs_options: ['--ssl-protocol=TLSv1.2']
}
Capybara::Poltergeist::Driver.new(app, poltergeist_options)
end
Here's a simplified version of my spec:
it 'logs the user in', js: true do
visit login_url
fill_in 'Email', with: email
fill_in 'Password', with: password
find('input[value="LOG IN"]').click()
expect(page).to have_selector '#logout-btn'
end
I'm using Capybara 2.7.1, Poltergeist 1.10.0, and PhantomJS 2.1.1.
Edit: I know that window.location was broken in PhantomJS once upon a time but that appears to have been fixed years ago.
However, when I add this to my spec:
#hackishly get the redirect URL
matches = page.body.match(%r{window.location.href ='(https?://.*)';})
redirect_url = matches[1]
evaluate_script("window.location = '#{redirect_url}'")
evaluate_script('console.log(window.location)')
...it's still outputting the old window.location instead of the newly set redirect_url. Huh??

Why is Poltergeist not registered as a driver for Capybara when used with Guard/Konacha?

I'm having trouble using Poltergeist as the driver for Capybara in Konacha tests run with Guard.
I have the following in config/initializers/konacha.rb:
Konacha.configure do |config|
require 'capybara/poltergeist'
config.spec_dir = "spec/javascripts"
config.spec_matcher = /_spec\.|_test\./
config.driver = :poltergeist
config.stylesheets = %w(manifest_public)
end if defined?(Konacha)
My tests run successfully in Poltergeist with bundle exec rake konacha:run
However, when I use the following Guardfile:
guard :konacha, driver: :poltergeist do
watch(%r{^app/assets/javascripts/(.*)\.js(\.coffee)?$}) { |m| "#{m[1]}_spec.js" }
watch(%r{^spec/javascripts/.+_spec(\.js|\.js\.coffee)$})
end
Guard complains while starting Konacha:
14:18:05 - INFO - Starting Konacha
14:18:05 - ERROR -Capybara::DriverNotFoundError: no driver called :poltergeist was found, available drivers: :rack_test, :selenium>
I found a solution which works, but it seems a little hacky, so if anyone wants to suggest something better I'm all ears. I realized that the Konacha initializer is not being loaded until after Guard loads Konacha, so I had to replicate the registration of the Capybara::Poltergeist driver in the Guardfile:
require 'capybara/poltergeist'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {:js_errors => true})
end
guard :konacha, driver: :poltergeist do
watch(%r{^app/assets/javascripts/(.*)\.js(\.coffee)?$}) { |m| "#{m[1]}_spec.js" }
watch(%r{^spec/javascripts/.+_spec(\.js|\.js\.coffee)$})
end

StaleElementReferenceError When Search for regex Using Watir-WebDriver

I'm encountering an error when attempting to search for regexes within a page. I used the following to actually locate the regex, but I'm receiving various errors.
...
browser.script.html.include? "event49"
The errors seem to be browser (and possibly environment) dependent. I've tested back and forth between Safari, Chrome, and Firefox. Firefox seems to have intermittent issues. Chrome, almost constant. I've also tested this between my PC and Mac and it seems to be standard across the board.
I have no issues with Safari.
This error is from Firefox:
[remote server] resource://fxdriver/modules/web_element_cache.js:7204:in `fxdriver.cache.getElementAt': Element not found in the cache - perhaps the page has changed since it was looked up (Selenium::WebDriver::Error::StaleElementReferenceError)
This error is from Chrome:
/Library/Ruby/Gems/1.8/gems/selenium-webdriver-2.29.0/lib/selenium/webdriver/remote/response.rb:52:in `assert_ok': getElementTagName execution failed; (Selenium::WebDriver::Error::StaleElementReferenceError)
Element does not exist in cache
Any help is much appreciated!
UPDATE:
Please see script below:
require "rubygems"
require "watir-webdriver"
require "watir-webdriver-performance"
require "rspec"
include Watir
require 'logger'
browser = Watir::Browser.new :chrome
test_site = 'http://laughlin:driveafirestone#fcac-rebrand.laughlin.com/'
browser.goto(test_site)
year_select = browser.select_list(:id => 'universal-year')
browser.select_list(:id => 'universal-year', :disabled => 'disabled').wait_while_present
year_select.select '2010'
make_select = browser.select_list(:id => 'universal-make')
browser.select_list(:id => 'universal-make', :disabled => 'disabled').wait_while_present
make_select.select 'Volkswagen'
model_select = browser.select_list(:id => 'universal-model')
browser.select_list(:id => 'universal-model', :disabled => 'disabled').wait_while_present
model_select.select 'Jetta'
submodel_select = browser.select_list(:id => 'universal-submodel')
browser.select_list(:id => 'universal-submodel', :disabled => 'disabled').wait_while_present
submodel_select.select '2.0T TDI Sedan'
zipcode_input = browser.text_field(:id => 'universal-selectorZip')
zipcode_input.set '53202'
browser.button(:id => 'universal-submit-tires-quote').click
browser.script.html.include? "event49"
browser.close
How about you use
Watir::Wait.until(30) { browser.text.include? "event49" }

Selenium does not detect page load (Ruby)

I am currently using
- firefox 13
- selenium-server-standalone-2.23.1.jar
- selenium-client (1.2.18)
- rspec 1.2.8
Selenium stops here even if page has fully loaded
08:49:43.888 INFO - Command request: waitForPageToLoad[300000, ] on session 2718493e6d4640eea76d6cb3ab1a6fc3
require 'rubygems'
require "selenium/client"
require "selenium/rspec/spec_helper"
describe "Google Search" do
attr_reader :selenium_driver
alias :page :selenium_driver
before(:all) do
#selenium_driver = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 10
end
before(:each) do
selenium_driver.start_new_browser_session
end
# The system capture need to happen BEFORE closing the Selenium session
append_after(:each) do
#selenium_driver.close_current_browser_session
end
it "can find Selenium" do
page.open "/"
page.title.should eql("Google")
page.type "q", "Selenium seleniumhq"
page.click "btnG", :wait_for => :page
page.value("q").should eql("Selenium seleniumhq")
page.text?("seleniumhq.org").should be_true
page.title.should eql("Selenium seleniumhq - Google Search")
page.text?("seleniumhq.org").should be_true
page.element?("link=Cached").should be_true
end
end
I wonder if the google page is using AJAX which is confusing the page load event.
When I went to http://google.com I noticed that submitting the search only added #q=selenium to the url.
This could likely be confusing selenium. Maybe instead of wait for pageload you can wait for page element
Some search element xpath (for example) /html/body/div[1]/div[3]/div[3]/div[6]/div[2]/div[3]/div/div[2]/div[2]/div/div/ol/div[2]/li[1]/div (ugly I know)

Rails 3.0 / net-ldap - works in irb, but not in app

I've looked around and can't seem to find the answer to this... I'm trying to use net-ldap with rails.
in irb, I can run a quick bind and it works:
irb(main):003:0> ldap_con = Net::LDAP.new({:host => 'myhost.ad',
irb(main):004:2* :base => 'DC=myhost,DC=ad',
irb(main):005:2* :port => 389,:auth=>{:method=>:simple,:username => 'bsharpe#myhost.ad',
irb(main):006:3* :password => 'letmein' } } )
=> #<Net::LDAP:0x292089f0 #host="myhost.ad", #open_connection=nil, #encryption=nil, #auth={:password=>"letmein", :method=>:simple, :username=>"bsharpe#myhost.ad"}, #verbose=false, #port=389, #base="DC=myhost,DC=ad">
irb(main):007:0> ldap_con.bind
=> true
but if i run the app i get
no such file to load -- net/ldap
which is from my require net/ldap
I checked $LOAD_PATH and it does contain /usr/local/lib/ruby/gems/1.8/gems/net-ldap-0.1.1/lib
What am I missing here?
I had the same problem. Adding: require 'rubygems' before require 'net/ldap
For example:
#!/usr/bin/ruby -w
require 'rubygems'
require 'net/ldap'
ruby version 1.8.7 and net/ldap(0.3.1)