Driver.pm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. use strict;
  2. use warnings;
  3. package Test::Selenium::Remote::Driver;
  4. use parent 'Selenium::Remote::Driver';
  5. # ABSTRACT: Useful testing subclass for Selenium::Remote::Driver
  6. use Test::Selenium::Remote::WebElement;
  7. use Test::More;
  8. use Test::Builder;
  9. use Test::LongString;
  10. use IO::Socket;
  11. our $AUTOLOAD;
  12. my $Test = Test::Builder->new;
  13. $Test->exported_to(__PACKAGE__);
  14. my %comparator = (
  15. is => 'is_eq',
  16. isnt => 'isnt_eq',
  17. like => 'like',
  18. unlike => 'unlike',
  19. );
  20. my $comparator_keys = join '|', keys %comparator;
  21. # These commands don't require a locator
  22. my %no_locator = map { $_ => 1 }
  23. qw( alert_text current_window_handle current_url
  24. title page_source body location path);
  25. sub no_locator {
  26. my $self = shift;
  27. my $method = shift;
  28. return $no_locator{$method};
  29. }
  30. sub AUTOLOAD {
  31. my $name = $AUTOLOAD;
  32. $name =~ s/.*:://;
  33. return if $name eq 'DESTROY';
  34. my $self = $_[0];
  35. my $sub;
  36. if ($name =~ /(\w+)_($comparator_keys)$/i) {
  37. my $getter = "get_$1";
  38. my $comparator = $comparator{lc $2};
  39. # make a subroutine that will call Test::Builder's test methods
  40. # with driver data from the getter
  41. if ($self->no_locator($1)) {
  42. $sub = sub {
  43. my( $self, $str, $name ) = @_;
  44. diag "Test::Selenium::Remote::Driver running no_locator $getter (@_[1..$#_])"
  45. if $self->{verbose};
  46. $name = "$getter, '$str'"
  47. if $self->{default_names} and !defined $name;
  48. no strict 'refs';
  49. my $rc = $Test->$comparator( $self->$getter, $str, $name );
  50. if (!$rc && $self->error_callback) {
  51. &{$self->error_callback}($name);
  52. }
  53. return $rc;
  54. };
  55. }
  56. else {
  57. $sub = sub {
  58. my( $self, $locator, $str, $name ) = @_;
  59. diag "Test::Selenium::Remote::Driver running with locator $getter (@_[1..$#_])"
  60. if $self->{verbose};
  61. $name = "$getter, $locator, '$str'"
  62. if $self->{default_names} and !defined $name;
  63. no strict 'refs';
  64. no strict 'refs';
  65. my $rc = $Test->$comparator( $self->$getter($locator), $str, $name );
  66. if (!$rc && $self->error_callback) {
  67. &{$self->error_callback}($name);
  68. }
  69. return $rc;
  70. };
  71. }
  72. }
  73. elsif ($name =~ /(\w+?)_?ok$/i) {
  74. my $cmd = $1;
  75. # make a subroutine for ok() around the selenium command
  76. $sub = sub {
  77. my( $self, $arg1, $arg2, $name ) = @_;
  78. if ($self->{default_names} and !defined $name) {
  79. $name = $cmd;
  80. $name .= ", $arg1" if defined $arg1;
  81. $name .= ", $arg2" if defined $arg2;
  82. }
  83. diag "Test::Selenium::Remote::Driver running _ok $cmd (@_[1..$#_])"
  84. if $self->{verbose};
  85. local $Test::Builder::Level = $Test::Builder::Level + 1;
  86. my $rc = '';
  87. eval { $rc = $self->$cmd( $arg1, $arg2 ) };
  88. die $@ if $@ and $@ =~ /Can't locate object method/;
  89. diag($@) if $@;
  90. $rc = ok( $rc, $name );
  91. if (!$rc && $self->error_callback) {
  92. &{$self->error_callback}($name);
  93. }
  94. return $rc;
  95. };
  96. }
  97. # jump directly to the new subroutine, avoiding an extra frame stack
  98. if ($sub) {
  99. no strict 'refs';
  100. *{$AUTOLOAD} = $sub;
  101. goto &$AUTOLOAD;
  102. }
  103. else {
  104. # try to pass through to Selenium::Remote::Driver
  105. my $sel = 'Selenium::Remote::Driver';
  106. my $sub = "${sel}::${name}";
  107. goto &$sub if exists &$sub;
  108. my ($package, $filename, $line) = caller;
  109. die qq(Can't locate object method "$name" via package ")
  110. . __PACKAGE__
  111. . qq(" (also tried "$sel") at $filename line $line\n);
  112. }
  113. }
  114. sub error_callback {
  115. my ($self, $cb) = @_;
  116. if (defined($cb)) {
  117. $self->{error_callback} = $cb;
  118. }
  119. return $self->{error_callback};
  120. }
  121. =head2 new ( %opts )
  122. This will create a new Test::Selenium::Remote::Driver object, which subclasses
  123. L<Selenium::Remote::Driver>. This subclass provides useful testing
  124. functions. It is modeled on L<Test::WWW::Selenium>.
  125. Environment vars can be used to specify options to pass to
  126. L<Selenium::Remote::Driver>. ENV vars are prefixed with C<TWD_>.
  127. Set the Selenium server address with C<$TWD_HOST> and C<$TWD_PORT>.
  128. Pick which browser is used using the C<$TWD_BROWSER>, C<$TWD_VERSION>,
  129. C<$TWD_PLATFORM>, C<$TWD_JAVASCRIPT>, C<$TWD_EXTRA_CAPABILITIES>.
  130. See L<Selenium::Driver::Remote> for the meanings of these options.
  131. =cut
  132. sub new {
  133. my ($class, %p) = @_;
  134. for my $opt (qw/remote_server_addr port browser_name version platform
  135. javascript auto_close extra_capabilities/) {
  136. $p{$opt} ||= $ENV{ 'TWD_' . uc($opt) };
  137. }
  138. $p{browser_name} ||= $ENV{TWD_BROWSER}; # ykwim
  139. $p{remote_server_addr} ||= $ENV{TWD_HOST}; # ykwim
  140. $p{webelement_class} ||= 'Test::Selenium::Remote::WebElement';
  141. my $self = $class->SUPER::new(%p);
  142. $self->{verbose} = $p{verbose};
  143. return $self;
  144. }
  145. =head2 server_is_running( $host, $port )
  146. Returns true if a Selenium server is running. The host and port
  147. parameters are optional, and default to C<localhost:4444>.
  148. Environment vars C<TWD_HOST> and C<TWD_PORT> can also be used to
  149. determine the server to check.
  150. =cut
  151. sub server_is_running {
  152. my $class_or_self = shift;
  153. my $host = $ENV{TWD_HOST} || shift || 'localhost';
  154. my $port = $ENV{TWD_PORT} || shift || 4444;
  155. return ($host, $port) if IO::Socket::INET->new(
  156. PeerAddr => $host,
  157. PeerPort => $port,
  158. );
  159. return;
  160. }
  161. =head2 $twd->content_like( $regex [, $desc ] )
  162. $twd->content_like( $regex [, $desc ] )
  163. $twd->content_like( [$regex_1, $regex_2] [, $desc ] )
  164. Tells if the content of the page matches I<$regex>. If an arrayref of regex's
  165. are provided, one 'test' is run for each regex against the content of the
  166. current page.
  167. A default description of 'Content is like "$regex"' will be provided if there
  168. is no description.
  169. =cut
  170. sub content_like {
  171. my $self = shift;
  172. my $regex = shift;
  173. my $desc = shift;
  174. local $Test::Builder::Level = $Test::Builder::Level + 1;
  175. my $content = $self->get_page_source();
  176. if (not ref $regex eq 'ARRAY') {
  177. my $desc = qq{Content is like "$regex"} if (not defined $desc);
  178. return like_string($content , $regex, $desc );
  179. }
  180. elsif (ref $regex eq 'ARRAY') {
  181. for my $re (@$regex) {
  182. my $desc = qq{Content is like "$re"} if (not defined $desc);
  183. like_string($content , $re, $desc );
  184. }
  185. }
  186. }
  187. =head2 $twd->content_unlike( $regex [, $desc ] )
  188. $twd->content_unlike( $regex [, $desc ] )
  189. $twd->content_unlike( [$regex_1, $regex_2] [, $desc ] )
  190. Tells if the content of the page does NOT match I<$regex>. If an arrayref of regex's
  191. are provided, one 'test' is run for each regex against the content of the
  192. current page.
  193. A default description of 'Content is unlike "$regex"' will be provided if there
  194. is no description.
  195. =cut
  196. sub content_unlike {
  197. my $self = shift;
  198. my $regex = shift;
  199. my $desc = shift;
  200. local $Test::Builder::Level = $Test::Builder::Level + 1;
  201. my $content = $self->get_page_source();
  202. if (not ref $regex eq 'ARRAY') {
  203. my $desc = qq{Content is unlike "$regex"} if (not defined $desc);
  204. return unlike_string($content , $regex, $desc );
  205. }
  206. elsif (ref $regex eq 'ARRAY') {
  207. for my $re (@$regex) {
  208. my $desc = qq{Content is unlike "$re"} if (not defined $desc);
  209. unlike_string($content , $re, $desc );
  210. }
  211. }
  212. }
  213. =head2 $twd->text_like( $regex [, $desc ] )
  214. $twd->text_like( $regex [, $desc ] )
  215. $twd->text_like( [$regex_1, $regex_2] [, $desc ] )
  216. Tells if the text of the page (as returned by C<< get_body() >>) matches
  217. I<$regex>. If an arrayref of regex's are provided, one 'test' is run for each
  218. regex against the content of the current page.
  219. A default description of 'Content is like "$regex"' will be provided if there
  220. is no description.
  221. To also match the HTML see, C<< content_unlike() >>.
  222. =cut
  223. sub text_like {
  224. my $self = shift;
  225. my $regex = shift;
  226. my $desc = shift;
  227. local $Test::Builder::Level = $Test::Builder::Level + 1;
  228. my $text = $self->get_body();
  229. if (not ref $regex eq 'ARRAY') {
  230. my $desc = qq{Text is like "$regex"} if (not defined $desc);
  231. return like_string($text , $regex, $desc );
  232. }
  233. elsif (ref $regex eq 'ARRAY') {
  234. for my $re (@$regex) {
  235. my $desc = qq{Text is like "$re"} if (not defined $desc);
  236. like_string($text , $re, $desc );
  237. }
  238. }
  239. }
  240. =head2 $twd->text_unlike( $regex [, $desc ] )
  241. $twd->text_unlike( $regex [, $desc ] )
  242. $twd->text_unlike( [$regex_1, $regex_2] [, $desc ] )
  243. Tells if the text of the page (as returned by C<< get_body() >>)
  244. does NOT match I<$regex>. If an arrayref of regex's
  245. are provided, one 'test' is run for each regex against the content of the
  246. current page.
  247. A default description of 'Text is unlike "$regex"' will be provided if there
  248. is no description.
  249. To also match the HTML see, C<< content_unlike() >>.
  250. =cut
  251. sub text_unlike {
  252. my $self = shift;
  253. my $regex = shift;
  254. my $desc = shift;
  255. local $Test::Builder::Level = $Test::Builder::Level + 1;
  256. my $text = $self->get_body();
  257. if (not ref $regex eq 'ARRAY') {
  258. my $desc = qq{Text is unlike "$regex"} if (not defined $desc);
  259. return unlike_string($text , $regex, $desc );
  260. }
  261. elsif (ref $regex eq 'ARRAY') {
  262. for my $re (@$regex) {
  263. my $desc = qq{Text is unlike "$re"} if (not defined $desc);
  264. unlike_string($text , $re, $desc );
  265. }
  266. }
  267. }
  268. #####
  269. =head2 $twd->content_contains( $str [, $desc ] )
  270. $twd->content_contains( $str [, $desc ] )
  271. $twd->content_contains( [$str_1, $str_2] [, $desc ] )
  272. Tells if the content of the page contains I<$str>. If an arrayref of strngs's
  273. are provided, one 'test' is run for each string against the content of the
  274. current page.
  275. A default description of 'Content contains "$str"' will be provided if there
  276. is no description.
  277. =cut
  278. sub content_contains {
  279. my $self = shift;
  280. my $str = shift;
  281. my $desc = shift;
  282. local $Test::Builder::Level = $Test::Builder::Level + 1;
  283. my $content = $self->get_page_source();
  284. if (not ref $str eq 'ARRAY') {
  285. my $desc = qq{Content contains "$str"} if (not defined $desc);
  286. return contains_string($content , $str, $desc );
  287. }
  288. elsif (ref $str eq 'ARRAY') {
  289. for my $s (@$str) {
  290. my $desc = qq{Content contains "$s"} if (not defined $desc);
  291. contains_string($content , $s, $desc );
  292. }
  293. }
  294. }
  295. =head2 $twd->content_lacks( $str [, $desc ] )
  296. $twd->content_lacks( $str [, $desc ] )
  297. $twd->content_lacks( [$str_1, $str_2] [, $desc ] )
  298. Tells if the content of the page does NOT contain I<$str>. If an arrayref of strings
  299. are provided, one 'test' is run for each string against the content of the
  300. current page.
  301. A default description of 'Content lacks "$str"' will be provided if there
  302. is no description.
  303. =cut
  304. sub content_lacks {
  305. my $self = shift;
  306. my $str = shift;
  307. my $desc = shift;
  308. local $Test::Builder::Level = $Test::Builder::Level + 1;
  309. my $content = $self->get_page_source();
  310. if (not ref $str eq 'ARRAY') {
  311. my $desc = qq{Content lacks "$str"} if (not defined $desc);
  312. return lacks_string($content , $str, $desc );
  313. }
  314. elsif (ref $str eq 'ARRAY') {
  315. for my $s (@$str) {
  316. my $desc = qq{Content lacks "$s"} if (not defined $desc);
  317. lacks_string($content , $s, $desc );
  318. }
  319. }
  320. }
  321. =head2 $twd->text_contains( $str [, $desc ] )
  322. $twd->text_contains( $str [, $desc ] )
  323. $twd->text_contains( [$str_1, $str_2] [, $desc ] )
  324. Tells if the text of the page (as returned by C<< get_body() >>) contains
  325. I<$str>. If an arrayref of strings are provided, one 'test' is run for each
  326. regex against the content of the current page.
  327. A default description of 'Text contains "$str"' will be provided if there
  328. is no description.
  329. To also match the HTML see, C<< content_uncontains() >>.
  330. =cut
  331. sub text_contains {
  332. my $self = shift;
  333. my $str = shift;
  334. my $desc = shift;
  335. local $Test::Builder::Level = $Test::Builder::Level + 1;
  336. my $text = $self->get_body();
  337. if (not ref $str eq 'ARRAY') {
  338. my $desc = qq{Text contains "$str"} if (not defined $desc);
  339. return contains_string($text , $str, $desc );
  340. }
  341. elsif (ref $str eq 'ARRAY') {
  342. for my $s (@$str) {
  343. my $desc = qq{Text contains "$s"} if (not defined $desc);
  344. contains_string($text , $s, $desc );
  345. }
  346. }
  347. }
  348. =head2 $twd->text_lacks( $str [, $desc ] )
  349. $twd->text_lacks( $str [, $desc ] )
  350. $twd->text_lacks( [$str_1, $str_2] [, $desc ] )
  351. Tells if the text of the page (as returned by C<< get_body() >>)
  352. does NOT contain I<$str>. If an arrayref of strings
  353. are provided, one 'test' is run for each regex against the content of the
  354. current page.
  355. A default description of 'Text is lacks "$str"' will be provided if there
  356. is no description.
  357. To also match the HTML see, C<< content_lacks() >>.
  358. =cut
  359. sub text_lacks {
  360. my $self = shift;
  361. my $str = shift;
  362. my $desc = shift;
  363. local $Test::Builder::Level = $Test::Builder::Level + 1;
  364. my $text = $self->get_body();
  365. if (not ref $str eq 'ARRAY') {
  366. my $desc = qq{Text is lacks "$str"} if (not defined $desc);
  367. return lacks_string($text , $str, $desc );
  368. }
  369. elsif (ref $str eq 'ARRAY') {
  370. for my $s (@$str) {
  371. my $desc = qq{Text is lacks "$s"} if (not defined $desc);
  372. lacks_string($text , $s, $desc );
  373. }
  374. }
  375. }
  376. 1;
  377. __END__
  378. =head1 NOTES
  379. This module was forked from Test::WebDriver 0.01.
  380. For Best Practice - I recommend subclassing Test::Selenium::Remote::Driver for your application,
  381. and then refactoring common or app specific methods into MyApp::WebDriver so that
  382. your test files do not have much duplication. As your app changes, you can update
  383. MyApp::WebDriver rather than all the individual test files.
  384. =head1 AUTHORS
  385. =over 4
  386. =item *
  387. Created by: Luke Closs <lukec@cpan.org>, but inspired by
  388. L<Test::WWW::Selenium> and its authors.
  389. =back
  390. =head1 CONTRIBUTORS
  391. This work was sponsored by Prime Radiant, Inc. Mark Stosberg <mark@stosberg.com> also contributed.
  392. =head1 COPYRIGHT AND LICENSE
  393. Copyright (c) 2012 Prime Radiant, Inc.
  394. This program is free software; you can redistribute it and/or
  395. modify it under the same terms as Perl itself.