How to skip SSL certificate verification with Capybara - ruby

I'm trying to test a webapp that is served over HTTPS, but I'm not able to skip certificate verification:
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "capybara"
require "capybara/dsl"
require "capybara-webkit"
Capybara.run_server = false
Capybara.register_driver :webkit do |app|
Capybara::Driver::Webkit.new(app, :ignore_ssl_errors => true)
end
Capybara.current_driver = :webkit
Capybara.app_host = "https://foo.bar.com"
module Test
class Net
include Capybara::DSL
def get_results
visit('/index.jsp')
fill_in "#UserId", :with => "sheldon"
fill_in "#Pwd", :with => "cooper"
click_button "Enter"
page.save_screenshot('screenshot.png')
end
end
end
spider = Test::Net.new
spider.get_results
I get:
net.rb:10:in `initialize': wrong number of arguments (2 for 0) (ArgumentError)
from net.rb:10:in `new'
from net.rb:10:in `block in <main>'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/session.rb:69:in `call'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/session.rb:69:in `driver'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/session.rb:197:in `visit'
from /usr/local/lib/ruby/gems/2.0.0/gems/capybara-2.2.0/lib/capybara/dsl.rb:51:in `block (2 levels) in <module:DSL>'
from net.rb:20:in `get_results'
from net.rb:31:in `<main>'
How can I skip it?

I believe the API has changed and that you need to do:
Capybara.register_driver :webkit do |app|
Capybara::Webkit::Driver.new(app).tap {|d| d.browser.ignore_ssl_errors }
end

Related

Ruby: Selenium and WebMock causing TypeError

