Skip to content

Commit

Permalink
Item1326: Shut down the browsers explicitly before exiting - the "Unk…
Browse files Browse the repository at this point in the history
…nown encoding 'utf8'" errors are caused by the global destrution of objects when the script ends. The order of destruction then is not well-defined, so sometimes the uft8 encoding data is cleaned up before the selenium client.

Also make the failure-reporting tests work. Well - mostly work. open_ok does not report a failure on IE8.


git-svn-id: http://svn.foswiki.org/trunk@7220 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelTempest authored and MichaelTempest committed Apr 19, 2010
1 parent 15f2932 commit f50b031
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 71 deletions.
143 changes: 78 additions & 65 deletions UnitTestContrib/test/unit/FoswikiSeleniumTestCase.pm
Expand Up @@ -18,108 +18,121 @@ use Encode;
my $useSeleniumError;
my $browsers;

my $debug = 0;

my $instance_count = 0;

sub new {
my $class = shift;
my $this = $class->SUPER::new(@_);

$instance_count++;

$this->{useSeleniumError} = $this->_loadSeleniumInterface;
$this->{seleniumBrowsers} = $this->_loadSeleniumBrowsers;

return $this;
}

END {
_shutDownSeleniumBrowsers() if $browsers;
}

sub list_tests {
my ( $this, $suite ) = @_;
my @set = $this->SUPER::list_tests($suite);

if ($this->{useSeleniumError}) {
print STDERR "Cannot run Selenium-based tests: $this->{useSeleniumError}";
return;
}
return @set;
return;
}
return @set;
}

sub fixture_groups {
my ( $this, $suite ) = @_;

if ($this->{useSeleniumError}) {
print STDERR "Cannot run Selenium-based tests: $this->{useSeleniumError}";
return;
}

my @groups;

for my $browser (keys %{ $this->{seleniumBrowsers} })
{
my $onBrowser = "on$browser";
push @groups, $onBrowser;
my $selenium = $this->{seleniumBrowsers}->{$browser};
eval "sub $onBrowser { my \$this = shift; \$this->{selenium} = \$selenium; }";
die $@ if $@;
}
return \@groups;
return;
}

my @groups;

for my $browser (keys %{ $this->{seleniumBrowsers} })
{
my $onBrowser = "on$browser";
push @groups, $onBrowser;
my $selenium = $this->{seleniumBrowsers}->{$browser};
eval "sub $onBrowser { my \$this = shift; \$this->{browser} = \$browser; \$this->{selenium} = \$selenium; }";
die $@ if $@;
}
return \@groups;
}

sub _loadSeleniumInterface {
my $this = shift;
my $this = shift;

return $useSeleniumError if defined $useSeleniumError;
return $useSeleniumError if defined $useSeleniumError;

eval "use Test::WWW::Selenium";
if ($@) {
$useSeleniumError = $@;
$useSeleniumError =~ s/\(\@INC contains:.*$//s;
#$this->expect_failure();
#$this->annotate("CANNOT RUN SELENIUM TESTS: $this->{use_selenium_error}");
}
else
{
$useSeleniumError = '';
}
return $useSeleniumError;
else
{
$useSeleniumError = '';
}
return $useSeleniumError;
}

sub _loadSeleniumBrowsers {
my $this = shift;

return $browsers if $browsers;

$browsers = {};

unless ($this->{useSeleniumError}) {
if ($Foswiki::cfg{UnitTestContrib}{SeleniumRc}) {
for my $browser (keys %{ $Foswiki::cfg{UnitTestContrib}{SeleniumRc} }) {
my %config = %{ $Foswiki::cfg{UnitTestContrib}{SeleniumRc}{$browser} };
$config{host} ||= 'localhost';
$config{port} ||= 4444;
$config{browser} ||= '*firefox';
$config{browser_url} ||= $Foswiki::cfg{DefaultUrlHost};

$config{error_callback} = sub { $this->assert(0, join(' ', @_)); };

my $selenium = Test::WWW::Selenium->new( %config );
if ($selenium) {
$browsers->{$browser} = $selenium;
}
else {
print STDERR "Could not create Test::WWW::Selenium object for \$Foswiki::cfg{UnitTestContrib}{SeleniumRc}{$browser}\n";
}
}
}
}
if (keys %{ $browsers }) {
eval "use Test::Builder";
die $@ if $@;
my $test = Test::Builder->new;
$test->no_plan();
$test->no_diag(1);
$test->no_ending(1);
my $testOutput = '';
$test->output(\$testOutput );
}

return $browsers;
my $this = shift;

return $browsers if $browsers;

$browsers = {};

unless ($this->{useSeleniumError}) {
if ($Foswiki::cfg{UnitTestContrib}{SeleniumRc}) {
for my $browser (keys %{ $Foswiki::cfg{UnitTestContrib}{SeleniumRc} }) {
my %config = %{ $Foswiki::cfg{UnitTestContrib}{SeleniumRc}{$browser} };
$config{host} ||= 'localhost';
$config{port} ||= 4444;
$config{browser} ||= '*firefox';
$config{browser_url} ||= $Foswiki::cfg{DefaultUrlHost};

$config{error_callback} = sub { $this->assert(0, join(' ', @_)); };

my $selenium = Test::WWW::Selenium->new( %config );
if ($selenium) {
$browsers->{$browser} = $selenium;
}
}
}
}
if (keys %{ $browsers }) {
eval "use Test::Builder";
die $@ if $@;
my $test = Test::Builder->new;
$test->reset();
$test->no_plan();
$test->no_diag(1);
$test->no_ending(1);
my $testOutput = '';
$test->output(\$testOutput );
}

return $browsers;
}

sub _shutDownSeleniumBrowsers {
for my $browser (values %$browsers) {
print STDERR "Shutting down $browser\n" if $debug;
$browser->stop();
}
undef $browsers;
}

1;
14 changes: 8 additions & 6 deletions UnitTestContrib/test/unit/SeleniumConfigTests.pm
Expand Up @@ -32,17 +32,19 @@ sub verify_SeleniumRc_config {

sub verify_SeleniumRc_ok_failure_reporting {
my $this = shift;
$this->expect_failure();
$this->annotate("Testing that failures from Test::WWW::Selenium find their way to the Unit::TestCase infrastructure");
$this->{selenium}->open_ok( Foswiki::Func::getScriptUrl( 'Milkshakes', 'BronzeMoth', 'bananaFlavour') );
eval {
$this->{selenium}->open_ok( Foswiki::Func::getScriptUrl( 'Milkshakes', 'BronzeMoth', 'bananaFlavour') );
};
$this->assert_matches( "^\nopen, ", $@, "Expected an exception");
}

sub verify_SeleniumRc_like_failure_reporting {
my $this = shift;
$this->{selenium}->open_ok( Foswiki::Func::getScriptUrl( $this->{test_web}, $this->{test_topic}, 'view') );
$this->expect_failure();
$this->annotate("Testing that failures from Test::WWW::Selenium find their way to the Unit::TestCase infrastructure");
$this->{selenium}->title_like( qr/There is no way that this would ever find its way into the title of web page. That would be simply insane!/ );
eval {
$this->{selenium}->title_like( qr/There is no way that this would ever find its way into the title of web page. That would be simply insane!/ );
};
$this->assert_matches( "^\nget_title, ", $@, "Expected an exception");
}

1;

0 comments on commit f50b031

Please sign in to comment.