Driver.pm 19 KB

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