Headless Browser in ruby, non testing purpose - ruby

What are the options for using Headless browser in ruby? Already Tried Watir, but it opens a browser window for every page opened, that is un desired side effect. It would be awesome if there was something like Phanthom JS or if i could Phanthom JS in ruby code.

First, Watir does not "open a browser window for every page" as you describe, unless you are doing something wrong.
Second, you can use phantomjs with Watir. Uninstall the Headless gem, and just do
broswer = Watir::Browser.new :phantomjs
browser.goto "some_web_page.com"
browser.close # When you are done.
I suspect you are opening a new browser instance for every page, rather than just using goto. If that's the case, try using .new() or .start() just once, then .goto() after that. That is, just replace :phantomjs in the code above with :ff for example.
Keep in mind that if you use "headless", last I heard it did not yet work on OS X.

Related

Can extensions be installed in Watir's Firefox browser?

I'm trying to use Watir on a page that has a large number of ads and it's really slowing down my tests. Why when Watir launches Firefox do my extensions (Adblock, etc.) not carry over and is it possible to install them?
Yes, you can do it. Let' assume you to want use Firebug extension..
First download the Firebug xpi file, then use the following code:
profile = Selenium::WebDriver::Firefox::Profile.new
profile.add_extension "../path/to/firebug.xpi"
b = Watir::Browser.new :firefox, :profile => profile

Ruby Watir - getting NoMethodError: undefined method 'start' on a newly installed PC

I'm new to Ruby so this might be a really dumb question. But we have this code working on an existing Ruby install PC.
def usr_OpenURL(strURL, strBrowserType)
if strBrowserType == "IE"
# Open Browser at the specified URL and Maximise
browser = Watir::Browser.start(strURL)
browser.waitForIE
browser.bring_to_front
browser.maximize
sleep($nSleepTime)
elsif strBrowserType == "Chrome"
browser = Watir::Browser.new :chrome
browser.goto strURL
sleep($nSleepTime)
else
puts "No Known Browser is Declared"
end
return browser
end
However installing the same version of Ruby on my pc and running the code is returning the error:
undefined method 'start' for Watir::Browser:Class (NoMethodError).
So I used irb to play around with it a bit.
If I do:
require "Watir"
browser = Watir::Browser.start("http://www.google.com")
I get the error, but if I do:
require "Watir"
browser = Watir::Browser.new
browser = Watir::Browser.start("http://www.google.com")
then its ok. It opens a new browser window at that url as expected.
Comparing the PCs I can see different versions of the watir, and watir-webdriver gems are installed - so not sure if something changed between versions.
The person who wrote this ruby code is no longer here - so I can't ask them why they're not doing a .new and Goto for IE.
Any ideas would be appreciated :) Thanks!
Update:
I found this in watir-classic 3.2.0 gem. Thinking maybe that is the culprit.
Watir::Browser is now a class instead of a module - beware if you're monkey-patching.
This is a problem in watir 4.0. I've opened up an issue for that https://github.com/watir/watir/issues/5 - hopefully i will fix it soon :)
The error is likely occurring because the new PC is using Watir 4.0.0, where as the previous PC was using a Watir 3.x version. The recently released Watir 4.0.0 has logic for directing usage between the watir-classic gem and the watir-webdriver gem.
I believe that the gem is not determined until you do Browser.new. After that, the gem is loaded and if it is the watir-classic gem you will have the methods such as Browser.start and Browser.attach (as these do not exist in watir-webdriver).
A possible solution, if you are only using IE is to directly require the needed gem. So do:
require 'watir-classic'
instead of
require 'watir'
As far as I know Browser#start is just a shortcut for Browser#new and Browser#goto. I do not know what is causing the error, but it should be perfectly safe to replace
browser = Watir::Browser.start(strURL)
with
browser = Watir::Browser.new :ie
browser.goto strURL

How to access Firefox Extension I added in Selenium Webdriver?

