How do I run Firefox browser headless with my Ruby script? - firefox

I need to be able to run my Ruby scripts with my Hudson builds but don't want a browser window attempting to open.
I tried:
batman:ETW cmiller$ ./createAccount.rb -b
but this did not work.
How can I run my Ruby scripts using FireWatir headless?
Running on Mac OSX

I'd suggest the usage of PhantomJS (http://phantomjs.org/download.html)
Add binaries to your path and run:
MEDBEDbs-iMac:~ medbedb$ irb
1.9.3p392 :001 > require 'watir-webdriver'
=> true
1.9.3p392 :002 > b = Watir::Browser.new :phantomjs
=> #<Watir::Browser:0x..fe670a70a449fe1ca url="about:blank" title="">
1.9.3p392 :003 > b.goto "www.google.com"
=> "http://www.google.md/"
1.9.3p392 :004 > b.url
=> "http://www.google.md/"
1.9.3p392 :005 > b.title
=> "Google"
1.9.3p392 :007 > b.text
=> "+Вы\nПоиск\nКартинки\nКарты\nPlay\nПочта\nДиск\nКалендарь\nЕщё\nВойти\n×\nОткрывай любимые сайты быстрее\nЗагрузить Google Chrome\n\nМолдoва\n\n \nРасширенный поиск\nЯзыковые инструменты\n\nGoogle.md предлагается на: Moldovenească\n\nРекламные программыВсё о GoogleGoogle.com\n© 2013 - Правила и принципы"

I would look at using Watir-Webdriver instead of just plain Watir or Fire-watir. Especially since the only way to work with newer versions of firefox is going to be via Watir-Webdriver.
There's an earlier SO question where the answer covers just this sort of thing, so I'd suggest trying what is described there there first. Can I use Watir to scrape data from a website on a linux server without monitor?
Also since I now know you are using Mac OS, the advice in this thread from the webdriver google group might be more applicable to you

Look at http://watirmelon.com/tag/headless/.
I have followed http://watirwebdriver.com/headless/ on Linux that works for me. In above link the tutorial is intended for Mac
Here is extract of the links, how to use headless browser:
require 'watir-webdriver'
require 'headless'
Now start headless and browser
headless = Headless.new
headless.start
b = Watir::Browser.start 'www.google.com' puts b.title
b.close headless.destroy
Your browser should run headless.

The browser is opening the window as part of its normal operation and behavior.
You could look into using Mechanize, which supplies the ability to navigate a site. It isn't a browser, but uses Nokogiri underneath to let you grab DOM information and manipulate forms, submit them, and retrieve any resulting page changes. Mechanize doesn't support parsing/executing Javascript though, which puts you back into WATIR's realm.

Related

How to configure env.rb to start portable chrome (instead of local installation) with Ruby, Selenium and LapisLazuli

Question:
For my Test Automation Suite I would like to know how to configure/setup my portable Chrome to run instead of the local installed Chrome.
Reason:
There is an issue with the application I'm trying to test and Chrome Version 55. Issue: Certain buttons are since this version not clickable and I want to bypass this for the time being by using an older Chrome version.
I use Ruby version 2.3.0p0 (2015-12-25 revision 53290)
And LapisLazuli as described on www.testautomation.info
The setup itself is working. The answer might be simple, but I did not manage to get this running in this combination.
I found information about selenium/Java, but I have no idea how to get that to work with my setup.
Selenium not starting portable chrome but local installation
I also found this website: https://leanpub.com/watirbook/read
And here they mentioned the following:
3 > require "selenium-webdriver"
4 => true
5
6 > Selenium::WebDriver::Chrome.path =
7 'C:\chrome\chrome.exe'
8 => "C:\\chrome\\chrome.exe"
So I tried that in irb and this worked
require 'lapis_lazuli'
include LapisLazuli
Selenium::WebDriver::Chrome.path = 'C:\chrome_portable_v53\chrome.exe'
browser :chrome
The portable chrome version was started. Now I only need to know how to place this in my env.rb file (I assume), which looks currently like this:
require 'lapis_lazuli'
require 'lapis_lazuli/cucumber'
LapisLazuli::WorldModule::Config.config_file = "config/config.yml"
World(LapisLazuli)
I think the following is answering the question. I've added require selenium-webdriver and that Chrome.path to the env.rb and this seems to work.
require 'lapis_lazuli'
require 'lapis_lazuli/cucumber'
require 'selenium-webdriver'
Selenium::WebDriver::Chrome.path = 'C:\chrome_portable_v53\chrome.exe'
LapisLazuli::WorldModule::Config.config_file = "config/config.yml"
World(LapisLazuli)

Webdriver Watir Ruby Chrome Versions

I want to test with different versions of Chrome. How can you point at a specific Chrome executable/version with Watir and Ruby?
To use a specific Chrome executable specify the Selenium::WebDriver::Chrome.path.
require 'watir'
# Note that the path includes the file directory and executable
Selenium::WebDriver::Chrome.path = 'C:/Program Files (x86)/Google/Chrome/Application/Chrome.exe'
browser = Watir::Browser.new :chrome

Ruby webdriver doesn't navigate in Chrome

