Archive for the ‘Ruby’ Category
Setting up Ruby on Rails
I’m going to give Ruby on Rails (RoR) a try on my Windows XP machine. Here’s how I’ve set it up…
- Install Ruby
Using the Ruby One Click Installer Ruby One Click Installer (v184-16 release candidate 1) - Setup proxy
set HTTP_PROXY=http://wwwcache:8080
- GEM install Ruby on Rails
gem install rails --include-dependencies
- Install RadRails
I also downloaded and installed the latest RadRails (v0.6.1) - Database
I’m going to try rails with our dev MS-SQL server so no database to install. No doubt lots of configuring to do though!
NOTE:
There appears to be a problem with the WEBrick web server in this setup. I couldn’t find the answer on google but I did find a work around. If you run the WEBrick server using the standard ruby script\server you will get the following error:
ruby script\server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2006-04-04 13:20:35] INFO WEBrick 1.3.1
[2006-04-04 13:20:35] INFO ruby 1.8.4 (2005-12-24) [i386-mswin32]
[2006-04-04 13:20:35] WARN TCPServer Error: Bad file descriptor - bind(2)
c:/ruby/lib/ruby/1.8/webrick/utils.rb:73:in `initialize': Bad file descriptor - bind(2) (Errno::EBADF)
from c:/ruby/lib/ruby/1.8/webrick/utils.rb:73:in `create_listeners'
from c:/ruby/lib/ruby/1.8/webrick/utils.rb:70:in `create_listeners'
from c:/ruby/lib/ruby/1.8/webrick/server.rb:75:in `listen'
from c:/ruby/lib/ruby/1.8/webrick/server.rb:63:in `initialize'
from c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:24:in `initialize'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/webrick_server.rb:59:in `dispatch'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/servers/webrick.rb:59
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:136:in `require'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.0/lib/commands/server.rb:30
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:136:in `require'
from script/server:3
There appears to be some problem binding to the port 3000, netstat shows nothing running on port 3000 so I’ve no idea what is causing it. Any hoose a work around is simply to run on another port:
ruby script\server --port=8000
If I do find out what is causing the problem I’ll update this post.
Ruby iteractive debugging
Buggy code, me? Never! If you have a problem with buggy code though your in luck ruby has a debugger baked in:
Simply run ruby with the debug flag:
ruby –r debug test.rb
USEFUL COMMANDS
list – List the source code, pass in a number range to filter the list. For example to list the lines 1 to 9:
list 1-9
b – Set a break point, pass a line number to break on. For example to break on line 7:
b 7
c – Continue program until breakpoint or end of program
disp – Display the contents of a variable. For example to see the contents of the look_at_me variable:
display look_at_me
watch – Set a watchpoint on a variable. For example to break when the variable meaning_of_life equals 42:
watch meaning_of_life=42 c
del – Delete a breakpoint or watchpoint. For example to delete the first breakpoint we created:
del 1
Ruby unit tests – run only one test method
What if you don’t wish to run all the unit tests in your test case?
You can tell ruby to run only one test method by passing in the name of the test:
ruby test_mytestcase.rb --name test_just_this_method
Functionally testing SAP Enterprise Portal using Watir
It’s taken about a day but I’ve managed to get some functional tests running using Watir. It’s not been as easy as it could have been mainly due to the way the SAP Enterprise Portal uses iFrames and the anti cross site scripting in Internet Explorer. I got there in the end though! I can now logon to the portal and functionally test iViews using a fairly simple Watir (Ruby) script:
def logon
$ie.goto($html_root)
if $ie.contains_text("Log Off")
logoff()
end
$ie.text_field(:id, 'logonuidfield').set($user_id)
$ie.text_field(:id, 'logonpassfield').set($password)
$ie.button(:name, 'uidPasswordLogon').click
end
A couple of things to note:
Talk to the iView directly
Due to the use of iFrames and IE anti cross site scripting accessing an iView which is embedded in a portal page is not possible with the current (1.4.1) version of Watir. Simply get the url of the iView and access it directly.
Problem with HTMLB Text Fields
ID’s and Names of HTMLB Text Field controls appear to be generated on execution, so I had to write a little script which would find a text field using the title property:
def find_text_field_by_title(my_title)
found_text_field = nil
$ie.text_fields.each do |tf|
if tf.title == my_title then
found_text_field = tf
break
end
end
raise "No text field found with title = " +
my_title if found_text_field.nil?
return found_text_field
end
NOTE: I’m sure my Ruby/Watir scripts leave a lot to be desired, I don’t really know who to code in Ruby but they do seem to work!
Searching Google for Watir help
Having trouble finding help on Watir?
Try the following in google:
site:rubyforge.org "[Wtr-general]" your search
Installing Watir via the gem utility
The install should be as simple as dropping into dos, cd to your Ruby folder and run the command:
gem install watir
Unfortunately it wasn’t that simple for me, we work via a proxy here so I had to tell the gem utility about it.
Running gem install when you are behind a proxy
Set the HTTP_PROXY environment variable.
Example:
set HTTP_PROXY=http://mycache:8080
You can now run:
gem install watir
Or you can pass the proxy information as a parameter to gem via the -p switch:
gem install watir -p http://mycache:8080
Gem will now go off download and install the latest Watir package, version 1.4.1 in my case. Be patient this can take some time depending on server load.
Watir – Testing your website
I am hoping to use this software to test my web developments, from what I have read it looks very promising.
“WATIR stands for “Web Application Testing in Ruby”. Watir
is a free, open-source functional testing tool for
automating browser-based tests of web applications. It is pronounced water.”
The install…
Using the Ruby One-Click installer for windows (version ruby182-15.exe), just run standard install accepting all defaults.
NOTE: I tried the final version of Ruby 182-14 and had lots of install problems so use at least version 182-15!
Using the Watir One-Click installer for windows (version watir-1.4.1.exe), just run standard install accepting all defaults.
Now onto learning what can be done with this thing…