Driver.pm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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 = shift;
  78. my $name = pop;
  79. my ($arg1, $arg2) = @_;
  80. if ($self->{default_names} and !defined $name) {
  81. $name = $cmd;
  82. $name .= ", $arg1" if defined $arg1;
  83. $name .= ", $arg2" if defined $arg2;
  84. }
  85. diag "Test::Selenium::Remote::Driver running _ok $cmd (@_[1..$#_])"
  86. if $self->{verbose};
  87. local $Test::Builder::Level = $Test::Builder::Level + 1;
  88. my $rc = '';
  89. eval { $rc = $self->$cmd( $arg1, $arg2 ) };
  90. die $@ if $@ and $@ =~ /Can't locate object method/;
  91. diag($@) if $@;
  92. $rc = ok( $rc, $name );
  93. if (!$rc && $self->error_callback) {
  94. &{$self->error_callback}($name);
  95. }
  96. return $rc;
  97. };
  98. }
  99. # jump directly to the new subroutine, avoiding an extra frame stack
  100. if ($sub) {
  101. no strict 'refs';
  102. *{$AUTOLOAD} = $sub;
  103. goto &$AUTOLOAD;
  104. }
  105. else {
  106. # try to pass through to Selenium::Remote::Driver
  107. my $sel = 'Selenium::Remote::Driver';
  108. my $sub = "${sel}::${name}";
  109. goto &$sub if exists &$sub;
  110. my ($package, $filename, $line) = caller;
  111. die qq(Can't locate object method "$name" via package ")
  112. . __PACKAGE__
  113. . qq(" (also tried "$sel") at $filename line $line\n);
  114. }
  115. }
  116. sub error_callback {
  117. my ($self, $cb) = @_;
  118. if (defined($cb)) {
  119. $self->{error_callback} = $cb;
  120. }
  121. return $self->{error_callback};
  122. }
  123. =head1 NAME
  124. Test::Selenium::Remote::Driver
  125. =head1 DESCRIPTION
  126. A subclass of L<Selenium::Remote::Driver>. which provides useful testing
  127. functions.
  128. This is an I<experimental> addition to the Selenium::Remote::Driver
  129. distribution, and some interfaces may change.
  130. =head1 Methods
  131. =head2 new ( %opts )
  132. This will create a new Test::Selenium::Remote::Driver object, which subclasses
  133. L<Selenium::Remote::Driver>. This subclass provides useful testing
  134. functions. It is modeled on L<Test::WWW::Selenium>.
  135. Environment vars can be used to specify options to pass to
  136. L<Selenium::Remote::Driver>. ENV vars are prefixed with C<TWD_>.
  137. ( After the old fork name, "Test::WebDriver" )
  138. Set the Selenium server address with C<$TWD_HOST> and C<$TWD_PORT>.
  139. Pick which browser is used using the C<$TWD_BROWSER>, C<$TWD_VERSION>,
  140. C<$TWD_PLATFORM>, C<$TWD_JAVASCRIPT>, C<$TWD_EXTRA_CAPABILITIES>.
  141. See L<Selenium::Driver::Remote> for the meanings of these options.
  142. =cut
  143. sub new {
  144. my ($class, %p) = @_;
  145. for my $opt (qw/remote_server_addr port browser_name version platform
  146. javascript auto_close extra_capabilities/) {
  147. $p{$opt} ||= $ENV{ 'TWD_' . uc($opt) };
  148. }
  149. $p{browser_name} ||= $ENV{TWD_BROWSER}; # ykwim
  150. $p{remote_server_addr} ||= $ENV{TWD_HOST}; # ykwim
  151. $p{webelement_class} ||= 'Test::Selenium::Remote::WebElement';
  152. my $self = $class->SUPER::new(%p);
  153. $self->{verbose} = $p{verbose};
  154. return $self;
  155. }
  156. =head2 server_is_running( $host, $port )
  157. Returns true if a Selenium server is running. The host and port
  158. parameters are optional, and default to C<localhost:4444>.
  159. Environment vars C<TWD_HOST> and C<TWD_PORT> can also be used to
  160. determine the server to check.
  161. =cut
  162. sub server_is_running {
  163. my $class_or_self = shift;
  164. my $host = $ENV{TWD_HOST} || shift || 'localhost';
  165. my $port = $ENV{TWD_PORT} || shift || 4444;
  166. return ($host, $port) if IO::Socket::INET->new(
  167. PeerAddr => $host,
  168. PeerPort => $port,
  169. );
  170. return;
  171. }
  172. =head1 Testing Methods
  173. The following testing methods are available. For
  174. more documentation, see the related test methods in L<Selenium::Remote::Driver>
  175. (And feel free to submit a patch to flesh out the documentation for these here).
  176. alert_text_is
  177. alert_text_isnt
  178. alert_text_like
  179. alert_text_unlike
  180. current_window_handle_is
  181. current_window_handle_isnt
  182. current_window_handle_like
  183. current_window_handle_unlike
  184. window_handles_is
  185. window_handles_isnt
  186. window_handles_like
  187. window_handles_unlike
  188. window_size_is
  189. window_size_isnt
  190. window_size_like
  191. window_size_unlike
  192. window_position_is
  193. window_position_isnt
  194. window_position_like
  195. window_position_unlike
  196. current_url_is
  197. current_url_isnt
  198. current_url_like
  199. current_url_unlike
  200. title_is
  201. title_isnt
  202. title_like
  203. title_unlike
  204. active_element_is
  205. active_element_isnt
  206. active_element_like
  207. active_element_unlike
  208. # Basically the same as 'content_like()', but content_like() supports multiple regex's.
  209. page_source_is
  210. page_source_isnt
  211. page_source_like
  212. page_source_unlike
  213. send_keys_to_active_element_ok
  214. send_keys_to_alert_ok
  215. send_keys_to_prompt_ok
  216. send_modifier_ok
  217. accept_alert_ok
  218. dismiss_alert_ok
  219. move_mouse_to_location_ok
  220. move_to_ok
  221. get_ok
  222. go_back_ok
  223. go_forward_ok
  224. add_cookie_ok
  225. get_page_source_ok
  226. find_element_ok($search_target)
  227. find_element_ok($search_target)
  228. find_elements_ok
  229. find_child_element_ok
  230. find_child_elements_ok
  231. compare_elements_ok
  232. click_ok
  233. double_click_ok
  234. =head2 $twd->type_ok($search_target, $keys, [, $desc ]);
  235. $twd->type_ok( $search_target, $keys [, $desc ] );
  236. Use L<Selenium::Remote::Driver/find_element> to resolve the C<$search_target>
  237. to a web element, and then type C<$keys> into it, providing an optional test
  238. label.
  239. Currently, other finders besides the default are not supported for C<type_ok()>.
  240. =cut
  241. sub type_ok {
  242. my $self = shift;
  243. my $locator = shift;
  244. my $keys = shift;
  245. my $desc = shift;
  246. return $self->find_element($locator)->send_keys_ok($keys,$desc);
  247. }
  248. =head2 $twd->find_element_ok($search_target [, $desc ]);
  249. $twd->find_element_ok( $search_target [, $desc ] );
  250. Returns true if C<$search_target> is successfully found on the page. L<$search_target>
  251. is passed to L<Selenium::Remote::Driver/find_element> using the C<default_finder>. See
  252. there for more details on the format. Currently, other finders besides the default are not supported
  253. for C<find_element_ok()>.
  254. =cut
  255. # Eventually, it would be nice to support other finds like Test::WWW::Selenium does, like this:
  256. # 'xpath=//foo', or 'css=.foo', etc.
  257. =head2 $twd->find_no_element_ok($search_target [, $desc ]);
  258. $twd->find_no_element_ok( $search_target [, $desc ] );
  259. Returns true if C<$search_target> is I<not> found on the page. L<$search_target>
  260. is passed to L<Selenium::Remote::Driver/find_element> using the C<default_finder>. See
  261. there for more details on the format. Currently, other finders besides the default are not supported
  262. for C<find_no_element_ok()>.
  263. =cut
  264. sub find_no_element_ok {
  265. my $self = shift;
  266. my $search_target = shift;
  267. my $desc = shift;
  268. local $Test::Builder::Level = $Test::Builder::Level +1;
  269. eval { $self->find_element($search_target) };
  270. ok((defined $@),$desc);
  271. }
  272. =head2 $twd->content_like( $regex [, $desc ] )
  273. $twd->content_like( $regex [, $desc ] )
  274. $twd->content_like( [$regex_1, $regex_2] [, $desc ] )
  275. Tells if the content of the page matches I<$regex>. If an arrayref of regex's
  276. are provided, one 'test' is run for each regex against the content of the
  277. current page.
  278. A default description of 'Content is like "$regex"' will be provided if there
  279. is no description.
  280. =cut
  281. sub content_like {
  282. my $self = shift;
  283. my $regex = shift;
  284. my $desc = shift;
  285. local $Test::Builder::Level = $Test::Builder::Level + 1;
  286. my $content = $self->get_page_source();
  287. if (not ref $regex eq 'ARRAY') {
  288. my $desc = qq{Content is like "$regex"} if (not defined $desc);
  289. return like_string($content , $regex, $desc );
  290. }
  291. elsif (ref $regex eq 'ARRAY') {
  292. for my $re (@$regex) {
  293. my $desc = qq{Content is like "$re"} if (not defined $desc);
  294. like_string($content , $re, $desc );
  295. }
  296. }
  297. }
  298. =head2 $twd->content_unlike( $regex [, $desc ] )
  299. $twd->content_unlike( $regex [, $desc ] )
  300. $twd->content_unlike( [$regex_1, $regex_2] [, $desc ] )
  301. Tells if the content of the page does NOT match I<$regex>. If an arrayref of regex's
  302. are provided, one 'test' is run for each regex against the content of the
  303. current page.
  304. A default description of 'Content is unlike "$regex"' will be provided if there
  305. is no description.
  306. =cut
  307. sub content_unlike {
  308. my $self = shift;
  309. my $regex = shift;
  310. my $desc = shift;
  311. local $Test::Builder::Level = $Test::Builder::Level + 1;
  312. my $content = $self->get_page_source();
  313. if (not ref $regex eq 'ARRAY') {
  314. my $desc = qq{Content is unlike "$regex"} if (not defined $desc);
  315. return unlike_string($content , $regex, $desc );
  316. }
  317. elsif (ref $regex eq 'ARRAY') {
  318. for my $re (@$regex) {
  319. my $desc = qq{Content is unlike "$re"} if (not defined $desc);
  320. unlike_string($content , $re, $desc );
  321. }
  322. }
  323. }
  324. =head2 $twd->body_text_like( $regex [, $desc ] )
  325. $twd->body_text_like( $regex [, $desc ] )
  326. $twd->body_text_like( [$regex_1, $regex_2] [, $desc ] )
  327. Tells if the text of the page (as returned by C<< get_body() >>) matches
  328. I<$regex>. If an arrayref of regex's are provided, one 'test' is run for each
  329. regex against the content of the current page.
  330. A default description of 'Content is like "$regex"' will be provided if there
  331. is no description.
  332. To also match the HTML see, C<< content_unlike() >>.
  333. =cut
  334. sub body_text_like {
  335. my $self = shift;
  336. my $regex = shift;
  337. my $desc = shift;
  338. local $Test::Builder::Level = $Test::Builder::Level + 1;
  339. my $text = $self->get_body();
  340. if (not ref $regex eq 'ARRAY') {
  341. my $desc = qq{Text is like "$regex"} if (not defined $desc);
  342. return like_string($text , $regex, $desc );
  343. }
  344. elsif (ref $regex eq 'ARRAY') {
  345. for my $re (@$regex) {
  346. my $desc = qq{Text is like "$re"} if (not defined $desc);
  347. like_string($text , $re, $desc );
  348. }
  349. }
  350. }
  351. =head2 $twd->body_text_unlike( $regex [, $desc ] )
  352. $twd->body_text_unlike( $regex [, $desc ] )
  353. $twd->body_text_unlike( [$regex_1, $regex_2] [, $desc ] )
  354. Tells if the text of the page (as returned by C<< get_body() >>)
  355. does NOT match I<$regex>. If an arrayref of regex's
  356. are provided, one 'test' is run for each regex against the content of the
  357. current page.
  358. A default description of 'Text is unlike "$regex"' will be provided if there
  359. is no description.
  360. To also match the HTML see, C<< content_unlike() >>.
  361. =cut
  362. sub body_text_unlike {
  363. my $self = shift;
  364. my $regex = shift;
  365. my $desc = shift;
  366. local $Test::Builder::Level = $Test::Builder::Level + 1;
  367. my $text = $self->get_body();
  368. if (not ref $regex eq 'ARRAY') {
  369. my $desc = qq{Text is unlike "$regex"} if (not defined $desc);
  370. return unlike_string($text , $regex, $desc );
  371. }
  372. elsif (ref $regex eq 'ARRAY') {
  373. for my $re (@$regex) {
  374. my $desc = qq{Text is unlike "$re"} if (not defined $desc);
  375. unlike_string($text , $re, $desc );
  376. }
  377. }
  378. }
  379. #####
  380. =head2 $twd->content_contains( $str [, $desc ] )
  381. $twd->content_contains( $str [, $desc ] )
  382. $twd->content_contains( [$str_1, $str_2] [, $desc ] )
  383. Tells if the content of the page contains I<$str>. If an arrayref of strngs's
  384. are provided, one 'test' is run for each string against the content of the
  385. current page.
  386. A default description of 'Content contains "$str"' will be provided if there
  387. is no description.
  388. =cut
  389. sub content_contains {
  390. my $self = shift;
  391. my $str = shift;
  392. my $desc = shift;
  393. local $Test::Builder::Level = $Test::Builder::Level + 1;
  394. my $content = $self->get_page_source();
  395. if (not ref $str eq 'ARRAY') {
  396. my $desc = qq{Content contains "$str"} if (not defined $desc);
  397. return contains_string($content , $str, $desc );
  398. }
  399. elsif (ref $str eq 'ARRAY') {
  400. for my $s (@$str) {
  401. my $desc = qq{Content contains "$s"} if (not defined $desc);
  402. contains_string($content , $s, $desc );
  403. }
  404. }
  405. }
  406. =head2 $twd->content_lacks( $str [, $desc ] )
  407. $twd->content_lacks( $str [, $desc ] )
  408. $twd->content_lacks( [$str_1, $str_2] [, $desc ] )
  409. Tells if the content of the page does NOT contain I<$str>. If an arrayref of strings
  410. are provided, one 'test' is run for each string against the content of the
  411. current page.
  412. A default description of 'Content lacks "$str"' will be provided if there
  413. is no description.
  414. =cut
  415. sub content_lacks {
  416. my $self = shift;
  417. my $str = shift;
  418. my $desc = shift;
  419. local $Test::Builder::Level = $Test::Builder::Level + 1;
  420. my $content = $self->get_page_source();
  421. if (not ref $str eq 'ARRAY') {
  422. my $desc = qq{Content lacks "$str"} if (not defined $desc);
  423. return lacks_string($content , $str, $desc );
  424. }
  425. elsif (ref $str eq 'ARRAY') {
  426. for my $s (@$str) {
  427. my $desc = qq{Content lacks "$s"} if (not defined $desc);
  428. lacks_string($content , $s, $desc );
  429. }
  430. }
  431. }
  432. =head2 $twd->body_text_contains( $str [, $desc ] )
  433. $twd->body_text_contains( $str [, $desc ] )
  434. $twd->body_text_contains( [$str_1, $str_2] [, $desc ] )
  435. Tells if the text of the page (as returned by C<< get_body() >>) contains
  436. I<$str>. If an arrayref of strings are provided, one 'test' is run for each
  437. regex against the content of the current page.
  438. A default description of 'Text contains "$str"' will be provided if there
  439. is no description.
  440. To also match the HTML see, C<< content_uncontains() >>.
  441. =cut
  442. sub body_text_contains {
  443. my $self = shift;
  444. my $str = shift;
  445. my $desc = shift;
  446. local $Test::Builder::Level = $Test::Builder::Level + 1;
  447. my $text = $self->get_body();
  448. if (not ref $str eq 'ARRAY') {
  449. my $desc = qq{Text contains "$str"} if (not defined $desc);
  450. return contains_string($text , $str, $desc );
  451. }
  452. elsif (ref $str eq 'ARRAY') {
  453. for my $s (@$str) {
  454. my $desc = qq{Text contains "$s"} if (not defined $desc);
  455. contains_string($text , $s, $desc );
  456. }
  457. }
  458. }
  459. =head2 $twd->body_text_lacks( $str [, $desc ] )
  460. $twd->body_text_lacks( $str [, $desc ] )
  461. $twd->body_text_lacks( [$str_1, $str_2] [, $desc ] )
  462. Tells if the text of the page (as returned by C<< get_body() >>)
  463. does NOT contain I<$str>. If an arrayref of strings
  464. are provided, one 'test' is run for each regex against the content of the
  465. current page.
  466. A default description of 'Text is lacks "$str"' will be provided if there
  467. is no description.
  468. To also match the HTML see, C<< content_lacks() >>.
  469. =cut
  470. sub body_text_lacks {
  471. my $self = shift;
  472. my $str = shift;
  473. my $desc = shift;
  474. local $Test::Builder::Level = $Test::Builder::Level + 1;
  475. my $text = $self->get_body();
  476. if (not ref $str eq 'ARRAY') {
  477. my $desc = qq{Text is lacks "$str"} if (not defined $desc);
  478. return lacks_string($text , $str, $desc );
  479. }
  480. elsif (ref $str eq 'ARRAY') {
  481. for my $s (@$str) {
  482. my $desc = qq{Text is lacks "$s"} if (not defined $desc);
  483. lacks_string($text , $s, $desc );
  484. }
  485. }
  486. }
  487. 1;
  488. __END__
  489. =head1 NOTES
  490. This module was forked from Test::WebDriver 0.01.
  491. For Best Practice - I recommend subclassing Test::Selenium::Remote::Driver for your application,
  492. and then refactoring common or app specific methods into MyApp::WebDriver so that
  493. your test files do not have much duplication. As your app changes, you can update
  494. MyApp::WebDriver rather than all the individual test files.
  495. =head1 AUTHORS
  496. =over 4
  497. =item *
  498. Created by: Luke Closs <lukec@cpan.org>, but inspired by
  499. L<Test::WWW::Selenium> and its authors.
  500. =back
  501. =head1 CONTRIBUTORS
  502. This work was sponsored by Prime Radiant, Inc. Mark Stosberg <mark@stosberg.com> also contributed.
  503. =head1 COPYRIGHT AND LICENSE
  504. Copyright (c) 2012 Prime Radiant, Inc.
  505. This program is free software; you can redistribute it and/or
  506. modify it under the same terms as Perl itself.