1
0

Test-Selenium-Remote-Driver.t 10.0 KB

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