1
0

Driver.pm 19 KB

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