1
0

Test-Selenium-Remote-Driver.t 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 => '123456' } };
  17. }
  18. return { status => 'NOK', return => 0, error => 'element not found' };
  19. };
  20. my $find_child_element = sub {
  21. my ( $session_object, $searched_item ) = @_;
  22. my $elem_id = $session_object->{id};
  23. if ( $elem_id == 1 && $searched_item->{value} eq 'p' ) {
  24. if ( $searched_item->{using} eq 'class name' ) {
  25. return { status => 'OK', return => { ELEMENT => '11223344' } };
  26. }
  27. if ( $searched_item->{using} eq 'xpath' ) {
  28. return { status => 'OK',
  29. return => [ { ELEMENT => '112' }, { ELEMENT => '113' } ] };
  30. }
  31. }
  32. return {
  33. status => 'NOK', return => 0,
  34. error => 'child element not found'
  35. };
  36. };
  37. my $find_elements = sub {
  38. my ( undef, $searched_expr ) = @_;
  39. if ( $searched_expr->{value} eq 'abc'
  40. && $searched_expr->{using} eq 'xpath' )
  41. {
  42. return { status => 'OK',
  43. return => [ { ELEMENT => '123456' }, { ELEMENT => '12341234' } ] };
  44. }
  45. };
  46. my $spec = {
  47. findElement => $find_element,
  48. findChildElement => $find_child_element,
  49. getPageSource => sub { return 'this output matches regex'},
  50. findElements => $find_elements,
  51. findChildElements => $find_child_element,
  52. };
  53. my $mock_commands = Selenium::Remote::Mock::Commands->new;
  54. my $successful_driver =
  55. Test::Selenium::Remote::Driver->new(
  56. remote_conn => Selenium::Remote::Mock::RemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
  57. commands => $mock_commands,
  58. );
  59. $successful_driver->find_element_ok('q','find_element_ok works');
  60. $successful_driver->find_element_ok('p','class','find_element_ok with a locator works');
  61. $successful_driver->find_child_element_ok({id => 1},'p','class','find_child_element_ok with a locator works');
  62. dies_ok{ $successful_driver->find_child_element_ok({id => 1200}) } 'find_child_element_ok dies if the element is not found';
  63. dies_ok { $successful_driver->find_element_ok('notq') } 'find_element_ok dies if element not found';
  64. $successful_driver->find_no_element_ok('notq','find_no_element_ok works');
  65. $successful_driver->content_like( qr/matches/, 'content_like works');
  66. $successful_driver->content_unlike( qr/nomatch/, 'content_unlike works');
  67. $successful_driver->find_elements_ok('abc','find_elements_ok works');
  68. $successful_driver->find_child_elements_ok({id => 1},'p','find_child_elements_ok works');
  69. done_testing();