10-switch-to-window.t 2.3 KB

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