The combination of WebMock and Selenium is causing a Type Error on the first call to visit
Failure/Error: id['ELEMENT'] || id['element-6066-11e4-a52e-4f735466cecf']
TypeError:
no implicit conversion of String into Integer
# /selenium-webdriver-3.4.0/lib/selenium/webdriver/common/bridge_helper.rb:43:in `[]'
# /selenium-webdriver-3.4.0/lib/selenium/webdriver/common/bridge_helper.rb:43:in `element_id_from'
# /selenium-webdriver-3.4.0/lib/selenium/webdriver/remote/bridge.rb:638:in `block in find_elements_by'
# /selenium-webdriver-3.4.0/lib/selenium/webdriver/remote/bridge.rb:638:in `each'
# /selenium-webdriver-3.4.0/lib/selenium/webdriver/remote/bridge.rb:638:in `map'
# /selenium-webdriver-3.4.0/lib/selenium/webdriver/remote/bridge.rb:638:in `find_elements_by'
# /selenium-webdriver-3.4.0/lib/selenium/webdriver/common/search_context.rb:79:in `find_elements'
# /capybara-2.14.0/lib/capybara/selenium/driver.rb:86:in `find_xpath'
# /capybara-2.14.0/lib/capybara/node/base.rb:108:in `find_xpath'
# /capybara-2.14.0/lib/capybara/queries/selector_query.rb:135:in `block in resolve_for'
# /capybara-2.14.0/lib/capybara/node/base.rb:81:in `synchronize'
# /capybara-2.14.0/lib/capybara/queries/selector_query.rb:131:in `resolve_for'
# /capybara-2.14.0/lib/capybara/node/finders.rb:40:in `block in find'
# /capybara-2.14.0/lib/capybara/node/base.rb:85:in `synchronize'
# /capybara-2.14.0/lib/capybara/node/finders.rb:38:in `find'
# /capybara-2.14.0/lib/capybara/node/actions.rb:92:in `fill_in'
# /capybara-2.14.0/lib/capybara/session.rb:784:in `block (2 levels) in <class:Session>'
# /capybara-angular-0.2.5/lib/capybara/angular/dsl.rb:8:in `block (2 levels) in <module:DSL>'
# ./spec/features/assesment_picker_spec.rb:34:in `block (3 levels) in <top (required)>'
However, when I remove all of the WebMock code my tests execute. I've allowed localhost in my WebMock exceptions, however my tests still fail to execute. I'm not sure what else I need to do to allow Selenium to run while using WebMock. Additionally, if the Selenium specs are the first to run, they pass.
Capybara Setup
Capybara.configure do |config|
config.run_server = false
config.app_host = root_url #retrieved elsewhere
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
config.save_path = File.join Dir.pwd, 'tmp', 'capybara'
end
Capybara.default_driver = :chrome
Capybara.current_driver = Capybara.default_driver
Capybara.javascript_driver = Capybara.default_driver
Spec Helper
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl_rails'
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true, allow: [/browserstack.com/])
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Hash returned from find_elements in remote/bridge#find_elements_by
$ how
"css selector"
$ what
"a"
$ execute :find_elements, {}, {using: how, value: what}
{
"acceptSslCerts" => true,
"applicationCacheEnabled" => false,
"browserConnectionEnabled" => false,
"browserName" => "chrome",
"chrome" => {
"chromedriverVersion" => "2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b)",
"userDataDir" => "/var/folders/2h/ws89fb2s381c9_3fzy6f9gzm0000gn/T/.org.chromium.Chromium.RPWXqC"
},
"cssSelectorsEnabled" => true,
"databaseEnabled" => false,
"handlesAlerts" => true,
"hasTouchScreen" => false,
"javascriptEnabled" => true,
"locationContextEnabled" => true,
"mobileEmulationEnabled" => false,
"nativeEvents" => true,
"networkConnectionEnabled" => false,
"pageLoadStrategy" => "normal",
"platform" => "Mac OS X",
"rotatable" => false,
"takesHeapSnapshot" => true,
"takesScreenshot" => true,
"unexpectedAlertBehaviour" => "",
"version" => "57.0.2987.133",
"webStorageEnabled" => true
}
Tools
$ chromedriver -v
ChromeDriver 2.29.461585 (0be2cd95f834e9ee7c46bcc7cf405b483f5ae83b)
$ bundle list selenium-webdriver
/Users/aaron/.rvm/gems/ruby-2.3.4/gems/selenium-webdriver-3.4.0
Most likely the selenium-webdriver you're using isn't compatible with the version of chromedriver you're using. Update to the latest selenium-webdriver (3.4.0 as I'm typing this), and chromedriver (2.29). You may also need to instruct WebMock to allow connections on 127.0.0.1 for the communications between selenium and chromedriver, depending on exactly how you have it configured.
I was importing the curb module for Browserstack and SauceLabs always, not just if I was running a remote driver.

Errors are shown when I am trying to run mobile automation TC using Appium + Capybara + Rspec + Ruby

I am trying to adjust environment for mobile TC running using Appium + Capybara + Rspec + Ruby. I've created two files:
1) The first file is capybara_spec.rb:
require 'rspec'
require 'capybara/rspec'
require 'appium_capybara'
require 'site_prism'
require 'rspec/expectations'
require 'capybara-screenshot/rspec'
require 'launchy'
require '../specs/capybara_test_spec'
RSpec.configure do |config|
# config.include CapybaraTest
config.include RSpec::Matchers
config.include Capybara::DSL
Capybara.default_driver = :appium
Capybara.register_driver(:appium) do |app|
desired_caps = {
caps: {
platformName: 'Android',
versionNumber: '6.0.1',
deviceName: 'Galaxy S6',
device: 'Android',
app: '../app-mockBLE_SK.apk'
# browserName: 'Chrome'
}
}
Appium::Capybara::Driver.new(app, desired_caps)
end
Capybara.save_path = "../Reports" # path where screenshots are saved
config.after(:each) do |result|
CapybaraScreenshot.save_and_open_page_path if result.exception
Capybara.current_session.driver.browser.remove_app 'com.medtronicndt.envisionpro.mock'
Capybara.current_session.driver.browser.quit
end
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
"screenshot_#{example.description.gsub(' ', '-').gsub(/^.*\/spec\//, '')}"
end
end
Capybara.default_max_wait_time = 15
2) the second file with TC capybara_test_spec.rb:
require_relative '../specs/capybara_spec'
describe 'First ' do
context 'Mobile ' do
it 'Test:' do
a = Capybara.current_session.driver.browser.app_installed?('com.medtronicndt.envisionpro.mock')
puts "App Status: " + a.to_s
expect(a).to be true
end
end
end
After running I see the following errors:
1) First Mobile Test:
Failure/Error: if e = error()
Selenium::WebDriver::Error::NoSuchDriverError:
2.1) Failure/Error: a = Capybara.current_session.driver.browser.app_installed?('com.medtronicndt.envisionpro.mock')
Selenium::WebDriver::Error::NoSuchDriverError:
# ./capybara_test_spec.rb:6:in `block (3 levels) in <top (required)>'
2.2) Failure/Error: CapybaraScreenshot.save_and_open_page_path if result.exception
NoMethodError:
undefined method `save_and_open_page_path' for CapybaraScreenshot:Class
# ./capybara_spec.rb:34:in `block (2 levels) in <top (required)>'
2.3) Failure/Error: if e = error()
Selenium::WebDriver::Error::NoSuchDriverError:
Why these error are displayed? I see that app is started, also expect in TC returns true. But report is not created and TC is failed.

