Converting Watir/Watin script to Selenium Scripts - watin

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.

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

How do I send text to a Redactor WYSIWYG editor box using Watir-WebDriver?

I'm a new user to Watir and Ruby. I have a watir-webdriver script that fills out forms on a page and submits them. The page has a few Redactor WYSIWYG editor boxes and I'm trying to send text to them. Originally I did the following:
#browser.div(:id => 'redactor-container-5').send_keys 'automated test script'
However, I then found out that Firefox had to be in the foreground for this to work (this is a problem for my team since the scripts they run are sometimes in the background). So instead I used some JavaScript to fill out this same field.
#browser.execute_script('var html="automated test script"; $(".redactor, .redactorCaption, .redactorTitle").redactor("insertHtml", html);')
However, this too required the browser to be in the foreground (this works completely fine in the foreground). I took a look at this link but there was not a Redactor example. Does anyone know how to set these fields when they are in the background?
I just tested it on my machine, and it works perfectly even when FF is minimized.
irb(main):001:0> require "watir-webdriver"
=> true
irb(main):002:0> b = Watir::Browser.new :ff
=> #<Watir::Browser:0x454b6ab6 url="about:blank" title="">
irb(main):003:0> b.goto "http://imperavi.com/redactor/"
=> "http://imperavi.com/redactor/"
irb(main):004:0> b.div(:class => 'redactor_ redactor_editor').send_keys 'automated test script'
=> ""
Before executing the last step, i actually minimized Firefox.
Env. details:
Running Windows 7 Enterpricse SP1 (64bit)
Firefox 24.4.0 ESR
watir-webdriver (0.6.9)

Headless Browser in ruby, non testing purpose

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.

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.

Resources