Why does Selenium Web Driver say Service Unavailable? - ruby

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

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.

HTTP request path is empty (ArgumentError)

I am new to ruby programming. This is my first program in Watir. When I execute the code below, I am getting HTTP request path is empty (ArgumentError). Appreciate your help in fixing this error. I am accessing internet through proxy settings. I have added HTTP_PROXY variable in environment variables to http://myproxy.mynetwork.net:8008/
test.rb
require "watir"
require "rubygems"
require "rspec"
require "watir-webdriver"
puts "Hello,World...!"
#browser = Watir::Browser.new
#browser.goto("http://www.google.com")
#browser.close
puts "Browser should have been closed.."
Output
d:\>ruby test.rb
Hello,World...!
C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1860:in `initialize': HTTP request path is
empty (ArgumentError)
from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2093:in `initialize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/remote/http/default.rb:71:in `new'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/remote/http/default.rb:71:in `new_request_for'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/remote/http/default.rb:35:in `request'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/remote/http/default.rb:64:in `request'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/remote/http/common.rb:40:in `call'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/remote/bridge.rb:634:in `raw_execute'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/remote/bridge.rb:99:in `create_session'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/remote/bridge.rb:68:in `initialize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/firefox/bridge.rb:36:in `initialize'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/common/driver.rb:31:in `new'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver/common/driver.rb:31:in `for'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.41.0/lib/s
elenium/webdriver.rb:67:in `for'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.9/lib/watir
-webdriver/browser.rb:46:in `initialize'
from test.rb:8:in `new'
from test.rb:8:in `<main>'
Firefox browser window is getting opened. After that the address bar is not changing, and it is getting closed after some time.
net/http require a valid path. So you have to put a trailing slash at the end of the URL, like this:
#browser.goto("http://www.google.com/")
I have added HTTP_PROXY variable in environment variables to
http://myproxy.mynetwork.net:8008/
Removing my HTTP_PROXY variable from my (Windows) user/system env settings helped!
am not sure why this is causing the issue with selenium..
The following steps helped me:
Remove HTTP_PROXY and HTTPS_PROXY variables from my system (User Variables)
Restarted my terminal for the changes to take effect.
That's about it! No more errors!

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.

Access denied while started browsermob proxy server

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

How to open tor browser using watir?

With my ruby code I want to open Tor Browser instead of Firefox,for this I tried this code
path='C:\Tor Browser\App\tor.exe'
Selenium::WebDriver::Firefox.path = path
driver = Selenium::WebDriver.for :firefox
ie = Watir::Browser.new :firefox, :driver => driver
I got this error
c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/launcher.rb:79:in `connect_until_stable': unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055) (Selenium::WebDriver::Error::WebDriverError)
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/launcher.rb:37:in `block in launch'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/socket_lock.rb:20:in `locked'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/launcher.rb:32:in `launch'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/firefox/bridge.rb:24:in `initialize'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/driver.rb:31:in `new'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/driver.rb:31:in `for'
from c:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver.rb:67:in `for'
from C:/new_trademap/newTrademapTest.rb:28:in `<main>'
What should I have to do ? or there is any other way to do so?
links link1 google group link
The answer:
First, open the Tor browser (it needs to be running already). Then, in your code, make sure to simply start with:
Selenium::WebDriver::Firefox::Binary.path=/Applications/TorBrowser.app/Contents/MacOS/TorBrowser.app/Contents/MacOS/firefox
browser = Watir::Browser.new :firefox
*In my case: /home/tor/tor-browser_en-US/start-tor-browser
Nothing else is required - don't bother messing with profile
Your path C:\Tor Browser\App\tor.exe is not a path to firefox, it is a path to the Tor executable. What you will probably need to do is to modify your start-tor-browser script to run that tor.exe but not start firefox and then initialize Tor's Firefox separately.
Another option would be to modify your path of C:\Tor Browser\App\tor.exe to the path for the TorBrowser's Firefox binary instead, now that the TorBrowser bundles Tor and Firefox into a single executable.

Resources