Access denied while started browsermob proxy server - ruby

I'm trying to use browsermob proxy in my Selenium tests. Hence I wrote following code:
require 'selenium-webdriver'
require 'browsermob/proxy'
server = BrowserMob::Proxy::Server.new ".//browsermob-proxy-2.0-beta-8//"
server.start
But when I run above script, it results into Access is denied. (5) (ChildProcess::LaunchError) error:
c:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.3.6/lib/childprocess/windows/process_builder.rb:87:in `create_process': Access is denied. (5) (ChildProcess::LaunchError)
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.3.6/lib/childprocess/windows/process_builder.rb:34:in `start'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.3.6/lib/childprocess/windows/process.rb:63:in `launch_process'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.3.6/lib/childprocess/abstract_process.rb:67:in `start'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/browsermob-proxy-0.1.3/lib/browsermob/proxy/server.rb:27:in `start'
from test.rb:6:in `<main>'
Please help me to get rid of the error.

I faced with the same issue and it was hard to figure out from the documentation here to understand that path to browsermob-proxy server should also include executable.
In your case the code should be the next:
require 'selenium-webdriver'
require 'browsermob/proxy'
server = BrowserMob::Proxy::Server.new ".//browsermob-proxy-2.0-beta-8//bin//browsermob-proxy.bat"
server.start
Hope it helps

Related

Running Cucumber/Watir-Webdriver on Jenkins using Xvfb (headless gem)

Trying to run Cucumber/Watir-Webdriver on Jenkins using Xvfb (headless gem)
This is my env.rb:
require 'rubygems'
require 'cucumber'
require 'json'
require 'watir-webdriver'
require 'page-object'
require 'page-object/page_factory'
require 'allure-cucumber'
require 'rspec'
require 'data_magic'
require 'fig_newton'
require 'yaml'
require 'headless'
ENVT = FigNewton.load('staging.yml')
DATA = DataMagic.load('data.yml')
KBA = YAML.load_file('config/data/kba.yml')
headless = Headless.new
headless.start
browser = Watir::Browser.new :firefox
Before do
#browser = browser
end
at_exit do
#browser.close
headless.destroy
end
World(PageObject::PageFactory)
This is the error I'm getting:
end of file reached (EOFError)
/var/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/2.1.0/net/protocol.rb:153:in `read_nonblock'
/var/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/2.1.0/net/protocol.rb:153:in `rbuf_fill'
/var/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/2.1.0/net/protocol.rb:134:in `readuntil'
/var/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/2.1.0/net/protocol.rb:144:in `readline'
/var/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/2.1.0/net/http/response.rb:39:in `read_status_line'
/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.45.0.dev3/lib/selenium/webdriver/remote/bridge.rb:640:in `raw_execute'
/var/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.45.0.dev3/lib/selenium/webdriver/remote/bridge.rb:618:in `execute'
/var/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.45.0.dev3/lib/selenium/webdriver/remote/bridge.rb:112:in `get'
/var/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.45.0.dev3/lib/selenium/webdriver/common/navigation.rb:14:in `to'
/var/lib/jenkins/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/watir-webdriver-0.6.11/lib/watir-webdriver/browser.rb:77:in `goto'
/var/lib/jenkins/jobs/idme_revelator/workspace/features/support/hooks.rb:32:in `Before'
* I resize the browser # features/step_definitions/misc_step_defs.rb:1
Connection refused - connect(2) for "127.0.0.1" port 7055 (Errno::ECONNREFUSED)
./features/step_definitions/misc_step_defs.rb:2:in `/^I resize the browser$/'
How do I need to setup my env file to correctly run Cucumber/Watir-Webdriver using the headless gem (Xvfb)?
In some other attempts, errors have been related to undefined method 'goto', etc. In my tests other places like hooks.rb I use the variable #browser or #browser.driver, so I need those to work as well.
I would take a look at the answer to this question as it your error is the same save for the different port number
Opening several threads with watir-webdriver results in 'Connection refused' error
It looks like you maybe have multiple browsers trying to access the same port at the same time. Adding a sleep might fix it, but is not a best-practice solution. I would more clearly define your port numbers that each browser connects to.

Why does Selenium Web Driver say Service Unavailable?

When I run the code below:
require "selenium-webdriver"
require 'rubygems'
require 'watir-webdriver'
b = Watir::Browser.new :phantomjs
b.goto 'http://www.google.com'
puts b.title
b.close
the following error is displayed:
/home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/http/common.rb:66:in `create_response': unexpected response, code=503, content-type="text/html" (Selenium::WebDriver::Error::WebDriverError)
<HTML><TITLE>503 Service Unavailable</TITLE>
<H1>503 Service Unavailable</H1>
Failed to connect to server <B>127.0.0.1</B></HTML>
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/bridge.rb:634:in `raw_execute'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/bridge.rb:99:in `create_session'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/remote/bridge.rb:68:in `initialize'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/phantomjs/bridge.rb:32:in `initialize'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/common/driver.rb:45:in `new'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver/common/driver.rb:45:in `for'
from /home/jotsarup/.gem/gems/selenium-webdriver-2.41.0/lib/selenium/webdriver.rb:67:in `for'
from /home/jotsarup/.gem/gems/watir-webdriver-0.6.8/lib/watir-webdriver/browser.rb:46:in `initialize'
from test_phantom.rb:7:in `new'
from test_phantom.rb:7:in `<main>'
phantomjs is not connected. I also tried Firefox and the results are the same.
It looks like you are failing to reach outside of your local machine based on "Failed to connect to server 127.0.0.1" 127.0.0.1 is your loopback address (for your machine) and I have seen this issue arise in the past when there is a firewall up. If you are in a company that requires traffic to be routed through the firewall I would recommend seeing if they see any traffic trying to make it out from your machine. If you're not in a company requiring a firewall then I would recommend dropping the firewall/proxy for testing.
Looks like you are behind the PROXY. Add the following snippet before starting the server:
ENV['HTTP_PROXY'] = ENV['http_proxy'] = nil
b = Watir::Browser.new :phantomjs

