From: Chris Nehren Date: Wed, 8 Dec 2010 01:24:34 +0000 (-0500) Subject: Initial checkin X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=676409e697322de1c2a35df741d7e0a2ef26fe70;p=scpubgit%2FTest-Harness-Selenium.git Initial checkin --- 676409e697322de1c2a35df741d7e0a2ef26fe70 diff --git a/lib/Test/Harness/Selenium.pm b/lib/Test/Harness/Selenium.pm new file mode 100644 index 0000000..defe00f --- /dev/null +++ b/lib/Test/Harness/Selenium.pm @@ -0,0 +1,59 @@ +package Test::Harness::Selenium; +use strictures 1; + +use File::Find; +use WWW::Selenium; +use HTML::TableExtract; +use IO::All; + +use Test::Builder; +BEGIN { + package Test::Builder; + + use Class::Method::Modifiers; + use ExtUtils::MakeMaker qw(prompt); + + if (!$ENV{AUTOMATED_TESTING}) { + around ok => sub { + my ($orig, $self) = (shift, shift); + my $res = $self->$orig(@_); + unless ($res) { + if ('y' eq prompt "Well that didn't work, did it. Bail out?", 'y') { + exit 255; + } + } + return $res; + }; + } +} + +sub test_directory { + my ($self) = @_; + my @tests = File::Find::Rule->file()->name('*.html')->in($self->{dir}); + $self->run_tests_for($_) for @tests; +} + +sub run_tests_for { + my ($self, $html_file) = @_; + my $rows = $self->get_rows_for($html_file); + my $src = WWW::Selenium->new( + host => $args->{host}, + port => $args->{port}, + browser_url => $args->{browser_url}, + ); + $src->run_test_table($rows); +} + +my $te = HTML::TableExtract->new; +sub get_rows_for { + my ($self, $html_file) = @_; + my $html = io($html_file)->all; + $te->parse($html); + my $table = ($te->tables)[0]; + my @rows = map { + [ map { $_ eq "\240" ? () : $_ } @$_ ] + } $table->rows; + return \@rows; +} + +1; diff --git a/selenium.txt b/selenium.txt new file mode 100644 index 0000000..d334ea4 --- /dev/null +++ b/selenium.txt @@ -0,0 +1,70 @@ +# (c) 2010 Matt S Trout (mst) +# same license as perl +# mail ideas, suggestions and flames to mst (at) shadowcat.co.uk +# proof of concept only +# no warranty, express or implied +# +# vnc4server :21 +# DISPLAY=:21 perl -MAlien::SeleniumRC -e 'Alien::SeleniumRC::start' + +use strict; +use warnings; +use Test::More qw(no_plan); +use Socialtext::WikiFixture::Selenese; +use HTML::TableExtract; + +# HTML ripped from http://seleniumhq.org/docs/05_selenium_rc.html#from-selense-to-a-program + +my $html = < + + +++++ + + + + + + + + + + + + + + + + + + + + +
open/ 
typeqselenium rc
clickAndWaitbtnG 
textLikeSearches related to selenium rc 
+ + +END + +my $te = HTML::TableExtract->new; + +$te->parse($html); + +my $table = ($te->tables)[0]; + +my @rows = map { + [ map { $_ eq "\240" ? () : $_ } @$_ ] +} $table->rows; + +$ENV{selenium_browser} = '*firefox /usr/lib/iceweasel/firefox-bin'; + +my $wf = Socialtext::WikiFixture::Selenese->new( + host => 'localhost', + port => $< + 6900, + browser_url => 'http://www.google.com' +); + +$wf->run_test_table(\@rows); diff --git a/selnotes.txt b/selnotes.txt new file mode 100644 index 0000000..841f555 --- /dev/null +++ b/selnotes.txt @@ -0,0 +1,101 @@ +use Test::Builder; +BEGIN { + package Test::Builder; + + use Class::Method::Modifiers; + use ExtUtils::MakeMaker qw(prompt); + + if (!$ENV{AUTOMATED_TESTING}) { + around ok => sub { + my ($orig, $self) = (shift, shift); + my $res = $self->$orig(@_); + unless ($res) { + if ('y' eq prompt "Well that didn't work, did it. Bail out?", 'y') { + exit 255; + } + } + return $res; + }; + } +} + +my $CAT_SERVER_PORT = 15000 + $<; +my $VNC_SERVER_PORT = 5900 + $<; +my $SEL_SERVER_PORT = 6900 + $<; + +sub gimme_table ($file) { + my $te = HTML::TableExtract->new; + $te->parse(do { local (@ARGV, $/) = ($file); <> }); + [ do { + my @r = map { + [ map { (!defined or $_ eq "\240") ? () : $_ } @$_ ] + } ($te->tables)[0]->rows; + shift @r; @r; + } + ] +} + +sub start_server ($port) { + my $server = Proc::Background->new( + { + die_upon_destroy => 1 + }, + 'script/app_server.pl', '-p', $port + ); + sleep 3; + $server; +} + +sub setup_selenium ($system, $port, $browser, $server_url) { + Socialtext::WikiFixture::Selenese->new( + selenium => do { my $s = Test::WWW::Selenium->new( + host => $system, + port => $port, + browser => $browser, + browser_url => $server_url, + ); $s->start; $s } + ); +} + +--- t/sel-local-lib/lib/perl5/Socialtext/WikiFixture/Selenese.pm 2007-06-21 18:28:37.000000000 -0400 ++++ t/sel-overrides/Socialtext/WikiFixture/Selenese.pm 2010-06-29 14:11:17.000000000 -0400 +@@ -128,7 +128,7 @@ + } + + # Try to guess _ok methods +- $command .= '_ok' if { map { $_ => 1 } qw(open type) }->{$command}; ++ $command .= '_ok' if { map { $_ => 1 } qw(open type click) }->{$command}; + $self->$command($opt1, $opt2); + } + +@@ -149,6 +149,12 @@ + $command = lc($1) . '_like'; + } + ++ # mst is guessing. ++ ++ if ($command =~ /^(?:assert|verify)_(\w+)$/) { ++ $command = lc($1) . '_is'; ++ } ++ + return $command; + } + +@@ -200,6 +206,16 @@ + $sel->wait_for_page_to_load_ok($self->{selenium_timeout}, @args); + } + ++sub select_and_wait { ++ my ($self, $opt1, $opt2) = @_; ++ my $sel = $self->{selenium}; ++ ++ my @args; ++ push @args, $opt2 if $opt2; ++ $sel->select_ok($opt1, @args); ++ $sel->wait_for_page_to_load_ok($self->{selenium_timeout}, @args); ++} ++ + =head2 text_present_like() + + Search entire body for given text +