10-switch-to-window.t 2.1 KB

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