1
0

Driver.pm 20 KB

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