Ruby / Sinatra - Route any HTTP request to HTTPS (using Rack::SSL) - !! Invalid request

I'm playing with Ruby / Sinatra at present and attempting to get HTTPS working.
I've taken a look at the rack:ssl gem here: https://github.com/josh/rack-ssl
It seems to be working when I run the application (as in redirecting to HTTPS), but nothing is displayed in the browser and the log comes up with the following error:
!! Invalid request
#!/usr/bin/env ruby
require 'rack/ssl'
require 'sinatra'
use Rack::SSL
get '/' do
'Hello World'
end
I'm not sure what to do from here.
Update:
Turned Thin Logging on in the sinatra app and got the following in the log:
!! Invalid request
Invalid HTTP format, parsing fails.
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/thin-1.5.1/lib/thin/request.rb:82:in `execute'
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/thin-1.5.1/lib/thin/request.rb:82:in `parse'
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/thin-1.5.1/lib/thin/connection.rb:39:in `receive_data'
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/thin-1.5.1/lib/thin/backends/base.rb:63:in `start'
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/thin-1.5.1/lib/thin/server.rb:159:in `start'
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.5.2/lib/rack/handler/thin.rb:16:in `run'
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/base.rb:1408:in `run!'
/Users/ashleycox/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.3/lib/sinatra/main.rb:25:in `block in <module:Sinatra>'
Any help would be greatly appreciated, thank you.

RestClient failing to GET resource using SSL client certificate

I'm trying to use RestClient to retrieve a page that's secured using an SSL client certificate. My code is as follows:
require 'restclient'
p12 = OpenSSL::PKCS12.new(File.read('client.p12'), 'password')
client = RestClient::Resource.new('https://example.com/',
:ssl_client_key => p12.key,
:verify_ssl => OpenSSL::SSL::VERIFY_NONE)
client.get
When I run it, I see the following failure:
1.9.3-p374 :007 > client.get
RestClient::BadRequest: 400 Bad Request
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:48:in `return!'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
from /home/duncan/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/net/http.rb:745:in `start'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/resource.rb:51:in `get'
from (irb):7
from /home/duncan/.rvm/rubies/ruby-1.9.3-p374/bin/irb:13:in `<main>'
I'm fairly sure this is a failure to authenticate, as I get the same error in a browser if I don't install the client certificate.
I'm using OpenSSL::SSL::VERIFY_NONE because the server has a self-signed certificate, and I believe this is the correct value to pass to ignore that.
Any suggestions on how to get this working would be greatly appreciated - even a pointer to some detailed documentation, or a suggestion of a different Gem could work. I've not had much luck with either the Gem docs or Google :(
Your HTTPS request is going to need the client certificate as well as the key. Try:
client = RestClient::Resource.new('https://example.com/',
:ssl_client_cert => p12.certificate,
:ssl_client_key => p12.key,
:verify_ssl => OpenSSL::SSL::VERIFY_NONE)
If that doesn't work you can try capturing the handshake packets (e.g. with WireShark) to verify that the API is offering the certificate.

Checking if proxy is working in Ruby

I'm new to Ruby and trying to find a way to check if a proxy is working. I've tried using the 'net/ping' library but I am just getting some errors while requiring it(in Interactive Ruby the script works even though there are the errors, it just closes itself when I run the script normally though)
I'm trying to find a solution to make it either ignore the error, fix the error or find another way to check if the proxy works.
Thanks for help.
This is the error message I get when I try to require the 'net/ping'
proxy.rb(main):005:0> require 'net/ping'
LoadError: no such file to load -- windows/console
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/ext
ernal.rb:12:in require'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping/ext
ernal.rb:12:in'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping.rb:
10:in require'
from C:/Ruby191/lib/ruby/gems/1.9.1/gems/net-ping-1.5.2/lib/net/ping.rb:
10:in'
from C:\Users\X\Desktop\proxy.rb:5:in require'
from C:\Users\X\Desktop\proxy.rb:5
from C:/Ruby191/bin/irb:12:in'
In my opinion the only way to check if a proxy is working is to connect through it and make a request.

Resources