Test-Selenium-Remote-Driver.t 6.0 KB

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