I know that you can load up either an existing Firefox profile, or create one using Ruby Bindings in the selenium-webdriver gem, as described here:
http://code.google.com/p/selenium/wiki/RubyBindings
And then use add_extension to add any number of Firefox extensions to the instance, but then what? The window for the extension I'm using does not appear during the test. How do I use the extension?
Is there a way to have the extension be open by default when the driver opens Firefox?
Here is the code I'm using:
#!/usr/bin/env ruby
require "rubygems"
require "selenium-webdriver"
default_profile = Selenium::WebDriver::Firefox::Profile.from_name "default"
default_profile.add_extension("/Users/******/Library/Application Support/Firef\
ox/Profiles/wvon3h99.default/extensions/{9c51bd27-6ed8-4000-a2bf-36cb95c0c947}.\
xpi")
driver = Selenium::WebDriver.for(:firefox, :profile => default_profile)
driver.navigate.to "http://google.com"
element = driver.find_element(:name, 'q')
element.send_keys "Hello WebDriver!"
element.submit
puts driver.title
driver.quit
It depends on extension. Usually the extension's behaviour can be to some extent controlled by setting appropriate properties (the ones you can find in about:config) when creating an FF profile. For instance to have Firebug window open by default after FF starts I would include the following line in my code:
default_profile["extensions.firebug.allPagesActivation"] = true
The extensions I use usually have some kind of auto-export feature that automatically sends data to server or saves it on disk. I am afraid there is no way of controlling an extension with WebDriver so not all extensions will be usable in automated tests.

Converting Watir/Watin script to Selenium Scripts

Is it possible to automate the translation of a Watir/Waitin script to selenium? The problem is the selenium ide is only for firefox, and I need to record on Internet Explorer.
If you already have a Watir script, why don't you use it for both IE and FF?
require 'watir'
Watir::Browser.default = "ie"
#Watir::Browser.default = "firefox"
#browser = Watir::Browser.new
I'm not seeing where Selenium enters into that problem based on your first statement and the problem with FF/IE.

Programmatically take ScreenShot of Desktop in Ruby?

I asked this question about taking a picture of a webpage programmatically, and I've downloaded and got webkit2png working (taking pictures of HTML pages like blogs and whatnot). So cool, thanks for showing me that!
Now I would like to start doing more, like being able to take pictures of Flash websites after they have loaded, and of my desktop.
Is it possible to take pictures of Flash websites with webkit2png (considering you might have to wait a few seconds for it to load)?
But the main question is, how do I programmatically take pictures of the desktop? That would allow me to have much more control over what's going on.
You can use xwd(1) to take a screenshot of the root window:
xwd -display :0 -root|xwdtopnm |pnmtopng > $1
This is an awesome question!
A couple of years ago I has to work on a similar project. I found a library, called watir, you can use to control system browsers from Ruby.
At the time I checked, it wasn't really reliable in a Linux environment, but right now it seems to be pretty solid.
Here's a couple of links:
http://90kts.com/blog/2008/capturing-screenshots-in-watir/
http://www.marekj.com/wp/2008/04/desktop-screenshots-with-watir-win32screenshot-and-rmagick/
http://clearspace.openqa.org/thread/13949
http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoItakescreenshotsandappendtoaWordfile%3F
I have never tried this solution so I would be really happy if you can write here a feedback if you decide to go with Watir. All the examples targets a Windows server, I didn't found a valid tutorial using a Linux + Firefox environment.
You can use Watir WebDriver like this:
$ irb
irb(main):001:0> require 'watir-webdriver'
=> true
irb(main):002:0> browser = Watir::Browser.new
=> #<Watir::Browser:0x136da92fad77d562 url="about:blank" title="">
irb(main):003:0> browser.goto 'http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby'
=> "http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby"
irb(main):004:0> browser.screenshot.save 'screenshot.png'
=> #<File:screenshot.png (closed)>
irb(main):005:0>
which seems to do the job rather well! I tested this on my Mac, but I'd be surprised if it didn't work well on Linux too.

Resources