Saturday, October 1, 2011

Playing with ruby modules


Its time to play around with ruby modules as its a weekend. I always wanted to check how do ruby modules solves multiple inheritance problem. So lets not waste time, here it goes:



module M1

def add(x, y)
x + y
end

end

module M2

def add(x, y)
(x * x) + (y * y)
end

end

class A
include M1, M2
end

a = A.new
puts "Addition is: #{a.add(2,3)}"
puts A.ancestors

Result:
Addition is: 5
A
M1
M2
Object
Kernel


So what I have done over here is that I have included 2 modules in a class "A" and both the modules "M1" and "M2" have the "add" method. When I create an object of "A" and call "add" method, "M1's add" got executed. To confirm that "M2" is included in class "A", I also made a call to "A.ancestors".



Whats going on?

Any module gets injected as a direct superclass of whoever includes it, as mentioned over here: http://mousebender.wordpress.com/2007/11/18/including-a-module-in-ruby/. But again in which order? To figure out that, I read this: http://www.ruby-doc.org/core/classes/Module.src/M000438.html.


After reading about how does include works, I changed the code:

module M1

def M1.append_features(mod)
puts "append_features of M1"
end

def add(x, y)
x + y
end

end

module M2

def M2.append_features(mod)
puts "append_features of M2"
end

def add(x, y)
(x * x) + (y * y)
end
end

class A
include M1, M2
end

a = A.new
puts "Addition is: #{a.add(2,3)}"
puts A.ancestors

result:
append_features of M2
append_features of M1
NoMethodError: undefined method `add' for #
A
Object
Kernel


After implementing append_features method its clear that in which order modules get included. Obviously a.add is now failing because that method didn't got injected in the class "A". As one can see the result of a.ancestors, no M1 and M2 in the output. Lets remove the append_features implementation, now runtime will check "add" method first in class "A" if not found it will check in the immediate ancestor which is M1 and it will find it and M1.add will be executed.


Enjoy Ruby

Tuesday, September 13, 2011

Invalid date format in gemspec


Recently I faced a issue while executing bundle update on a ruby project. The gems that got updated were cucumber 1.0.4, gherkinn 2.4.18, webrobots etc. After this update I was getting "invalid date format in gemspec" with the path of the gem in the error description on firing many of the commands like running any rake task. After looking into the *.gemspec file those were corrupted and comparing them with the other ones those were proper, I found that date format was the issue. The wrong date values were having the time value like "00:00:00.00000Z" which was not required only the date part was required. Fixing the date values solved the problems related to date format, but after that I was getting this error "Illformed requirement ["# 0.8.4"]" for 1 of the gemspec file. I finally had no clew what was going on, I simply ran few commands that solved the problem:



  • gem update --system.

  • gem update bundler.

  • bundle update on the ruby project.


Hope this helps.

Monday, September 5, 2011

Another day fixing cucumber tests because of firefox update.


After updating to firefox 6 all of my cucumber tests stopped working. Basically none of the button and link clicks were working. After debugging for a while I figured out that for some reason "click_button" and "click_link" were silently failing. I then googled about the issue and found work around for it. Here it is:



  • Instead of click_button(button_text), first find the button by invoking find_button(button_text)

  • Then call button.native.send_keys([:return]), which will perform "Enter" key on the button.

  • Same will work with link as well.


I faced the issue with following system configuration:

  • Windows XP SP3

  • ruby 1.8.7

  • Firefox 6.0.1

  • cucumber 1.0.2

  • capybara 1.1.0

  • selenium-webdriver 2.5.0

Wednesday, August 24, 2011

Some of my findings while working with cucumber

Finding # 1


I was wondering how to use utf8 characters in the cucumber feature file.
For e.g When I click the "»" button
After searching for the problem I figured out the solution:



  • ".Feature" file should be saved in utf8 format.

  • "require 'cucumber/formatter/unicode'" needs to be added in support/env.rb.


Thats it, problem got solved.


Finding # 2


If an element is clicked from a page and that click opens a new window. To perform some actions on the new window.

Got the solution from:
http://qastuffs.blogspot.com/2010/10/testing-pop-up-windows-using-selenium.html


The solution however doesn't work with capybara, atleast for me it didn't worked. I replaced "response" with "page" and then it worked as shown below:


declare these functions in your web_steps.rb:
def switch_to_new_pop_up
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
end

def close_active_window
page.driver.browser.close
page.driver.browser.switch_to.window(page.driver.browser.window_handles[0])
end