Why am I unable to launch a URL via Ruby script? - ruby

I am a Selenium (Ruby) newbie.
I am trying to launch Google homepage as below:
#selenium = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000);
#selenium.start
However, after the script runs, it launches Firefox with below URL:
http://localhost:4444/selenium-server/core/Blank.html?start=true
I am unable to figure out the reason.

i think you need the keys for your hash, eg
Selenium::Client::Driver.new(:host => 'localhost', :port => 4444, etc)

Related

Enabling SSL for a thin server and sinatra

I'm trying to enable SSL for my thin server web app so that it can work over HTTPS.
I have done the following:-
launching of thin web server
MyApp.run! :host => '127.0.0.1', :port => 9090, :sslenable => true, :sslverifyclient => OpenSSL::SSL::VERIFY_NONE,
:sslcertificate => '.ssl/server_key.pem', :sslprivatekey => '.ssl/key.pem'
I generated a self signed certificate and private key using the openssl module in Ruby, created a directory called .ssl and stored them there as pem files.
The web framework I'm using for my web app is Sinatra. I'm also using the rack-ssl gem in the following way..
myapp.rb
require 'rack/ssl'
class MyApp < Sinatra ::Base
use Rack::SSL
use Rack::Session::Cookie,
:key => '_rack_session',
:path => '/',
:expire_after => 2592000,
:secret => ''
...
end
When I go to http://localhost:9090, I would expect to see my app displayed as normal but with a padlock and a cross through it as any http request is being redirected to https and I see the error "webpage is not available". However when I remove ssl-rack ruby gem and restart my app and go to https://localhost:9090,i get an ssl connection error with the following details:
Unable to make a secure connection to the server. This may be a problem with the server, or it may be requiring a client authentication certificate that you don't have.
Error code: ERR_SSL_PROTOCOL_ERROR
Can anyone please advise me on how best to configure a thin server to enable SSL?
I'm running Sinatra and Thin on Heroku with SSL using the Rack::SslEnforcer, doing this:
if production?
require 'rack/ssl-enforcer'
use Rack::SslEnforcer
end
This should be before you enable :sessions in your file. So, Rack::SslEnforcer needs to be placed above the session part when you configure your app.
Somewhat unrelated, but perhaps still relevant, you might consider adding:
require 'encrypted_cookie'
cookie_config = {
:key => 'usr',
:path => "/",
:expire_after => 86400, # one day in seconds
:secret => ENV["COOKIE_KEY"],
:httponly => true
}
cookie_config.merge!( :secure => true ) if production?
use Rack::Session::EncryptedCookie, cookie_config
You also need to set the COOKIE_KEY in your environment to something secret and long-ish.

Can Watir-Webdriver with PhantomJS access an external URL that has basic auth?

I am using watir-webdriver with cucumber and ruby to build up automated regression tests for a website. Cucumber and watir-webdriver are outside of the rails application that is used for the actual site. To get my tests to run on Jenkins, I would like to start to use phantomjs to run the tests headlessly. In order to access the the pre-production environments of the website, I need to get past the site's basic authentication.
The issue I am running in to is: when phantomjs attempts to access an external URL that has basic auth, it is getting hung up and not getting past the basic auth username/password.
Any thoughts on how to get phantomjs to recognize a URL like the following:
https://admin:password#test.website.com"
env.rb:
BASE_URL = Configuration["base_url"]
require "watir-webdriver"
case ENV['BROWSER']
when 'chrome'
profile = Selenium::WebDriver::Chrome::Profile.new
browser = Watir::Browser.new :chrome, :profile => profile
when 'firefox'
profile = Selenium::WebDriver::Firefox::Profile.new
browser = Watir::Browser.new :firefox, :profile => profile
when 'phantomjs'
browser = Watir::Browser.new :phantomjs
end
end
config.yml
website:
base_url: "https://admin:password#test.website.com"
Running cucumber:
cucumber -p website BROWSER=phantomjs
The result:
Scenario: User Logs In
Given a logged out user
timed out after 30 seconds, waiting for {foobar...}
caps = { 'phantomjs.page.settings.userName' => 'admin', 'phantomjs.page.settings.password' => 'password' }
driver = Selenium::WebDriver.for(:phantomjs, :desired_capabilites => caps)
browser = Watir::Browser.new(driver)
browser.goto 'https://test.website.com'

