1
0

Driver.pm 25 KB

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