Test-Selenium-Remote-Driver.t 5.2 KB

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