Thursday, October 29, 2009

Finding the position of an element

To find the position of element <b>col_d</b> in the following xml:
1
2
3
4
5
6
7
<a>
<b>col_a</b>
<b>col_b</b>
<b>col_c</b>
<b>col_d</b>
<b>col_e</b>
</a>

U can probably use the following xpath:

count(//b[normalize-space(text())="col_d"]/preceding-sibling::b)+1

But it doesn't work for me in nokogiri, so what i did was:
1
2
doc = Nokogiri::XML(above_xml_string)
doc.xpath('//b[normalize-space(text())="col_d"]/preceding-sibling::b').count + 1

That's all !!

Wednesday, October 28, 2009

Finding Common Elements in Ruby Arrays

To find common elements in ruby arrays, use '|'. Eg.

%w{a b c d} | %w{c d e f}

# yields:

['c', 'd']


To find common elements in 2 dimensional array:

a1, a2, a3 = %w{a b c d}, %w{c d e f}, %w{d e f}
a = [a1, a2, a3]

a[1..-1].inject(a[0]) do |x, y|
x | y
end

# yields:

['d']


That's all !!

Getting the nearest xpath ancestor

Given the following xml fragment:
1
2
3
4
5
<a>
<a>
<b/>
</a>
<a>

To find the nearest ancestor <a/> w.r.t <b/>, i use:

//b/ancestor::a[1]

That's all !!

Tuesday, October 27, 2009

Renaming a screen window

To rename a screen window, just do:

'Ctrl+a' & 'Shift-a'.

Smart click_button

In previous post, i've mentioned abt webrat's smart click_link(), and do u know that click_button() is also quite smart, meaning:
1
<input type="image" alt="Update" />

Works as well if we do:
1
click_button("Update")

Nice ?!

Monday, October 26, 2009

Smart click_link()

WOW, just found out image within <a/> tag (see below):
1
2
3
<a class="image" href="...">
<img src="..." alt="Back"/>
</a>

Actually work for the following pair of:
1
2
# Step
Given I follow "Back"

&:
1
2
3
4
# Step definition
Given /^I follow "([^\"]+)"$/ do |link|
click_link(link) # plain vanilla webrat
end

Cool !!

Sunday, October 25, 2009

Workaround for Webrat's have_xpath()

I have the following xpath:

//td[normalize-space(text())='Peter']/parent::tr//label[normalize-space(text())="Present"]/@for

It is valid, yet webrat's have_xpath() kept complaining that it cannot find it. As a workaround, i did something like this in my cucumber:
1
2
hdoc = Nokogiri::HTML(response.body)
(field_id = hdoc.xpath(the_above_rejected_xpath)).first.should_not be_nil

That's all.

Webrat's have_xpath() in diff modes

have_xpath() behaves slightly differently in :selenium & :webrat mode.

From webrat's doc, we have:

Webrat::Matchers#have_xpath(expected, options = {}, &block)

And

Webrat::Selenium::Matchers::have_xpath(xpath)

If u are a xpath fan like me, there is no way u can conveniently switch between the 2 modes without getting bitten.

2 Way to Fetch a Node's Attribute Value

There are at least 2 ways to fetch a node's attribute value using xpath:

# Method 1

//a/b/@c

# Method 2

//a/b/attribute::c

Saturday, October 24, 2009

Fixing Gnome Tooltip

I've accidentally screwed up my gnome's config, as a result, all tooltips in my gnome desktop appear as meaningless black blocks. After some playing round & trial & error, i fixed the problem by replacing the following lines in ~/.gconf/desktop/gnome/interface/%gconf.xml:
1
2
tooltip_fg_color:#000000000000
tooltip_bg_color:#000000000000

With the more meaningful:
1
2
tooltip_fg_color:#000000000000
tooltip_bg_color:#ffffffffffff

That's all !!

Labels