Ruby NET::SCP and custom ports

I'm trying to define a custom port for the ssh connection using Net::SCP, but without luck so far.
Here's an example how I'm trying to download a remote file from a server with a custom ssh port:
require "rubygems"
require 'net/scp'
Net::SCP.download!("www.server.com", "user", "/opt/platform/upload/projects/file.txt", "/tmp/bb.pdf",{:password => "mypassword",:port => 22202})
The error message I'm getting is:
Errno::ECONNREFUSED: Connection refused - connect(2)
There are no entries in the server logs regarding the ssh connection, so I assume that Net::SCP isn't using my custom port.
Any tips for me ?
Regards, Alex
Well, I've found the solution myself.
require "rubygems"
require "net/scp"
Net::SSH.start("www.myserver.com", "theuser", {:password => "whateverpwd",:port => 22212}) do |ssh|
ssh.scp.download! "/opt/platform/upload/projects/my.pdf", "/tmp/bb.pdf"
end
I also keep SSH on a non-standard port and use SCP like so:
Net::SCP.upload!( "foo.net", "user", the_file, the_file, :ssh => { :keys => #keys, :port => the_port } )
Works like a champ. I also use key-based authentication, hence the keys parameter getting passed along with the port.

ies4linux with selenium and rspec

I use selenium, usually with firefox, to test my rails apps and it's all fine. I want to run my tests in IE6 as well. I'm in ubuntu, using the ruby selenium-client gem. For IE6 i use ies4linux, this is an executable which is at /home/max/.ies4linux/bin/ie6
I'm editing my selenium conf to try to get it to use the above, but can't get it working. Here's what i have at the moment:
SELENIUM_CONF = {
:client_options => {
:url => "http://awebsite.com",
:host => "localhost",
:port => 4444,
:browser => "*iexplore /home/max/.ies4linux/bin/ie6",
:javascript_framework => :jquery
}
}
Then when i make a browser in my scripts i call
Selenium::Client::Driver.new(SELENIUM_CONF[:client_options])
It's not happy with what i have in the :browser field at the moment - i get this error:
"Failed to start new browser session: Error while launching browser"
I also tried
:browser => "/home/max/.ies4linux/bin/ie6",
But got a "Browser not supported" error as it expects one from list, *iexplore in this case.
Can anyone tell me how i can get this working?
thanks, max
I don't think there's a way to do that. Selenium uses iehta, and some not-so-used parts of ie that could not be bundled in ie4linux, and even if there were, I wouldn't trust on a test that runs IE under linux.
Why not creating a VM using VirtualBox and pointing your tests to an RC server inside the VM?

Open Firefox browser with Ruby automation script

How is it possible to open FireFox browser by Ruby(for automation script)?
I use
#browser = RSpecSeleniumHelper.connect_browser('/admin/', '*firefox')
but it doesn't work.
You can start any program in ruby with:
`firefox http://www.google.com`
or
system("firefox http://www.google.com")
You can use Watir, as it supports Firefox also:
http://wtr.rubyforge.org/platforms.html
You may have to check if the Selenium Remote Control is start or not, normally it is running at port 4444.
java -jar selenium-server-xxx.jar
then you can use
#browser = Selenium::Client::Driver.new(
:host => "localhost",
:port => 4444,
:browser => "*firefox", #*iexplore, *firefox3, *safari...
:url => "http://www.google.com/",
:timeout_in_second => 60)
#browser.start_new_browser_session
Hope this helps, you can find more demo by download Selenium RC
I encountered two issues while getting this running:
If you are running your Ruby app from MacOS, the command firefox may not be properly aliased by default and so may fail without errors printed to your Ruby console.
If you already have an instance of Firefox open, you will get a message saying "Close Firefox - A copy of Firefox is already open. Only one copy of Firefox can be open at a time."
This code fixes both problems:
system("open -a /Applications/Firefox.app/Contents/MacOS/firefox-bin http://www.google.com http://www.cpap.com")
open's -a option Opens with the specified application.
The file path list works for me. If it won't load for you, first drop it and try plain "firefox" and failing that try "/Applications/Firefox.app/Contents/MacOS/firefox"
The example above shows two URLs separated by a space. You can use just one URL or as many as you care to following this pattern.

Resources