Test-Selenium-Remote-Driver.t 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use Test::Fatal;
  5. use Test::Selenium::Remote::Driver;
  6. use Test::Builder::Tester;
  7. use Selenium::Remote::WebElement;
  8. use Selenium::Remote::Mock::Commands;
  9. use Selenium::Remote::Mock::RemoteConnection;
  10. use Carp;
  11. my $find_element = sub {
  12. my ( undef, $searched_item ) = @_;
  13. if ( $searched_item->{value} eq 'q' ) {
  14. return { status => 'OK', return => { ELEMENT => '123456' } };
  15. }
  16. if ( $searched_item->{value} eq 'p'
  17. && $searched_item->{using} eq 'class name' )
  18. {
  19. return { status => 'OK', return => { ELEMENT => '123457' } };
  20. }
  21. if ( $searched_item->{value} eq '//body' && $searched_item->{using} eq 'xpath') {
  22. return { status => 'OK', return => { ELEMENT => '123458' } };
  23. }
  24. if ( $searched_item->{value} eq '//p'
  25. && $searched_item->{using} eq 'xpath' )
  26. {
  27. return { status => 'OK',
  28. return => [ { ELEMENT => '123456' }, { ELEMENT => '12341234' } ] };
  29. }
  30. return { status => 'NOK', return => 0, error => 'element not found' };
  31. };
  32. my $find_child_element = sub {
  33. my ( $session_object, $searched_item ) = @_;
  34. my $elem_id = $session_object->{id};
  35. if ( $elem_id == 1 && $searched_item->{value} eq 'p' ) {
  36. if ( $searched_item->{using} eq 'class name' ) {
  37. return { status => 'OK', return => { ELEMENT => '11223344' } };
  38. }
  39. if ( $searched_item->{using} eq 'xpath' ) {
  40. return { status => 'OK',
  41. return => [ { ELEMENT => '112' }, { ELEMENT => '113' } ] };
  42. }
  43. }
  44. return {
  45. status => 'NOK', return => 0,
  46. error => 'child element not found'
  47. };
  48. };
  49. my $find_elements = sub {
  50. my ( undef, $searched_expr ) = @_;
  51. if ( $searched_expr->{value} eq 'abc'
  52. && $searched_expr->{using} eq 'xpath' )
  53. {
  54. return { status => 'OK',
  55. return => [ { ELEMENT => '123456' }, { ELEMENT => '12341234' } ] };
  56. }
  57. };
  58. my $send_keys = sub {
  59. my ( $session_object, $val ) = @_;
  60. my $keys = join('', @{$val->{value}});
  61. return { status => 'OK', return => 1 } if ( $keys =~ /abc|def/ );
  62. return { status => 'NOK', return => 0, error => 'cannot send keys' };
  63. };
  64. my $get_text = sub {
  65. my ($session_object) =@_;
  66. return 'abc' if ($session_object->{id} eq '123456');
  67. return 'def' if ($session_object->{id} eq '123457');
  68. return 'this output matches' if ($session_object->{id} eq '123458');
  69. return;
  70. };
  71. my $get_attr = sub {
  72. my ($session_object) = @_;
  73. return 'foo';
  74. };
  75. my $spec = {
  76. findElement => $find_element,
  77. findChildElement => $find_child_element,
  78. getPageSource => sub { return 'this output matches regex'},
  79. findElements => $find_elements,
  80. findChildElements => $find_child_element,
  81. getElementText => $get_text,
  82. sendKeysToElement => $send_keys,
  83. getElementAttribute => $get_attr,
  84. clickElement => sub { return { status => 'OK', return => 1 }; },
  85. clearElement => sub { return { status => 'OK', return => 1 }; },
  86. isElementDisplayed => sub { return { status => 'OK', return => 1 }; },
  87. isElementEnabled => sub { return { status => 'OK', return => 1 }; },
  88. };
  89. my $mock_commands = Selenium::Remote::Mock::Commands->new;
  90. my $successful_driver =
  91. Test::Selenium::Remote::Driver->new(
  92. remote_conn => Selenium::Remote::Mock::RemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
  93. commands => $mock_commands,
  94. );
  95. # find element ok tests
  96. $successful_driver->find_element_ok('q','find_element_ok works');
  97. $successful_driver->default_finder('class');
  98. $successful_driver->find_element_ok('p','find_element_ok with a locator works');
  99. $successful_driver->default_finder('xpath');
  100. ok( exception { $successful_driver->find_element_ok('notq') }, 'find_element_ok dies if element not found' );
  101. $successful_driver->find_elements_ok('abc','find_elements_ok works');
  102. # find child element ok tests
  103. $successful_driver->find_child_elements_ok({id => 1},'p','find_child_elements_ok works');
  104. $successful_driver->find_child_element_ok({id => 1},'p','class','find_child_element_ok with a locator works');
  105. ok( exception { $successful_driver->find_child_element_ok({id => 1200}) }, 'find_child_element_ok dies if the element is not found' );
  106. # find no element ok test
  107. $successful_driver->find_no_element_ok('notq','xpath','find_no_element_ok works');
  108. ok(exception { $successful_driver->find_no_element_ok('abc','xpath','find_no_element_ok works') }, 'find no element dies when an element is found');
  109. my $called = 0;
  110. $successful_driver->error_handler(sub { my ($self,$msg) = @_; $called++; croak});
  111. $successful_driver->find_no_element_ok('notq','xpath');
  112. is($called, 0, 'find_no_element_ok does not call error handler when finding no element');
  113. $successful_driver->clear_error_handler;
  114. # body and content function family
  115. $successful_driver->content_like( qr/matches/, 'content_like works');
  116. $successful_driver->content_unlike( qr/nomatch/, 'content_unlike works');
  117. $successful_driver->content_contains( 'matches', 'content_contains works');
  118. $successful_driver->content_lacks( 'nomatch', 'content_lacks works');
  119. $successful_driver->body_text_contains( ['match','output'], 'body_text_contains works');
  120. $successful_driver->body_text_lacks( 'nomatch', 'body_text_lacks works');
  121. $successful_driver->body_text_like( qr/this/, 'body_text_like works');
  122. $successful_driver->body_text_unlike( qr/notthis/, 'body_text_unlike works');
  123. $successful_driver->type_element_ok('q','abc');
  124. $successful_driver->default_finder('class');
  125. $successful_driver->type_element_ok('p','def','type_element_ok works with a locator');
  126. $successful_driver->element_text_is('q','abc','element has a correct text');
  127. $successful_driver->element_text_is('p','class','def');
  128. $successful_driver->element_value_is('p','class','foo');
  129. $successful_driver->click_element_ok('p','class','click_element_ok works');
  130. #Test for regression of #340
  131. is(exception { $successful_driver->click_element_ok('//p') }, undef, "1-arg click_element_ok call does not croak");
  132. $successful_driver->clear_element_ok('q','element is cleared ok');
  133. $successful_driver->is_element_enabled_ok('p','class','element is enabled');
  134. $successful_driver->is_element_displayed_ok('q','element is displayed');
  135. test_out('not ok 1 - Content is ok', 'ok 2 - No error checking content');
  136. ok(!exception { $successful_driver->content_like( qr/nomatch/, 'Content is ok') }, 'No error checking content');
  137. test_test(title => "Can fail 'content_like'",skip_err => 1);
  138. test_out('not ok 1 - Content is ok', 'ok 2 - No error checking content');
  139. ok(!exception { $successful_driver->content_unlike(qr/matches/, 'Content is ok') }, 'No error checking content');
  140. test_test(title => "Can fail 'content_unlike'",skip_err => 1);
  141. test_out('not ok 1 - Content is ok', 'ok 2 - No error checking content');
  142. ok(!exception { $successful_driver->content_contains('blah', 'Content is ok') }, 'No error checking content');
  143. test_test(title => "Can fail 'content_contains'",skip_err => 1);
  144. test_out('not ok 1 - Content is ok', 'ok 2 - No error checking content');
  145. ok(!exception { $successful_driver->content_lacks('matches', 'Content is ok') }, 'No error checking content');
  146. test_test(title => "Error handler works with 'content_lacks'",skip_err => 1);
  147. test_out('not ok 1 - Body is ok', 'ok 2 - No error checking body text');
  148. ok(!exception { $successful_driver->body_text_like( qr/nomatch/, 'Body is ok') }, 'No error checking body text');
  149. test_test(title => "Error handler works with 'body_text_like'",skip_err => 1);
  150. test_out('not ok 1 - Body is ok', 'ok 2 - No error checking body text');
  151. ok(!exception { $successful_driver->body_text_unlike(qr/matches/, 'Body is ok') }, 'No error checking body text');
  152. test_test(title => "Error handler works with 'body_text_unlike'",skip_err => 1);
  153. test_out('not ok 1 - Body is ok', 'ok 2 - No error checking body text');
  154. ok(!exception { $successful_driver->body_text_contains('nomatch', 'Body is ok') }, 'No error checking body text');
  155. test_test(title => "Error handler works with 'body_text_contains'",skip_err => 1);
  156. # This one is just a little tricky: 'match' is a match, so body_text_lacks should report failure, and the opposite for 'bar'
  157. test_out('not ok 1 - Body is ok','ok 2 - Body is ok', 'ok 3 - No error checking body text');
  158. ok(!exception { $successful_driver->body_text_lacks(['match','bar'], 'Body is ok') }, 'No error checking body text');
  159. test_test(title => "Error handler works with 'body_text_lacks'",skip_err => 1);
  160. $successful_driver->error_handler(sub { my ($self,$msg) = @_; croak "Got message: $msg";});
  161. test_out('not ok 1 - Content is ok'."\n".'ok 2 - Error callback triggered');
  162. like(exception { $successful_driver->content_like( qr/nomatch/, 'Content is ok') },qr/^Got message/,'Error callback triggered');
  163. test_test(title => "Error handler works with 'content_like'",skip_err => 1);
  164. test_out('not ok 1 - Content is ok'."\n".'ok 2 - Error callback triggered');
  165. like(exception { $successful_driver->content_unlike(qr/matches/, 'Content is ok') },qr/^Got message/,'Error callback triggered');
  166. test_test(title => "Error handler works with 'content_unlike'",skip_err => 1);
  167. test_out('not ok 1 - Content is ok'."\n".'ok 2 - Error callback triggered');
  168. like(exception { $successful_driver->content_contains('blah', 'Content is ok') },qr/^Got message/,'Error callback triggered');
  169. test_test(title => "Error handler works with 'content_contains'",skip_err => 1);
  170. test_out('not ok 1 - Content is ok'."\n".'ok 2 - Error callback triggered');
  171. like(exception { $successful_driver->content_lacks('matches', 'Content is ok') },qr/^Got message/,'Error callback triggered');
  172. test_test(title => "Error handler works with 'content_lacks'",skip_err => 1);
  173. test_out('not ok 1 - Body is ok'."\n".'ok 2 - Error callback triggered');
  174. like(exception { $successful_driver->body_text_like( qr/nomatch/, 'Body is ok') },qr/^Got message/,'Error callback triggered');
  175. test_test(title => "Error handler works with 'body_text_like'",skip_err => 1);
  176. test_out('not ok 1 - Body is ok'."\n".'ok 2 - Error callback triggered');
  177. like(exception { $successful_driver->body_text_unlike(qr/matches/, 'Body is ok') },qr/^Got message/,'Error callback triggered');
  178. test_test(title => "Error handler works with 'body_text_unlike'",skip_err => 1);
  179. test_out('not ok 1 - Text contains "nomatch"'."\n".'ok 2 - Error callback triggered');
  180. like(exception { $successful_driver->body_text_contains('nomatch') },qr/^Got message/,'Error callback triggered');
  181. test_test(title => "Error handler works with 'body_text_contains'",skip_err => 1);
  182. test_out('not ok 1 - Text lacks "match"'."\n".'ok 2 - Error callback triggered');
  183. like(exception { $successful_driver->body_text_lacks(['match','bar']) },qr/^Got message/,'Error callback triggered');
  184. test_test(title => "Error handler works with 'body_text_lacks'",skip_err => 1);
  185. done_testing();