Driver.pm 19 KB

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