10-switch-to-window.t 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use strict;
  2. use warnings;
  3. use Test::More;
  4. use Test::Selenium::Remote::Driver;
  5. use Selenium::Remote::Mock::RemoteConnection;
  6. use FindBin;
  7. use lib $FindBin::Bin . '/lib';
  8. use TestHarness;
  9. my $harness = TestHarness->new(
  10. this_file => $FindBin::Script
  11. );
  12. my @browsers = qw/chrome firefox/;
  13. foreach (@browsers) {
  14. my %selenium_args = (
  15. default_finder => 'css',
  16. javascript => 1,
  17. %{ $harness->base_caps },
  18. browser_name => $_,
  19. );
  20. my $s = Test::Selenium::Remote::Driver->new(
  21. %selenium_args
  22. );
  23. my $perl_title = 'The Perl Programming Language - www.perl.org';
  24. my $cpan_title = 'The Comprehensive Perl Archive Network - www.cpan.org';
  25. $s->get_ok('http://perl.org/');
  26. $s->title_is($perl_title, 'perl.org title matches correctly');
  27. my $old_handles = $s->get_window_handles;
  28. is(scalar(@$old_handles), 1, 'got one window handle as expected');
  29. my $perl_handle = $old_handles->[0];
  30. # setting the window.name manually
  31. $s->execute_script(q{return window.name = 'perlorg';});
  32. # setting the window.name when opening the other window
  33. $s->execute_script(q{$(window.open('http://cpan.org/', 'cpanorg'))});
  34. $s->title_is($perl_title, 'creating a new window keeps us focused on the current window');
  35. my $handles = $s->get_window_handles;
  36. is(scalar(@$handles), 2, 'get_window_handles sees both of our browser windows');
  37. # We don't assume any order in the @$handles array:
  38. my $cpan_handle = $perl_handle eq $handles->[0] ? $handles->[1] : $handles->[0];
  39. $s->switch_to_window($cpan_handle);
  40. $s->get_ok('http://cpan.org');
  41. $s->title_is($cpan_title, 'can switch to window by handle');
  42. $s->switch_to_window($perl_handle);
  43. $s->title_is($perl_title, 'can switch back to main window by handle');
  44. if ($_ eq 'chrome') {
  45. $s->switch_to_window('cpanorg');
  46. $s->title_is($cpan_title, 'can switch to window by window title in chrome');
  47. $s->switch_to_window('perlorg');
  48. $s->title_is($perl_title, 'can switch to main window by window title in chrome');
  49. }
  50. }
  51. done_testing;