Driver.pm 25 KB

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