1
0

Test-Selenium-Remote-Driver.t 4.5 KB

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