1
0

Driver.pm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. =head1 Testing Methods
  162. The following testing methods are available. For
  163. more documentation, see the related test methods in L<Selenium::Remote::Driver>
  164. (And feel free to submit a patch to flesh out the documentation for these here).
  165. alert_text_is
  166. alert_text_isnt
  167. alert_text_like
  168. alert_text_unlike
  169. current_window_handle_is
  170. current_window_handle_isnt
  171. current_window_handle_like
  172. current_window_handle_unlike
  173. window_handles_is
  174. window_handles_isnt
  175. window_handles_like
  176. window_handles_unlike
  177. window_size_is
  178. window_size_isnt
  179. window_size_like
  180. window_size_unlike
  181. window_position_is
  182. window_position_isnt
  183. window_position_like
  184. window_position_unlike
  185. current_url_is
  186. current_url_isnt
  187. current_url_like
  188. current_url_unlike
  189. title_is
  190. title_isnt
  191. title_like
  192. title_unlike
  193. active_element_is
  194. active_element_isnt
  195. active_element_like
  196. active_element_unlike
  197. # Basically the same as 'content_like()', but content_like() supports multiple regex's.
  198. page_source_is
  199. page_source_isnt
  200. page_source_like
  201. page_source_unlike
  202. send_keys_to_active_element_ok
  203. send_keys_to_alert_ok
  204. send_keys_to_prompt_ok
  205. send_modifier_ok
  206. accept_alert_ok
  207. dismiss_alert_ok
  208. move_mouse_to_location_ok
  209. move_to_ok
  210. get_ok
  211. go_back_ok
  212. go_forward_ok
  213. add_cookie_ok
  214. get_page_source_ok
  215. find_element_ok
  216. find_elements_ok
  217. find_child_element_ok
  218. find_child_elements_ok
  219. compare_elements_ok
  220. click_ok
  221. double_click_ok
  222. =head2 $twd->content_like( $regex [, $desc ] )
  223. $twd->content_like( $regex [, $desc ] )
  224. $twd->content_like( [$regex_1, $regex_2] [, $desc ] )
  225. Tells if the content of the page matches I<$regex>. If an arrayref of regex's
  226. are provided, one 'test' is run for each regex against the content of the
  227. current page.
  228. A default description of 'Content is like "$regex"' will be provided if there
  229. is no description.
  230. =cut
  231. sub content_like {
  232. my $self = shift;
  233. my $regex = shift;
  234. my $desc = shift;
  235. local $Test::Builder::Level = $Test::Builder::Level + 1;
  236. my $content = $self->get_page_source();
  237. if (not ref $regex eq 'ARRAY') {
  238. my $desc = qq{Content is like "$regex"} if (not defined $desc);
  239. return like_string($content , $regex, $desc );
  240. }
  241. elsif (ref $regex eq 'ARRAY') {
  242. for my $re (@$regex) {
  243. my $desc = qq{Content is like "$re"} if (not defined $desc);
  244. like_string($content , $re, $desc );
  245. }
  246. }
  247. }
  248. =head2 $twd->content_unlike( $regex [, $desc ] )
  249. $twd->content_unlike( $regex [, $desc ] )
  250. $twd->content_unlike( [$regex_1, $regex_2] [, $desc ] )
  251. Tells if the content of the page does NOT match I<$regex>. If an arrayref of regex's
  252. are provided, one 'test' is run for each regex against the content of the
  253. current page.
  254. A default description of 'Content is unlike "$regex"' will be provided if there
  255. is no description.
  256. =cut
  257. sub content_unlike {
  258. my $self = shift;
  259. my $regex = shift;
  260. my $desc = shift;
  261. local $Test::Builder::Level = $Test::Builder::Level + 1;
  262. my $content = $self->get_page_source();
  263. if (not ref $regex eq 'ARRAY') {
  264. my $desc = qq{Content is unlike "$regex"} if (not defined $desc);
  265. return unlike_string($content , $regex, $desc );
  266. }
  267. elsif (ref $regex eq 'ARRAY') {
  268. for my $re (@$regex) {
  269. my $desc = qq{Content is unlike "$re"} if (not defined $desc);
  270. unlike_string($content , $re, $desc );
  271. }
  272. }
  273. }
  274. =head2 $twd->text_like( $regex [, $desc ] )
  275. $twd->text_like( $regex [, $desc ] )
  276. $twd->text_like( [$regex_1, $regex_2] [, $desc ] )
  277. Tells if the text of the page (as returned by C<< get_body() >>) matches
  278. I<$regex>. If an arrayref of regex's are provided, one 'test' is run for each
  279. regex against the content of the current page.
  280. A default description of 'Content is like "$regex"' will be provided if there
  281. is no description.
  282. To also match the HTML see, C<< content_unlike() >>.
  283. =cut
  284. sub text_like {
  285. my $self = shift;
  286. my $regex = shift;
  287. my $desc = shift;
  288. local $Test::Builder::Level = $Test::Builder::Level + 1;
  289. my $text = $self->get_body();
  290. if (not ref $regex eq 'ARRAY') {
  291. my $desc = qq{Text is like "$regex"} if (not defined $desc);
  292. return like_string($text , $regex, $desc );
  293. }
  294. elsif (ref $regex eq 'ARRAY') {
  295. for my $re (@$regex) {
  296. my $desc = qq{Text is like "$re"} if (not defined $desc);
  297. like_string($text , $re, $desc );
  298. }
  299. }
  300. }
  301. =head2 $twd->text_unlike( $regex [, $desc ] )
  302. $twd->text_unlike( $regex [, $desc ] )
  303. $twd->text_unlike( [$regex_1, $regex_2] [, $desc ] )
  304. Tells if the text of the page (as returned by C<< get_body() >>)
  305. does NOT match I<$regex>. If an arrayref of regex's
  306. are provided, one 'test' is run for each regex against the content of the
  307. current page.
  308. A default description of 'Text is unlike "$regex"' will be provided if there
  309. is no description.
  310. To also match the HTML see, C<< content_unlike() >>.
  311. =cut
  312. sub text_unlike {
  313. my $self = shift;
  314. my $regex = shift;
  315. my $desc = shift;
  316. local $Test::Builder::Level = $Test::Builder::Level + 1;
  317. my $text = $self->get_body();
  318. if (not ref $regex eq 'ARRAY') {
  319. my $desc = qq{Text is unlike "$regex"} if (not defined $desc);
  320. return unlike_string($text , $regex, $desc );
  321. }
  322. elsif (ref $regex eq 'ARRAY') {
  323. for my $re (@$regex) {
  324. my $desc = qq{Text is unlike "$re"} if (not defined $desc);
  325. unlike_string($text , $re, $desc );
  326. }
  327. }
  328. }
  329. #####
  330. =head2 $twd->content_contains( $str [, $desc ] )
  331. $twd->content_contains( $str [, $desc ] )
  332. $twd->content_contains( [$str_1, $str_2] [, $desc ] )
  333. Tells if the content of the page contains I<$str>. If an arrayref of strngs's
  334. are provided, one 'test' is run for each string against the content of the
  335. current page.
  336. A default description of 'Content contains "$str"' will be provided if there
  337. is no description.
  338. =cut
  339. sub content_contains {
  340. my $self = shift;
  341. my $str = shift;
  342. my $desc = shift;
  343. local $Test::Builder::Level = $Test::Builder::Level + 1;
  344. my $content = $self->get_page_source();
  345. if (not ref $str eq 'ARRAY') {
  346. my $desc = qq{Content contains "$str"} if (not defined $desc);
  347. return contains_string($content , $str, $desc );
  348. }
  349. elsif (ref $str eq 'ARRAY') {
  350. for my $s (@$str) {
  351. my $desc = qq{Content contains "$s"} if (not defined $desc);
  352. contains_string($content , $s, $desc );
  353. }
  354. }
  355. }
  356. =head2 $twd->content_lacks( $str [, $desc ] )
  357. $twd->content_lacks( $str [, $desc ] )
  358. $twd->content_lacks( [$str_1, $str_2] [, $desc ] )
  359. Tells if the content of the page does NOT contain I<$str>. If an arrayref of strings
  360. are provided, one 'test' is run for each string against the content of the
  361. current page.
  362. A default description of 'Content lacks "$str"' will be provided if there
  363. is no description.
  364. =cut
  365. sub content_lacks {
  366. my $self = shift;
  367. my $str = shift;
  368. my $desc = shift;
  369. local $Test::Builder::Level = $Test::Builder::Level + 1;
  370. my $content = $self->get_page_source();
  371. if (not ref $str eq 'ARRAY') {
  372. my $desc = qq{Content lacks "$str"} if (not defined $desc);
  373. return lacks_string($content , $str, $desc );
  374. }
  375. elsif (ref $str eq 'ARRAY') {
  376. for my $s (@$str) {
  377. my $desc = qq{Content lacks "$s"} if (not defined $desc);
  378. lacks_string($content , $s, $desc );
  379. }
  380. }
  381. }
  382. =head2 $twd->text_contains( $str [, $desc ] )
  383. $twd->text_contains( $str [, $desc ] )
  384. $twd->text_contains( [$str_1, $str_2] [, $desc ] )
  385. Tells if the text of the page (as returned by C<< get_body() >>) contains
  386. I<$str>. If an arrayref of strings are provided, one 'test' is run for each
  387. regex against the content of the current page.
  388. A default description of 'Text contains "$str"' will be provided if there
  389. is no description.
  390. To also match the HTML see, C<< content_uncontains() >>.
  391. =cut
  392. sub text_contains {
  393. my $self = shift;
  394. my $str = shift;
  395. my $desc = shift;
  396. local $Test::Builder::Level = $Test::Builder::Level + 1;
  397. my $text = $self->get_body();
  398. if (not ref $str eq 'ARRAY') {
  399. my $desc = qq{Text contains "$str"} if (not defined $desc);
  400. return contains_string($text , $str, $desc );
  401. }
  402. elsif (ref $str eq 'ARRAY') {
  403. for my $s (@$str) {
  404. my $desc = qq{Text contains "$s"} if (not defined $desc);
  405. contains_string($text , $s, $desc );
  406. }
  407. }
  408. }
  409. =head2 $twd->text_lacks( $str [, $desc ] )
  410. $twd->text_lacks( $str [, $desc ] )
  411. $twd->text_lacks( [$str_1, $str_2] [, $desc ] )
  412. Tells if the text of the page (as returned by C<< get_body() >>)
  413. does NOT contain I<$str>. If an arrayref of strings
  414. are provided, one 'test' is run for each regex against the content of the
  415. current page.
  416. A default description of 'Text is lacks "$str"' will be provided if there
  417. is no description.
  418. To also match the HTML see, C<< content_lacks() >>.
  419. =cut
  420. sub text_lacks {
  421. my $self = shift;
  422. my $str = shift;
  423. my $desc = shift;
  424. local $Test::Builder::Level = $Test::Builder::Level + 1;
  425. my $text = $self->get_body();
  426. if (not ref $str eq 'ARRAY') {
  427. my $desc = qq{Text is lacks "$str"} if (not defined $desc);
  428. return lacks_string($text , $str, $desc );
  429. }
  430. elsif (ref $str eq 'ARRAY') {
  431. for my $s (@$str) {
  432. my $desc = qq{Text is lacks "$s"} if (not defined $desc);
  433. lacks_string($text , $s, $desc );
  434. }
  435. }
  436. }
  437. 1;
  438. __END__
  439. =head1 NOTES
  440. This module was forked from Test::WebDriver 0.01.
  441. For Best Practice - I recommend subclassing Test::Selenium::Remote::Driver for your application,
  442. and then refactoring common or app specific methods into MyApp::WebDriver so that
  443. your test files do not have much duplication. As your app changes, you can update
  444. MyApp::WebDriver rather than all the individual test files.
  445. =head1 AUTHORS
  446. =over 4
  447. =item *
  448. Created by: Luke Closs <lukec@cpan.org>, but inspired by
  449. L<Test::WWW::Selenium> and its authors.
  450. =back
  451. =head1 CONTRIBUTORS
  452. This work was sponsored by Prime Radiant, Inc. Mark Stosberg <mark@stosberg.com> also contributed.
  453. =head1 COPYRIGHT AND LICENSE
  454. Copyright (c) 2012 Prime Radiant, Inc.
  455. This program is free software; you can redistribute it and/or
  456. modify it under the same terms as Perl itself.