I ran into a situation today where i wanted to scan through a page and return a table based on the table matching 3 attributes
Before:
browser.tables.find do |table|
table.class_name == 'my_class_name' and
table.cell(:class, 'class_1_name').text == 'some_text_1' and
table.cell(:class, 'class_2_name').text == 'some_text_2'
end
This was taking about 3minutes as there were about 86tables on this page under test. I was worried but the tables on this page was gonna increase with time which meant the time for this stage of the test was bound to increase.
In my search for how to search for a single table using multiple attributes:
I found this:
After:
browser.table(:class => 'my_class_name', :text => /#{'some_text_1}/, :text => /#{'some_text_2}/)
Believe it or not, i got my test time reduced to 3secs, awesome isnt it?
Enjoy!!!!!!
My blog has moved!
You should be automatically redirected in 5 seconds. If not, visit
http://samueladesoga.wordpress.com
and update your bookmarks.
Showing posts with label watir. Show all posts
Showing posts with label watir. Show all posts
Friday, 25 June 2010
Wednesday, 10 March 2010
Blank cells in step tables in cucumber 0.3.11 is represented as nil
In my current job, I have been doing a lot of acceptance test using Cucumber, Watir and RSpec, obvious all in Ruby. I have been using Cucumber 0.6.x, in which blank cells in the step table are represented by empty string (""). This was okay for me until today when i had to use a previous version of Cucumber 0.3.11 due to reasons including compatibility with other projects in the CI build. And my tests start failing because blank cells are represented as nil.
There was fix put in the current release of cucumber to change blanks to be represented as empty and not as nil(for whatever reason, which i don't really care, as both nil and "" has got its own arguments.)
Some code to explain this, Assuming i have a step table as below:
Given that i have the following detail in my app
|Header1|Header2|Header3|Header4|
|Body1 |Body2 |Body3 | |
Notice that the last column of the second row is blank, this would be represented as nil in cucumber 0.3.11 while it is represented as empty string "" in cucumber 0.6.x
In my step definition file, I have fixed this by converting every nil value to empty ("")
Given /have the following detail/ do |table|
table.rows.each do |row|
row.map! {|value| convert_nil_to_empty_string(value)}
# do some other stuff with row as every nil object has been converted to empty string
# The map! method for Array, allow you to invoke the block for the array and it amends
# the existing array
end
end
The method which i call to convert nil to empty is below:
def convert_nil_to_empty_string(test_string)
if (test_string.nil?)
return ""
else
return test_string
end
end
There might have been a better way to do this but this surely worked for me in this scenario and the reason i decided to use a method was that the map! method would only allow me use the variable "value" only once in the block. I am sure there are some rubyist out there, that could advice me on some shorthand for this ....... Hope it helps you too
There was fix put in the current release of cucumber to change blanks to be represented as empty and not as nil(for whatever reason, which i don't really care, as both nil and "" has got its own arguments.)
Some code to explain this, Assuming i have a step table as below:
Given that i have the following detail in my app
|Header1|Header2|Header3|Header4|
|Body1 |Body2 |Body3 | |
Notice that the last column of the second row is blank, this would be represented as nil in cucumber 0.3.11 while it is represented as empty string "" in cucumber 0.6.x
In my step definition file, I have fixed this by converting every nil value to empty ("")
Given /have the following detail/ do |table|
table.rows.each do |row|
row.map! {|value| convert_nil_to_empty_string(value)}
# do some other stuff with row as every nil object has been converted to empty string
# The map! method for Array, allow you to invoke the block for the array and it amends
# the existing array
end
end
The method which i call to convert nil to empty is below:
def convert_nil_to_empty_string(test_string)
if (test_string.nil?)
return ""
else
return test_string
end
end
There might have been a better way to do this but this surely worked for me in this scenario and the reason i decided to use a method was that the map! method would only allow me use the variable "value" only once in the block. I am sure there are some rubyist out there, that could advice me on some shorthand for this ....... Hope it helps you too
Labels:
automated test,
BDD,
cucumber,
feature driven development,
QA,
rspec,
ruby,
test,
watir
Subscribe to:
Posts (Atom)