Ruby not breaking out of while loop

I'm trying to get this to loop while a specific element exists on the page. The code runs and grabs the urls I want, however, when the next button is no longer on the page it wont break out of the loop and throws the following error.
/Users/someone/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rspec-expectations-3.2.0/lib/rspec/matchers.rb:926:in `method_missing': undefined method `each' for nil:NilClass (NoMethodError)
from /something/something/something.rb:30:in `block in <top (required)>'
from /something/something/something.rb:28:in `open'
from /something/something/something.rb:29:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'</code>
Brand new to Ruby, so please be gentle ;)
require 'capybara/poltergeist'
require 'capybara/dsl'
require 'csv'
require 'rspec'
include RSpec::Matchers
include Capybara::DSL
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app,timeout: 60, :phantomjs_options => ['--debug=no', '--load-images=yes', '--ignore-ssl-errors=yes', '--ssl-protocol=TLSv1'], :debug => false)
end
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.default_wait_time = 20
Capybara.ignore_hidden_elements = true
Capybara.current_session.driver.resize(1200, 1000)
visit('site.com')
while page.find(:xpath, 'html/body/div[4]/div[6]/div[1]/div[2]/div[2]/div[1]/div[2]/button[1]') do
page.find(:xpath, 'html/body/div[4]/div[6]/div[1]/div[2]/div[2]/div[1]/div[2]/button[1]').click
urls = page.all('.author-name>a').map { |a| a['href'] }.uniq
puts urls
end
puts urls
f = File.open("profiles.txt", "a") do |f|
urls.each { |element| f.puts(element) }
end

`block in non_options': file not found: (ArgumentError)

I'm trying to open browser url based on argument passed to script. Hence I wrote following ruby code:
require 'selenium-webdriver'
require 'test/unit'
class TestTitle < Test::Unit::TestCase
def setup
$driver = Selenium::WebDriver.for :firefox
if ARGV[0] == 'google'
$driver.get 'http://www.google.com'
elsif ARGV[0] == 'twitter'
$driver.get 'http://www.twitter.com'
end
end
def test_title
puts $driver.title
end
def teardown
$driver.quit
end
end
When I passed argument: ruby test.rb 'google', it results into following error:
c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:167:in `block in non_options': file not found: google (ArgumentError)
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `map!'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:146:in `non_options'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:207:in `non_options'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:52:in `process_args'
from c:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:891:in `_run'
from c:/Ruby193/lib/ruby/1.9.1/minitest/unit.rb:884:in `run'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:21:in `run'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:326:in `block (2 levels) in autorun'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:27:in `run_once'
from c:/Ruby193/lib/ruby/1.9.1/test/unit.rb:325:in `block in autorun'
Please help me understand what I'm doing wrong.
It appears that test-unit (as of 1.9.1) grabs command line options in its GlobOptions module. You are using ARGV[0] to pass browser name, but it thinks you're passing a file name. A workaround is to capture the value of ARGV[0] and then clear it before your test case runs:
browser = ARGV[0]
ARGV[0] = nil

