1
0

Driver.pm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  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. 'find_no_element_ok',
  34. 'compare_elements_ok', 'click_ok', 'double_click_ok',
  35. 'body_like',
  36. ];
  37. },
  38. );
  39. =for Pod::Coverage has_args
  40. =cut
  41. sub has_args {
  42. my $self = shift;
  43. my $fun_name = shift;
  44. my $hash_fun_args = {
  45. 'find_element' => 2,
  46. 'find_no_element' => 2,
  47. 'find_child_element' => 3,
  48. 'find_child_elements' => 3,
  49. 'find_element' => 2,
  50. 'find_elements' => 2,
  51. 'compare_elements' => 2,
  52. 'get' => 1,
  53. };
  54. return ( $hash_fun_args->{$fun_name} // 0 );
  55. }
  56. with 'Test::Selenium::Remote::Role::DoesTesting';
  57. =for Pod::Coverage BUILD
  58. =cut
  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::Remote::Driver> for the meanings of these options.
  92. =for Pod::Coverage BUILDARGS
  93. =cut
  94. sub BUILDARGS {
  95. my ( undef, %p ) = @_;
  96. for my $opt (
  97. qw/remote_server_addr port browser_name version platform
  98. javascript auto_close extra_capabilities/
  99. )
  100. {
  101. $p{$opt} //= $ENV{ 'TWD_' . uc($opt) };
  102. }
  103. $p{browser_name} //= $ENV{TWD_BROWSER}; # ykwim
  104. $p{remote_server_addr} //= $ENV{TWD_HOST}; # ykwim
  105. $p{webelement_class} //= 'Test::Selenium::Remote::WebElement';
  106. return \%p;
  107. }
  108. =head2 verbose
  109. Enable/disable debugging output, or view the status of verbosity.
  110. =cut
  111. has verbose => (
  112. is => 'rw',
  113. );
  114. =head2 server_is_running( $host, $port )
  115. Returns true if a Selenium server is running. The host and port
  116. parameters are optional, and default to C<localhost:4444>.
  117. Environment vars C<TWD_HOST> and C<TWD_PORT> can also be used to
  118. determine the server to check.
  119. =cut
  120. sub server_is_running {
  121. my $host = $ENV{TWD_HOST} || shift || 'localhost';
  122. my $port = $ENV{TWD_PORT} || shift || 4444;
  123. return ( $host, $port )
  124. if IO::Socket::INET->new(
  125. PeerAddr => $host,
  126. PeerPort => $port,
  127. );
  128. return;
  129. }
  130. =head2 error_handler
  131. As for L<Selenium::Remote::Driver>, this class also supports adding an
  132. optional C<error_handler> attribute during instantiation :
  133. my $test_driver = Test::Selenium::Remote::Driver->new(
  134. error_handler => sub { print $_[1]; croak 'goodbye'; }
  135. );
  136. Additionally, you can set and/or clear it at any time on an
  137. already-instantiated driver:
  138. # later, change the error handler to something else
  139. $driver->error_handler( sub { print $_[1]; croak 'hello'; } );
  140. # stop handling errors manually and use the default S:R:D behavior
  141. # (we will croak about the exception)
  142. $driver->clear_error_handler;
  143. Your error handler will receive two arguments,
  144. The first argument is the C<$driver> object itself.
  145. Due to some specificities of this class, the second argument passed to the
  146. handler can be:
  147. =over
  148. =item the error message from the Webdriver
  149. This is the case when the error message is raised by a WebDriver failure
  150. =item "Failed to find ..."
  151. This message is raised when the Webdriver call is successful but the failure
  152. occurs on the test performed aftwerwards. This is the case for functions like
  153. C<body_text_like>, C<body_text_unlike>, C<body_text_contains>, C<body_text_lacks>,
  154. C<content_like>, C<content_unlike>, C<content_contains>, C<content_lacks>.
  155. =back
  156. If you set your own handler, you should not rely that much on the message returned.
  157. You should also remember that you are entirely responsible for handling exceptions,
  158. which means that should the error handler be called, it means that the test you are
  159. doing has failed, so you should croak.
  160. You should also call fail() in your handler, in case the function called raised a
  161. webdriver error, because, as exceptions are not caught anymore when you specify a
  162. handler, the function will not fail anymore, which translates to a 'ok' in your TAP
  163. output if you do not handle it properly.
  164. =head1 Testing Methods
  165. The following testing methods are available. For
  166. more documentation, see the related test methods in L<Selenium::Remote::Driver>
  167. (And feel free to submit a patch to flesh out the documentation for these here).
  168. Defaults for optional arguments B<should> be the same as for their analogues in
  169. L<Selenium::Remote::Driver> and L<Selenium::Remote::WebElement>.
  170. alert_text_is
  171. alert_text_isnt
  172. alert_text_like
  173. alert_text_unlike
  174. current_window_handle_is
  175. current_window_handle_isnt
  176. current_window_handle_like
  177. current_window_handle_unlike
  178. window_handles_is
  179. window_handles_isnt
  180. window_handles_like
  181. window_handles_unlike
  182. window_size_is
  183. window_size_isnt
  184. window_size_like
  185. window_size_unlike
  186. window_position_is
  187. window_position_isnt
  188. window_position_like
  189. window_position_unlike
  190. current_url_is
  191. current_url_isnt
  192. current_url_like
  193. current_url_unlike
  194. title_is
  195. title_isnt
  196. title_like
  197. title_unlike
  198. active_element_is
  199. active_element_isnt
  200. active_element_like
  201. active_element_unlike
  202. # Basically the same as 'content_like()', but content_like() supports multiple regex's.
  203. page_source_is
  204. page_source_isnt
  205. page_source_like
  206. page_source_unlike
  207. send_keys_to_active_element_ok
  208. send_keys_to_alert_ok
  209. send_keys_to_prompt_ok
  210. send_modifier_ok
  211. accept_alert_ok
  212. dismiss_alert_ok
  213. move_mouse_to_location_ok # TODO
  214. move_to_ok # TODO
  215. get_ok
  216. go_back_ok
  217. go_forward_ok
  218. add_cookie_ok
  219. get_page_source_ok
  220. find_element_ok($search_target)
  221. find_element_ok($search_target)
  222. find_elements_ok
  223. find_child_element_ok
  224. find_child_elements_ok
  225. compare_elements_ok
  226. click_ok
  227. double_click_ok
  228. =cut
  229. # function composing a find_element with locator with a webelement test
  230. sub _find_element_with_action {
  231. my $self = shift;
  232. my $method = shift;
  233. my ( $locator, $locator_strategy, $params, $desc ) = @_;
  234. $locator_strategy //= 'xpath';
  235. # case 4 args
  236. if ($desc) {
  237. $self->croak('Invalid locator strategy')
  238. unless ( $self->FINDERS->{$locator_strategy} );
  239. }
  240. else {
  241. if ($params) {
  242. # means that we called it the 'old way' (no locator strategy)
  243. if ( !defined( $self->FINDERS->{$locator_strategy} ) ) {
  244. $desc = $params;
  245. $params = $locator_strategy;
  246. $locator_strategy =
  247. $self->_get_finder_key( $self->default_finder );
  248. }
  249. }
  250. else {
  251. # means it was called with no locator strategy and no desc
  252. if ($locator_strategy) {
  253. if ( !defined( $self->FINDERS->{$locator_strategy} ) ) {
  254. $params = $locator_strategy;
  255. $locator_strategy =
  256. $self->_get_finder_key( $self->default_finder );
  257. }
  258. }
  259. else {
  260. $self->croak('Not enough arguments');
  261. }
  262. }
  263. }
  264. unless ($desc) {
  265. $desc = $method;
  266. $desc .= "'" . join( " ", ( $params // '' ) ) . "'";
  267. }
  268. my $element;
  269. eval {
  270. $element = $self->find_element( $locator, $locator_strategy );
  271. };
  272. if ($@) {
  273. print "# Error: $@\n";
  274. return 0;
  275. }
  276. return $element->$method( $params, $desc );
  277. }
  278. =head2 $twd->type_element_ok($search_target [,$locator], $keys, [, $desc ]);
  279. $twd->type_element_ok( $search_target [,$locator], $keys [, $desc ] );
  280. Use L<Selenium::Remote::Driver/find_element> to resolve the C<$search_target>
  281. to a web element and an optional locator, and then type C<$keys> into it, providing an optional test
  282. label.
  283. =cut
  284. sub type_element_ok {
  285. my $self = shift;
  286. my $method = 'send_keys_ok';
  287. return $self->_find_element_with_action( $method, @_ );
  288. }
  289. =head2 $twd->element_text_is($search_target[,$finder],$expected_text [,$desc]);
  290. $twd->element_text_is($search_target[,$finder],$expected_text [,$desc]);
  291. =cut
  292. sub element_text_is {
  293. my $self = shift;
  294. my $method = 'text_is';
  295. return $self->_find_element_with_action( $method, @_ );
  296. }
  297. =head2 $twd->element_value_is($search_target[,$finder],$expected_value [,$desc]);
  298. $twd->element_value_is($search_target[,$finder],$expected_value [,$desc]);
  299. =cut
  300. sub element_value_is {
  301. my $self = shift;
  302. my $method = 'value_is';
  303. return $self->_find_element_with_action( $method, @_ );
  304. }
  305. =head2 $twd->click_element_ok($search_target [,$finder ,$desc]);
  306. $twd->click_element_ok($search_target [,$finder ,$desc]);
  307. Find an element and then click on it.
  308. =cut
  309. sub click_element_ok {
  310. my $self = shift;
  311. my $method = 'click_ok';
  312. return $self->_find_element_with_action( $method, @_ );
  313. }
  314. =head2 $twd->clear_element_ok($search_target [,$finder ,$desc]);
  315. $twd->clear_element_ok($search_target [,$finder ,$desc]);
  316. Find an element and then clear on it.
  317. =cut
  318. sub clear_element_ok {
  319. my $self = shift;
  320. my $method = 'clear_ok';
  321. return $self->_find_element_with_action( $method, @_ );
  322. }
  323. =head2 $twd->is_element_displayed_ok($search_target [,$finder ,$desc]);
  324. $twd->is_element_displayed_ok($search_target [,$finder ,$desc]);
  325. Find an element and check to confirm that it is displayed. (visible)
  326. =cut
  327. sub is_element_displayed_ok {
  328. my $self = shift;
  329. my $method = 'is_displayed_ok';
  330. return $self->_find_element_with_action( $method, @_ );
  331. }
  332. =head2 $twd->is_element_enabled_ok($search_target [,$finder ,$desc]);
  333. $twd->is_element_enabled_ok($search_target [,$finder ,$desc]);
  334. Find an element and check to confirm that it is enabled.
  335. =cut
  336. sub is_element_enabled_ok {
  337. my $self = shift;
  338. my $method = 'is_enabled_ok';
  339. return $self->_find_element_with_action( $method, @_ );
  340. }
  341. =head2 $twd->find_element_ok($search_target [,$finder, $desc ]);
  342. $twd->find_element_ok( $search_target [,$finder, $desc ] );
  343. Returns true if C<$search_target> is successfully found on the page. C<$search_target>
  344. is passed to L<Selenium::Remote::Driver/find_element> using a finder or the C<default_finder>
  345. if none passed.
  346. See there for more details on the format for C<find_element_ok()>.
  347. =head2 $twd->find_no_element_ok($search_target [,$finder, $desc ]);
  348. $twd->find_no_element_ok( $search_target [,$finder, $desc ] );
  349. Returns true if C<$search_target> is I<not> found on the page. C<$search_target>
  350. is passed to L<Selenium::Remote::Driver/find_element> using a finder or the
  351. C<default_finder> if none passed. See there for more details on the format for C<find_no_element_ok()>.
  352. =head2 $twd->content_like( $regex [, $desc ] )
  353. $twd->content_like( $regex [, $desc ] )
  354. $twd->content_like( [$regex_1, $regex_2] [, $desc ] )
  355. Tells if the content of the page matches I<$regex>. If an arrayref of regex's
  356. are provided, one 'test' is run for each regex against the content of the
  357. current page.
  358. A default description of 'Content is like "$regex"' will be provided if there
  359. is no description.
  360. =cut
  361. sub content_like {
  362. my $self = shift;
  363. my $regex = shift;
  364. my $desc = shift;
  365. local $Test::Builder::Level = $Test::Builder::Level + 1;
  366. my $content = $self->get_page_source();
  367. my $ret;
  368. if ( not ref $regex eq 'ARRAY' ) {
  369. $desc = qq{Content is like "$regex"} if ( not defined $desc );
  370. $ret = like_string( $content, $regex, $desc );
  371. if ( !$ret && $self->has_error_handler ) {
  372. $self->error_handler->($self,"Failed to find $regex");
  373. }
  374. return $ret;
  375. }
  376. elsif ( ref $regex eq 'ARRAY' ) {
  377. for my $re (@$regex) {
  378. $desc = qq{Content is like "$re"} if ( not defined $desc );
  379. $ret = like_string( $content, $re, $desc );
  380. if ( !$ret && $self->has_error_handler ) {
  381. $self->error_handler->($self,"Failed to find $re");
  382. }
  383. }
  384. }
  385. }
  386. =head2 $twd->content_unlike( $regex [, $desc ] )
  387. $twd->content_unlike( $regex [, $desc ] )
  388. $twd->content_unlike( [$regex_1, $regex_2] [, $desc ] )
  389. Tells if the content of the page does NOT match I<$regex>. If an arrayref of regex's
  390. are provided, one 'test' is run for each regex against the content of the
  391. current page.
  392. A default description of 'Content is unlike "$regex"' will be provided if there
  393. is no description.
  394. =cut
  395. sub content_unlike {
  396. my $self = shift;
  397. my $regex = shift;
  398. my $desc = shift;
  399. local $Test::Builder::Level = $Test::Builder::Level + 1;
  400. my $content = $self->get_page_source();
  401. my $ret;
  402. if ( not ref $regex eq 'ARRAY' ) {
  403. $desc = qq{Content is unlike "$regex"} if ( not defined $desc );
  404. $ret = unlike_string( $content, $regex, $desc );
  405. if ( !$ret && $self->has_error_handler ) {
  406. $self->error_handler->($self,"Failed to find $regex");
  407. }
  408. }
  409. elsif ( ref $regex eq 'ARRAY' ) {
  410. for my $re (@$regex) {
  411. $desc = qq{Content is unlike "$re"} if ( not defined $desc );
  412. $ret = unlike_string( $content, $re, $desc );
  413. if ( !$ret && $self->has_error_handler ) {
  414. $self->error_handler->($self,"Failed to find $re");
  415. }
  416. }
  417. }
  418. }
  419. =head2 $twd->body_text_like( $regex [, $desc ] )
  420. $twd->body_text_like( $regex [, $desc ] )
  421. $twd->body_text_like( [$regex_1, $regex_2] [, $desc ] )
  422. Tells if the text of the page (as returned by C<< get_body() >>) matches
  423. I<$regex>. If an arrayref of regex's are provided, one 'test' is run for each
  424. regex against the content of the current page.
  425. A default description of 'Content is like "$regex"' will be provided if there
  426. is no description.
  427. To also match the HTML see, C<< content_unlike() >>.
  428. =cut
  429. sub body_text_like {
  430. my $self = shift;
  431. my $regex = shift;
  432. my $desc = shift;
  433. local $Test::Builder::Level = $Test::Builder::Level + 1;
  434. my $text = $self->get_body();
  435. my $ret;
  436. if ( not ref $regex eq 'ARRAY' ) {
  437. $desc = qq{Text is like "$regex"} if ( not defined $desc );
  438. $ret = like_string( $text, $regex, $desc );
  439. if ( !$ret && $self->has_error_handler ) {
  440. $self->error_handler->($self,"Failed to find $regex");
  441. }
  442. return $ret;
  443. }
  444. elsif ( ref $regex eq 'ARRAY' ) {
  445. for my $re (@$regex) {
  446. $desc = qq{Text is like "$re"} if ( not defined $desc );
  447. $ret = like_string( $text, $re, $desc );
  448. if ( !$ret && $self->has_error_handler ) {
  449. $self->error_handler->($self,"Failed to find $re");
  450. }
  451. }
  452. }
  453. }
  454. =head2 $twd->body_text_unlike( $regex [, $desc ] )
  455. $twd->body_text_unlike( $regex [, $desc ] )
  456. $twd->body_text_unlike( [$regex_1, $regex_2] [, $desc ] )
  457. Tells if the text of the page (as returned by C<< get_body() >>)
  458. does NOT match I<$regex>. If an arrayref of regex's
  459. are provided, one 'test' is run for each regex against the content of the
  460. current page.
  461. A default description of 'Text is unlike "$regex"' will be provided if there
  462. is no description.
  463. To also match the HTML see, C<< content_unlike() >>.
  464. =cut
  465. sub body_text_unlike {
  466. my $self = shift;
  467. my $regex = shift;
  468. my $desc = shift;
  469. local $Test::Builder::Level = $Test::Builder::Level + 1;
  470. my $text = $self->get_body();
  471. my $ret;
  472. if ( not ref $regex eq 'ARRAY' ) {
  473. $desc = qq{Text is unlike "$regex"} if ( not defined $desc );
  474. $ret = unlike_string( $text, $regex, $desc );
  475. if ( !$ret && $self->has_error_handler ) {
  476. $self->error_handler->($self,"Failed to find $regex");
  477. }
  478. return $ret;
  479. }
  480. elsif ( ref $regex eq 'ARRAY' ) {
  481. for my $re (@$regex) {
  482. $desc = qq{Text is unlike "$re"} if ( not defined $desc );
  483. $ret = unlike_string( $text, $re, $desc );
  484. if ( !$ret && $self->has_error_handler ) {
  485. $self->error_handler->($self,"Failed to find $re");
  486. }
  487. }
  488. }
  489. }
  490. #####
  491. =head2 $twd->content_contains( $str [, $desc ] )
  492. $twd->content_contains( $str [, $desc ] )
  493. $twd->content_contains( [$str_1, $str_2] [, $desc ] )
  494. Tells if the content of the page contains I<$str>. If an arrayref of strngs's
  495. are provided, one 'test' is run for each string against the content of the
  496. current page.
  497. A default description of 'Content contains "$str"' will be provided if there
  498. is no description.
  499. =cut
  500. sub content_contains {
  501. my $self = shift;
  502. my $str = shift;
  503. my $desc = shift;
  504. local $Test::Builder::Level = $Test::Builder::Level + 1;
  505. my $content = $self->get_page_source();
  506. my $ret;
  507. if ( not ref $str eq 'ARRAY' ) {
  508. $desc = qq{Content contains "$str"} if ( not defined $desc );
  509. $ret = contains_string( $content, $str, $desc );
  510. if ( !$ret && $self->has_error_handler ) {
  511. $self->error_handler->($self,"Failed to find $str");
  512. }
  513. return $ret;
  514. }
  515. elsif ( ref $str eq 'ARRAY' ) {
  516. for my $s (@$str) {
  517. $desc = qq{Content contains "$s"} if ( not defined $desc );
  518. $ret = contains_string( $content, $s, $desc );
  519. if ( !$ret && $self->has_error_handler ) {
  520. $self->error_handler->($self,"Failed to find $s");
  521. }
  522. }
  523. }
  524. }
  525. =head2 $twd->content_lacks( $str [, $desc ] )
  526. $twd->content_lacks( $str [, $desc ] )
  527. $twd->content_lacks( [$str_1, $str_2] [, $desc ] )
  528. Tells if the content of the page does NOT contain I<$str>. If an arrayref of strings
  529. are provided, one 'test' is run for each string against the content of the
  530. current page.
  531. A default description of 'Content lacks "$str"' will be provided if there
  532. is no description.
  533. =cut
  534. sub content_lacks {
  535. my $self = shift;
  536. my $str = shift;
  537. my $desc = shift;
  538. local $Test::Builder::Level = $Test::Builder::Level + 1;
  539. my $content = $self->get_page_source();
  540. my $ret;
  541. if ( not ref $str eq 'ARRAY' ) {
  542. $desc = qq{Content lacks "$str"} if ( not defined $desc );
  543. $ret = lacks_string( $content, $str, $desc );
  544. if ( !$ret && $self->has_error_handler ) {
  545. $self->error_handler->($self,"Failed to find $str");
  546. }
  547. return $ret;
  548. }
  549. elsif ( ref $str eq 'ARRAY' ) {
  550. for my $s (@$str) {
  551. $desc = qq{Content lacks "$s"} if ( not defined $desc );
  552. $ret = lacks_string( $content, $s, $desc );
  553. if ( !$ret && $self->has_error_handler ) {
  554. $self->error_handler->($self,"Failed to find $s");
  555. }
  556. }
  557. }
  558. }
  559. =head2 $twd->body_text_contains( $str [, $desc ] )
  560. $twd->body_text_contains( $str [, $desc ] )
  561. $twd->body_text_contains( [$str_1, $str_2] [, $desc ] )
  562. Tells if the text of the page (as returned by C<< get_body() >>) contains
  563. I<$str>. If an arrayref of strings are provided, one 'test' is run for each
  564. regex against the content of the current page.
  565. A default description of 'Text contains "$str"' will be provided if there
  566. is no description.
  567. To also match the HTML see, C<< content_uncontains() >>.
  568. =cut
  569. sub body_text_contains {
  570. my $self = shift;
  571. my $str = shift;
  572. my $desc = shift;
  573. local $Test::Builder::Level = $Test::Builder::Level + 1;
  574. my $text = $self->get_body();
  575. my $ret;
  576. if ( not ref $str eq 'ARRAY' ) {
  577. $desc = qq{Text contains "$str"} if ( not defined $desc );
  578. $ret = contains_string( $text, $str, $desc );
  579. if ( !$ret && $self->has_error_handler ) {
  580. $self->error_handler->($self,"Failed to find $str");
  581. }
  582. return $ret;
  583. }
  584. elsif ( ref $str eq 'ARRAY' ) {
  585. for my $s (@$str) {
  586. $desc = qq{Text contains "$s"} if ( not defined $desc );
  587. $ret = contains_string( $text, $s, $desc );
  588. if ( !$ret && $self->has_error_handler ) {
  589. $self->error_handler->($self,"Failed to find $s");
  590. }
  591. }
  592. }
  593. }
  594. =head2 $twd->body_text_lacks( $str [, $desc ] )
  595. $twd->body_text_lacks( $str [, $desc ] )
  596. $twd->body_text_lacks( [$str_1, $str_2] [, $desc ] )
  597. Tells if the text of the page (as returned by C<< get_body() >>)
  598. does NOT contain I<$str>. If an arrayref of strings
  599. are provided, one 'test' is run for each regex against the content of the
  600. current page.
  601. A default description of 'Text lacks "$str"' will be provided if there
  602. is no description.
  603. To also match the HTML see, C<< content_lacks() >>.
  604. =cut
  605. sub body_text_lacks {
  606. my $self = shift;
  607. my $str = shift;
  608. my $desc = shift;
  609. local $Test::Builder::Level = $Test::Builder::Level + 1;
  610. my $text = $self->get_body();
  611. my $ret;
  612. if ( not ref $str eq 'ARRAY' ) {
  613. $desc = qq{Text lacks "$str"} if ( not defined $desc );
  614. $ret = lacks_string( $text, $str, $desc );
  615. if ( !$ret && $self->has_error_handler ) {
  616. $self->error_handler->($self,"Failed to find $str");
  617. }
  618. return $ret;
  619. }
  620. elsif ( ref $str eq 'ARRAY' ) {
  621. for my $s (@$str) {
  622. $desc = qq{Text lacks "$s"} if ( not defined $desc );
  623. $ret = lacks_string( $text, $s, $desc );
  624. if ( !$ret && $self->has_error_handler ) {
  625. $self->error_handler->($self,"Failed to find $s");
  626. }
  627. }
  628. }
  629. }
  630. 1;
  631. __END__
  632. =head1 NOTES
  633. This module was forked from Test::WebDriver 0.01.
  634. For Best Practice - I recommend subclassing Test::Selenium::Remote::Driver for your application,
  635. and then refactoring common or app specific methods into MyApp::WebDriver so that
  636. your test files do not have much duplication. As your app changes, you can update
  637. MyApp::WebDriver rather than all the individual test files.
  638. =head1 AUTHORS
  639. =over 4
  640. =item *
  641. Created by: Luke Closs <lukec@cpan.org>, but inspired by
  642. L<Test::WWW::Selenium> and its authors.
  643. =back
  644. =head1 CONTRIBUTORS
  645. Test::WebDriver work was sponsored by Prime Radiant, Inc.
  646. Mark Stosberg <mark@stosberg.com> forked it as Test::Selenium::Remote::Driver
  647. and significantly expanded it.
  648. =head1 COPYRIGHT AND LICENSE
  649. Parts Copyright (c) 2012 Prime Radiant, Inc.
  650. This program is free software; you can redistribute it and/or
  651. modify it under the same terms as Perl itself.