Željko Filipin's Blog on Software and Testing

Test like you do not need the money.

Archive for the ‘Software’ Category

watir-nokogiri

with 4 comments

Random Croatian Countryside

Random Croatian Countryside

At Test Automation Bazaar Aliaksandr Ikhelis gave a lightning talk Watir-Webdriver is slow (did you know that?). He talked about the speed problem he had when watir-webdriver was locating elements via regular expression on big pages. (He also reported the issue at GitHub.)

Recently I have started working on a new project that had a similar speed problem, so I remembered Aliaksandr’s talk. I found a solution using Nokogiri.

For example, you have to iterate over all div elements that have id attribute starting with id. Then, you have to do something with the last element.

The page that looks like this:

...
<div id='id0'>0</div>
<div id='id1'>1</div>
...
<div id='id999'>999</div>
...

Watir code for iterating over elements and clicking the last one could look something like this.

browser.divs(id: /^id/).each do |div|
  div.click if div.id == "id999"
end

You could do exactly the same thing using Watir and Nokogiri.

Nokogiri::HTML(browser.html).css("div[id ^= 'id']").each do |div|
  browser.element(css: div.css_path).click if div["id"] == "id999"
end

The speed improvements is noticeable. For example, this script will click the last div using Nokogiri in about 0.2 seconds and using Watir in about 15 seconds.

require "watir-webdriver"
browser = Watir::Browser.new :firefox

folder = Dir.pwd
browser.goto "file://#{folder}/div.html"

require "benchmark"
require "nokogiri"

Benchmark.bm do |x|
  x.report("nokogiri") do
    Nokogiri::HTML(browser.html).css("div[id ^= 'id']").each do |div|
      if div["id"] == "id999"
        css = div.css_path
        browser.element(css: css).wd.location_once_scrolled_into_view
        browser.element(css: css).flash
      end
    end
  end
end

Benchmark.bm do |x|
  x.report("watir") do
    browser.divs(id: /^id/).each do |div|
      if div.id == "id999"
        div.wd.location_once_scrolled_into_view
        div.flash
      end
    end
  end
end

browser.close

Output:

          user       system     total    real
nokogiri  0.040000   0.010000   0.050000 (  0.219716)
watir     1.980000   0.310000   2.290000 ( 15.309858)

There is a way to speed things up using just the Watir API, but I wanted to provide an example code if Watir API is sometimes too slow for your taste.

You can find more code at watir-nokogiri.

Written by Željko Filipin

May 9th, 2012 at 2:39 pm

Posted in Software

Zagreb STC MeetUp #2

with 2 comments

Yesterday I was at Zagreb STC MeetUp #2. It was organized by Karlo Šmid at Ericsson Nikola Tesla.

After the first meetup (which was great) I thought the second one could be only worse. But to my great surprise, it was even better than the first one!

About ten of us appeared. Pizza was already there, so we started talking informally while eating pizza. (We even had a snack and refreshment sponsor, Calyx.)

Karlo brought a few books, I brought a few and we organized a little library. I think I got a pretty good deal, I borrowed a few good books:

(Book reviews coming soon.)

While we were still eating pizza, Karlo had a talk about The Grinder, a Java Load Testing Framework. After that I talked about Watir (automated testing that doesn’t hurt), Cucumber (making BDD fun) and Sikuli (automate anything you see) and how I use them together.

A really important part of every meetup are informal conversations in pairs or small groups, and we also had plenty of those.

A really good meetup. If you have a chance to attend the next one, I would highly recommend it.

Written by Željko Filipin

January 13th, 2012 at 1:25 pm

Posted in Events,Testing

Tagged with

The Best Thank You Note from a Reader of My Watir Book

with 7 comments

The book
Feedback like this makes me want to spend even more of my time writing the Watir Book and giving it away for free*.

Thanks for wasting a bunch of my time. Next time, before you publish something you might want to check that it works. I’ve spent 5 hours trying to get this shit to run and I can’t get the dev kit (ruby dk.rb install) to install. A search indicates that I’m not the only one having this issue. WHat a BUSH operation: from Ruby to Watir to YOU.

Please: take your page down — stop wasting peoples’ time.

On your BEST DAY, you’re an IDIOT.

* I am selling the book, but all content is available on GitHub for free. I have spent hundreds of hours on the book so far, and I have earned about $200. Do the math.


Update: I have replied after I updated the instructions.

Hi,

Thanks for the kind words. Since you have asked so nicely, I have just spent another afternoon and evening away from my family, updating instructions on how to install Watir:

https://github.com/zeljkofilipin/watirbook/blob/master/installation/windows.md

I did it just for you, for free of course, as always. Please notice new chapter, DevKit.

Regards,

Željko

Written by Željko Filipin

December 2nd, 2011 at 10:31 am

Posted in Books,Watir,Watir Book