How do I use selenium with Ruby?

I made some tests with the Firefox Selenium and then had it exported to Ruby. Although the tests all ran fine in Firefox, I am having trouble running the same suite in Ruby.
I tried to run one of the example programs they have and I also get the same connection refused error. Here is the error I got when trying to run their google_test suite.
tellingsen$ ruby google_test.rb
Loaded suite google_test
Started
E
Finished in 0.001558 seconds.
1) Error:
test_page_search(ExampleTest):
Errno::ECONNREFUSED: Connection refused - connect(2)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `initialize'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `open'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `connect'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:560:in `connect'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:553:in `do_start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:542:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1035:in `request'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:845:in `post'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:89:in `http_post'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:12:in `remote_control_command'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:11:in `remote_control_command'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/protocol.rb:19:in `string_command'
/Users/tellingsen/.gem/ruby/1.8/gems/selenium-client-1.2.18/lib/selenium/client/base.rb:85:in `start_new_browser_session'
google_test.rb:21:in `setup'
1 tests, 0 assertions, 0 failures, 1 errors
Can someone help me with this?
Note:
Mac OS: 10.6.4
Macbook Pro
Ruby: 1.8.7
gem: selenium-client 1.2.18
EDIT
Here is the google_test.rb that I tried
#!/usr/bin/env ruby
#
# Sample Test:Unit based test case using the selenium-client API
#
require "test/unit"
require "rubygems"
gem "selenium-client", ">=1.2.18"
require "selenium/client"
class ExampleTest < Test::Unit::TestCase
attr_reader :browser
def setup
#browser = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
browser.start_new_browser_session
end
def teardown
browser.close_current_browser_session
end
def test_page_search
browser.open "/"
assert_equal "Google", browser.title
browser.type "q", "Selenium seleniumhq"
browser.click "btnG", :wait_for => :page
assert_equal "Selenium seleniumhq - Google Search", browser.title
assert_equal "Selenium seleniumhq", browser.field("q")
assert browser.text?("seleniumhq.org")
assert browser.element?("link=Cached")
end
end
I figured it out after a few hours of searching on forums and through google.
What I needed to do was have the selenium server running for it to work. I was able to download it from this site http://seleniumhq.org/download/ (current: Selenium RC February 23, 2010 1.0.3).
From there I opened up a new terminal and did
cd Downloads/selenium-remote-control-1.0.3/selenium-server-1.0.3
java -jar selenium-server.jar
Then ran my ruby generated script with another terminal window
ruby google_test.rb
And it worked!
This is Selenium Webdriver example for simple google search
Save as google_search.rb
require "selenium-webdriver"
require "test/unit"
class GoogleSearch < Test::Unit::TestCase
def setup
#driver = Selenium::WebDriver.for :firefox
#base_url = "http://www.google.com/"
#driver.manage.timeouts.implicit_wait = 30
#verification_errors = []
end
def teardown
#driver.quit
assert_equal [], #verification_errors
end
def test_google_search
#driver.get(#base_url)
#driver.find_element(:name, "q").clear
#driver.find_element(:name, "q").send_keys "Thiyagarajan Veluchamy"
#driver.find_element(:name, "btnK").click
end
def element_present?(how, what)
#driver.find_element(how, what)
true
rescue Selenium::WebDriver::Error::NoSuchElementError
false
end
def verify(&blk)
yield
rescue Test::Unit::AssertionFailedError => ex
#verification_errors << ex
end
end
$ruby google_search.rb
Here is a much simpler version of the script:
require "selenium-webdriver"
#driver = Selenium::WebDriver.for :chrome
#base_url = "http://www.google.com/"
#driver.get(#base_url)
#driver.find_element(:name, "q").send_keys "Stack Overflow"
Methods available on the #driver object can be found here: http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html
find_element gives you access to the Element class. Methods available on the Element class can be found here:
http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Element.html

Resources