Test-Selenium-Remote-Driver.t 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 => '123456' } };
  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 $spec = {
  54. findElement => $find_element,
  55. findChildElement => $find_child_element,
  56. getPageSource => sub { return 'this output matches regex'},
  57. findElements => $find_elements,
  58. findChildElements => $find_child_element,
  59. sendKeysToElement => $send_keys,
  60. };
  61. my $mock_commands = Selenium::Remote::Mock::Commands->new;
  62. my $successful_driver =
  63. Test::Selenium::Remote::Driver->new(
  64. remote_conn => Selenium::Remote::Mock::RemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
  65. commands => $mock_commands,
  66. );
  67. $successful_driver->find_element_ok('q','find_element_ok works');
  68. $successful_driver->find_element_ok('p','class','find_element_ok with a locator works');
  69. $successful_driver->find_child_element_ok({id => 1},'p','class','find_child_element_ok with a locator works');
  70. dies_ok{ $successful_driver->find_child_element_ok({id => 1200}) } 'find_child_element_ok dies if the element is not found';
  71. dies_ok { $successful_driver->find_element_ok('notq') } 'find_element_ok dies if element not found';
  72. $successful_driver->find_no_element_ok('notq','xpath','find_no_element_ok works');
  73. $successful_driver->content_like( qr/matches/, 'content_like works');
  74. $successful_driver->content_unlike( qr/nomatch/, 'content_unlike works');
  75. $successful_driver->find_elements_ok('abc','find_elements_ok works');
  76. $successful_driver->find_child_elements_ok({id => 1},'p','find_child_elements_ok works');
  77. $successful_driver->type_element_ok('q','abc');
  78. $successful_driver->type_element_ok('p','class','def','type_element_ok works with a locator');
  79. done_testing();