10 April 2011
WWW::Selenium is a good choice when you have to test your own web application or work with a webpage full of javascript.

well, but right now it's a pity that it doesn't support the API of Selenium 2.0. though it's still a good tool to play with.

below is some notes that may help when you meet the issues like me.

1. the tests may fail but you don't need worry about it.
actually I never made the tests passing or SYNOPSIS working because it uses wait_for_page_to_load and Google. you can try a Yahoo! search and it works.
one suggestion to avoid the timeout error from wait_for_page_to_load is using wait_for_element_present. wait_for_element_present works much more better in my side.

2. get image stored
big thanks to stackoverflow, here is an example code.

my $js = <<'JS';
var img = this.browserbot.findElement("//img[contains(@src,'captchaData')]");

var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;

var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);

var dataURL = canvas.toDataURL("image/png");

dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
JS

my $val = $sel->get_eval($js);
my $bin_data = decode_base64($val); # from use MIME::Base64;
open(my $fh, '>', "$Bin/test.jpg");
binmode($fh);
print $fh $bin_data;
close($fh);

I'm using the firefox 3.6 and it works pretty well. canvas.toDataURL produces encoded base64 string and you can decode it then save it.

The solution using javascript to get image stored is pretty interesting.

3. how to get the element HTML?
well, this module doesn't return javascript DOM HTML to you. instead, you need always use js to get what you want. Yes. javascript is powerful.

my $html = $sel->get_eval('this.browserbot.findElement("id=whatever").innerHTML');

anyway, Selenium is somehow slow. I'm trying to use WWW::Mechanize::Firefox but I don't have X in my server right now. (tried to install the mozrepl plugin through command line but no luck yet)

I'll keep you posted. Thanks.


blog comments powered by Disqus