Archive
Useful RubyMine Keyboard Shortcuts
Meet my new colleague, Jetbrains RubyMine “The Most Intelligent Ruby on Rail IDE” sounds big huh! Since he will be my new companion / buddy for the next months to come, I decided to know him better and build a good rapport.
Here are some of the keyboard shortcuts I find to be friendly and useful:
| Shortcut | Description |
| Ctrl+Alt+S | Go to Settings |
| Ctrl+N | Open a class |
| Ctrl+Shift+N | Open a file |
| Ctrl+B | Go to declaration |
| Ctrl+Space | Code completion |
| Ctrl+E | Show recent files |
| Ctrl+K | Commit changes |
| Ctrl+G | Go to line |
| Ctrl+T | Update project |
| Alt+Left/Right | Navigate through the editor tabs |
| Ctrl+Slash | Make a block comment |
| Ctrl+F | Find from current file |
| Ctrl+Shift+F | Find from current folder |
Error installing Nokogiri in Ubuntu 10.10
Following Nokogiri Installation for Ubuntu I run below #nokogiri requirement in my terminal:
sudo apt-get install libxslt-dev libxml2-devsudo gem install nokogiri
Running “sudo gem install nokogiri” displays the following error:
Building native extensions. This could take a while... ERROR: Error installing nokogiri: ERROR: Failed to build gem native extension. /usr/bin/ruby1.8 extconf.rb extconf.rb:5:in `require': no such file to load -- mkmf (LoadError) from extconf.rb:5 Gem files will remain installed in /var/lib/gems/1.8/gems/nokogiri-1.4.3.1 for inspection. Results logged to /var/lib/gems/1.8/gems/nokogiri-1.4.3.1/ext/nokogiri/gem_make.out
Was able to resolve the issue by installing ruby1.8-dev and reinstalling the nokogiri gem:
sudo apt-get install ruby1.8-devsudo gem install nokogiriexist@exist:~$ sudo gem install nokogiri Building native extensions. This could take a while... Successfully installed nokogiri-1.4.4 1 gem installed Installing ri documentation for nokogiri-1.4.4...
Selenium-Ruby Installations in Linux
In relation to my previous post on Selenium and Ruby setup in Windows, here’s an installation guide for Linux peeps:
1. Install JRE
- In your terminal type: sudo apt-get install sun-java6-jre
- To verify type: java -version
Note: Java version should be 1.5 or higher versions
2. Install Ruby
- In your terminal, type: sudo apt-get install ruby
- To verify type: ruby –version
Note: ruby 1.8.7 works with rspec version <= 1.3.1
3. Install Rubygems
- From the terminal, type: sudo apt-get install rubygems OR you can follow this tutorial Installing RubyGems
- To verify type: gem –version
4. Install other useful gems
- Rake ( to create a task that runs set of tests )
- Type: sudo gem install rake
- To verify type: rake –version
- Selenium-client ( API to drive Selenium tests from Ruby )
- Type: sudo gem install selenium-client -v 1.2.18
- To verify type: gem list selenium-client
Note: selenium-client 1.2.18 works with rspec version 1.2.8
- Rspec ( to define executable examples of the expected behaviour of your code )
- Type: sudo gem install rspec -v 1.2.8
- To verify type: spec –version
- Faker ( to easily generate fake data: names, addresses, phone numbers, etc. )
- Type: sudo gem install faker
- To verify type: gem list faker
Selenium and Ruby setup in Windows
Newbie in Ruby? Never heard Selenium? Had a reformat?
No sweat! Here’s a list of requirements to set up Selenium and Ruby in your Windows machine.
SELENIUM Remote-Control (RC )
–> is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser.
- get it here
- extract the file anywhere in you local machine
JAVA
–> Requirement to run the selenium server, should be 1.5 or later version
- get it here
- install Java and configure your PATH environment variable correctly.
- from the console, you can verify the installation by typing: java -version
SELENIUM RC RUBY CLIENT DRIVER
- requires Ruby
- recommended way to install: Ruby 1.8.6 One-Click Installer
- upgrade to latest version by getting the latest binary files of Ruby 1.8.7
Rubygems
- get the latest rubygem distribution as tgz or zip from here
- extract the archive to your desired directory
- 2 ways to install ruby:
from the console, go to the extracted directory then type: ruby setup.rb
from your explorer, go to the extracted directory and double click setup.rb file
- install the ruby client driver as a rubygem by typing: gem install <ruby gem>
gem install selenium-client
- install other gems that will be useful in your testing:
gem install rspec -v=1.2.6
gem install syntax
gem install faker
BDoc documentation
–> guide for all your rubygems documentations
- from the console type: gem install aptinio-bdoc
- then save the doc to your local, from the console type: bdoc //[path]
To get scripts or file from a repository through Git
- from the console, install github, bash or gui OpenInGitGui-2.0.zip
- generate an SSH rsa, type: $ ssh-keygen -t rsa
- copy the generated id_rsa pub
- paste it on the personal settings of ur public_key
- then clone
Get all hyperlinks within a page using Nokogiri
Task: Create a selenium script using Ruby that will collect all the available links within a page.
In essence we will try to create a method that will parse the html source of the current page and get all the elements with css(‘a’) or xpath ‘//a’ which indicates an anchor element. First let’s try to do it in IRB.
Steps:
1. Start your server and fire up your irb
2. In your console, type
require 'nokogiri'
3. Initialize the page we want to test, say we want to get all the hyperlinks within a google home page.
page = "http://www.google.com.ph"
4. Type the following commands
doc = Nokogiri::HTML(open(page))
links = doc.css('a')
hrefs = links.map {|link| link.attribute('href').to_s}.uniq.sort.delete_if
{|href| href.empty?}
Of course we would not like to do the procedure every time in our console, thus we could save it as a method in our class like the following:
# method that will get all links using Nokogiri
def get_all_hrefs_nokogiri
page = self.get_location()
doc = Nokogiri::HTML(open(page))
links = doc.css('a')
hrefs = links.map {|link| link.attribute('href').to_s}.uniq.sort.delete_if {|href| href.empty?}
return hrefs
end
# get all links without using Nokogiri
def get_all_hrefs
hrefs = []
self.get_xpath_count('//a').to_i.times do |i|
if self.is_element_present("document.links[#{i}]") {hrefs << self.get_attribute("document.links[#{i}]@href")}
end
return hrefs
end
end
Convert XML to CSV with Nokogiri Ruby gem
Once upon a time, in an exciting world of software testing… Exist QA team had been using Testlink 1.8.3 as an open-source tool for test management. They were happy and it serves them well not until their client request for a copy of the testcases with complete details in EXCEL format. Doomed! Testlink only offers generation of test specification in HTML, OpenOffice Writer and MS Word but unfortunately not in EXCEL.
But just like a princess with a prince charming… then came Nokogiri(saw in Japanese) gem from Ruby which is an HTML, XML, SAX and Reader parser. It supports document searching via XPATH and CSS3 Selectors. Not to mention FasterCSV also a Ruby gem which provides a complete interface to CSV files and data. It offers tools to enable you to read and write to and from Strings or IO objects, as needed.
First they install these precious gems in their Windows machine by executing the following commands:
gem install nokogiri
gem install fastercsv
With these tools Exist QA carefully plans a plot to solve their problem. Since Testlink has the ability to export testsuite together with its testcases in XML format, they use this advantage to pass it as an input file in their Testlink parser code in Ruby. Here’s their gameplan:
require 'rubygems'
require 'nokogiri'
require 'fastercsv'
FIELDS = %w{Testsuite ID Name Summary Steps Expected_Result }
def new_testcase(csv, suite, id, name, summary, steps, expectedresult)
testcases = []
testcases << suite
testcases << "GPC - #{id}"
testcases << name
testcases << summary
testcases << steps
testcases << expectedresult
csv << FasterCSV::Row.new(FIELDS, testcases)
end
csv = FasterCSV.open(ARGV[1],"w")
csv << FIELDS
doc = Nokogiri::XML(open(ARGV[0]))
doc.xpath('//testsuite').each do |tsuite|
puts "#{tsuite.attribute('name')}\n"
doc.xpath('//testcase').each do |tcase|
new_testcase(csv, tsuite.attribute('name'), tcase.css('externalid').inner_text,
tcase.attribute('name'), tcase.css('summary').inner_text,
tcase.css('steps').inner_text, tsuite.css('expectedresults').inner_text)
end
end
All they need to do is run the program in their console following this format:
ruby <filename> “<input>” “<output>”
Where filename is the name of the Testlink parser code; input is the xml filename(generated XML file from Testlink) and output is the csv filename(file where the parsed xml data will be saved).
ruby tlparser.rb “test.xml” “test.csv”
Nokogiri and FasterCSV saves the day! Now they can provide the testcase report in no time, every time their client request for it. And Exist QA lives happily ever after…
Using .irbrc file to configure your IRB
In my latest post “IRB Recipes” where we’ve discussed how to configure IRB to enable auto-complete, auto-indent and to clear screen, notice that when you exit your IRB the configurations return to its default value. And it not so DRY (Don’t Repeat Yourself) to type the recipes every time you fire up your IRB.
This prob leads us to using an .irbrc file to permanently configure your IRB every time you use it. Here’s how I set up my initial .irbrc file:
1. Create .irbrc or _irbrc file in wherever directory/location you want (In my case I saved it in C:\Documents and Settings\user)
Note: Creating a file in notepad/wordpad having “.” before the filename will not allow you to do so, I suggest you use Notepad++ source code editor
2. Edit your .irbrc file

.irbrc file
Let’s dissect the .irbrc file configuration:
Line 1: Enables auto-completion
Line 2: Enables pretty print
Line 3: Enables auto-indention
Line 4: Enables the use of readline
Line 6-8: Enables clear screen inside IRB
Line 9: Validates if that .irbrc file was loaded successfully
3. Check if the your .irbrc file is successfully loaded
- Run console
- Go to the directory where you saved your .irbrc file
- Fire up IRB
- Manually check auto-indention, auto-completion and clear screen method (in my case I validated it with a “Yes! Configuration is loaded!” message)
4. To effect the .irbrc configuration in other directory other than its current location
- Create a HOME environment variable name
- Set variable value to the current location of the .irbrc file
- Reboot to effect changes in the environment variable
5. Verify .irbrc file by going through step # 3 but this time go to other directory and not on the location of your .irbrc file


Multi select comment in Ruby
Here are some ways to do multi-select comments in your Ruby code:
1. =begin/ =end block
Note:
All codes between the =begin/=end block is treated as comments
=begin/=end block should NOT be indented
2. ctrl + shift + c
To do this, highlight the block that you want to comment out, then click ctrl + shift + c