I have just finished setting up the latest versions of Ruby (Ruby 2.0.0-p451 (x64)) and Watir. I have downloaded both the selenium web driver gem and the watir webdriver gem, both installed successfully. I then got the Ruby dev kit, installed that and it installed fine. Lastly, I got the latest version (2.9) of chromedriver.exe and placed it in the bin directory for ruby.
I can start Chrome from a Watir script but I can't navigate to any URL. When Chrome opens a new instance it has one tab and in the URL all it says is "data:,", the page is completely blank. I have used examples and this one works with Firefox but not Chrome
require 'watir-webdriver'
b = Watir::Browser.new :chrome
b.goto 'bit.ly/watir-webdriver-demo'
b.text_field(:id => 'entry_0').set 'your name'
b.select_list(:id => 'entry_1').select 'Ruby'
b.select_list(:id => 'entry_1').selected? 'Ruby'
b.button(:name => 'submit').click
b.text.include? 'Thank you'
(that example came from http://watirwebdriver.com/)
If I remove ":chrome" from the end of the second line, Firefox opens and navigates to the URL as I'd expect, but can't get that to work in Chrome.
If I use the selenium-webdriver with Chrome I get the exact same result: Chrome opens, it has "data:," in the URL and doesn't navigate to the URL in the script.
I also tried this in the Ruby 1.9.3-p545 version of Ruby and I get the exact same result, so I uninstalled that and am now using the 2.0.0 version of Ruby.
How can I get my script to open chrome and navigate to a URL?

new window creation in watir

I am new to watir. I am trying to create new ie window with
browser = Watir::Browser.new
but it gives error message like
`user_is_bewildered': Error in the default values: :browser's value must be one of 'ie', 'firefox', or 'safari', and '' doesn't look right. (StandardError)
I donno how to set default browser. Can some one help me? There is a another thread here. But I am not able to understand what i need to do in ffi.
Thanks
If this is the original Watir gem, then the following is how I used to launch it:
require 'rubygems'
require 'watir'
Watir::Browser.default = "firefox"
browser = Watir::Browser.new
# Whatever you want to do in watir
IamChuckB's answer may be a more efficient way of doing this, but having not used it, I'm not sure.
You need to tell it which browser to open. Try this:
browser = Watir::Browser.new :ff
I haven't played around with watir since my last job so I had to look this up. As I last recall, WATIR was not entirely integrated with FireWATIR (the Firefox based variant). It's good to see that the two have apparently been reconciled in the meantime.
Taken from Watir in Five Minutes on Zeljko Filipin's github, BTW.
To install devkit,
create a folder in the ruby directory called devkit
get the devkit from here
unpack it into the devkit directory created in step 1
add c:\ruby193\devkit\bin;c:\ruby193\devkit\wming\bin to your path, of course adjusting for your ruby install directory
now open a command prompt to install the gem again
c:\> gem install watir
The issue is resolved. Thanx for the the inputs. The issue was with wrong nokorigi gem installation, initially i installed x86-mswin32-60, i uninstalled it and tried with x86-mingw32, it solved.

Standalone testing using ruby 1.9.2, selenium-client and rspec (without rails)

Has anyone successfully set up a standalone environment for using ruby 1.9.2, selenium-client, and rspec to test a NON-RAILS website.
I have scoured the web but I cannot seem to get the magic formula. There are gem version conflicts, missing libraries, etc. At this point I am totally confused.
BASIC PROBLEM: I have many test suites and tests ceated using SeleniumIDE 1.0.10 for a NON-RAILS application (IIS/ASP). I SIMPLY want to use the converted tests that the SeleniumIDE can provide and run them using ruby or better yet rake. (Note that the SeleniumIDE can create RSpec or Test/Unit tests and I would be love to get either working).
As I understand it the selenium-client has rake tasks to start/stop the SeleniumRC server and to run tests. But for the life of me I cannot get any of this this going.
I can provide more information if need be. But, at this point I would be happy to know of anyone that is also going down this road.
-Thanks!
P.S. I am doing this as a prelude to hybridizing the IIS/ASP application with rails and iteratively going to a full conversion. If I cannot get this first step going my bosses are going to nix the whole idea of going to rails, so please help ;-)
You will need to gem install 'rspec' and 'selenium-client'. I have ruby 1.9.2 installed and yes, I use rvm. I apologize for the firefox_version nonsense. I have multiple firefox versions installed on my Macbook. If you have a single firefox installed then simply specify :browser => "*firefox"
You will need to open two Terminal windows. In the first, start the selenium-server.jar.
In the second window, cd to the directory containing rspec_hello.rb and enter
ruby ./rspec_hello.rb
Here is rspec_hello.rb
require "rubygems" # >= 1.8.12
require 'rspec' # >= 2.1.0
require "selenium/client" # >= 1.2.18
describe "google" do
# attr_reader :selenium_driver
#selenium_driver = nil
before(:each) do
firefox_version = 3
firefox_path = "/Applications/Firefox" + firefox_version.to_s + ".app/Contents/MacOS/firefox-bin"
#selenium_driver = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox #{firefox_path}",
:url => "http://www.google.com/",
:timeout_in_second => 60
#selenium_driver.start
end
after(:each) do
puts " "
puts "goodbye world"
#selenium_driver.close_current_browser_session
end
it "test google" do
puts "hello world"
#selenium_driver.open "/"
#selenium_driver.type "q", "selenium wiki"
#selenium_driver.click "btnG"
#selenium_driver.wait_for_text("Selenium Wiki")
#selenium_driver.text?("Selenium Wiki").should be_true
end
end
Have you considered using rvm to create a sandboxed environment and bundler to manage the dependencies for you?
I use rvm and bundler when working with rails. For the project I am concerned about in this question I did not want to have a separate environment for the stand-alone tests of the OLD stuff (IIS/ASP).
So, I decided to use the Test:Unit Selenium conversions instead of RSpec, along with some rake tasks to do the testing stand-alone in the same environment as the hybrid project.
This made sense because almost all of these selenium tests are testing the OLD half of the hybrid system and will be replaced with rspec/cucumber/capybara tests for the NEW rails code as it is integrated, until the OLD half (along with the stand alone tests) are no more.
There are still uses for selenium in the NEW integrated rails only project, especially for AJAX testing.
See this railscast for an example.

Resources