Changeset 7279
- Timestamp:
- 04/28/10 07:58:58 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UnitTestContrib/test/unit/FoswikiSeleniumTestCase.pm
r7246 r7279 15 15 use Error qw( :try ); 16 16 use Encode; 17 use Scalar::Util qw( weaken ); 17 18 18 19 my $startWait; … … 41 42 my $browsers; 42 43 my @BrowserFixtureGroups; 44 my $currentTest; 43 45 44 46 my $debug = 0; 45 46 my $instance_count = 0;47 47 48 48 sub new { … … 50 50 my $this = $class->SUPER::new(@_); 51 51 52 $instance_count++; 52 if (defined $currentTest) { 53 $this->assert(0, 54 "There may only be one FoswikiSeleniumTestCase-based test\n" 55 . "running in each test process.\n" 56 . "Cannot run the $class test \n" 57 . "because the $currentTest is still running."); 58 } 59 $currentTest = $this; 60 weaken($currentTest); # Ensure the destructor is called at the normal time 53 61 54 62 $this->{selenium_timeout} = 30_000; # Same as WWW::Selenium's default value … … 63 71 END { 64 72 _shutDownSeleniumBrowsers() if $browsers; 73 } 74 75 sub DESTROY { 76 my $this = shift; 77 if (not defined($currentTest) or $currentTest != $this) { 78 $this->assert(0, 79 "Unexpected change of current test:" 80 . "Expected $this but found $currentTest"); 81 } 82 $this->SUPER::DESTROY if $this->can('SUPER::DESTROY'); 65 83 } 66 84 … … 130 148 $config{browser_url} ||= $Foswiki::cfg{DefaultUrlHost}; 131 149 132 $config{error_callback} = sub { $this->assert(0, join(' ', @_)); }; 150 # The error callback needs a reference to the current test 151 # object. There may be several test objects that use the 152 # selenium interface, so the error callback cannot be a 153 # closure (anonymous sub) that uses $this (because $this 154 # in a closure would always refers to the first test 155 # to run that is derived from FoswikiSeleniumTestCase). 156 # Instead, the error callback uses a static class variable 157 # that is set (and weakened) in the constructor. 158 $config{error_callback} = \&_errorCallback; 133 159 134 160 my $selenium = Test::WWW::Selenium->new( %config ); 135 161 if ($selenium) { 136 162 $browsers->{$browser} = $selenium; 163 } 164 else { 165 $this->assert(0, "Could not create a Test::WWW::Selenium object for $browser"); 137 166 } 138 167 } … … 152 181 153 182 return $browsers; 183 } 184 185 sub _errorCallback { 186 if ($currentTest) { 187 $currentTest->assert(0, join(' ', @_)); 188 } 189 else { 190 die "A Test::WWW::Selenium class reported an error, " 191 . "but the associated test-case object has " 192 . "already been destroyed. The error is:\n" 193 . join(' ', @_); 194 } 154 195 } 155 196
Note: See TracChangeset
for help on using the changeset viewer.
