10-switch-to-window.t 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. use strict;
  2. use warnings;
  3. use 5.010;
  4. use Test::More;
  5. use Test::Selenium::Remote::Driver;
  6. use Selenium::Remote::Mock::RemoteConnection;
  7. my $record = (defined $ENV{'WD_MOCKING_RECORD'} && ($ENV{'WD_MOCKING_RECORD'}==1))?1:0;
  8. my $os = $^O;
  9. if ($os =~ m/(aix|freebsd|openbsd|sunos|solaris)/) {
  10. $os = 'linux';
  11. }
  12. my %selenium_args = (
  13. browser_name => 'firefox',
  14. default_finder => 'css',
  15. javascript => 1,
  16. );
  17. my $mock_file = "10-switch-to-window-mock-$os.json";
  18. if (!$record && !(-e "t/mock-recordings/$mock_file")) {
  19. plan skip_all => "Mocking of tests is not been enabled for this platform";
  20. }
  21. if ($record) {
  22. $selenium_args{remote_conn} = Selenium::Remote::Mock::RemoteConnection->new(record => 1);
  23. }
  24. else {
  25. $selenium_args{remote_conn} =
  26. Selenium::Remote::Mock::RemoteConnection->new( replay => 1,
  27. replay_file => "t/mock-recordings/$mock_file" );
  28. }
  29. plan tests => 9;
  30. my $s = Test::Selenium::Remote::Driver->new(
  31. %selenium_args
  32. );
  33. my $perl_title = 'The Perl Programming Language - www.perl.org';
  34. my $cpan_title = 'The Comprehensive Perl Archive Network - www.cpan.org';
  35. $s->get_ok('http://perl.org/');
  36. $s->title_is($perl_title);
  37. my $old_handles = $s->get_window_handles;
  38. is scalar(@$old_handles), 1;
  39. my $perl_handle = $old_handles->[0];
  40. # setting the window.name manually
  41. $s->execute_script(q{return window.name = 'perlorg';});
  42. # setting the window.name when opening the other window
  43. $s->execute_script(q{$(window.open('http://cpan.org/', 'cpanorg'))});
  44. $s->title_is($perl_title);
  45. my $handles = $s->get_window_handles;
  46. is scalar(@$handles), 2;
  47. # We don't assume any order in the @$handles array:
  48. my $cpan_handle = $perl_handle eq $handles->[0] ? $handles->[1] : $handles->[0];
  49. diag explain $handles;
  50. $s->switch_to_window($cpan_handle);
  51. $s->title_is($cpan_title);
  52. $s->switch_to_window($perl_handle);
  53. $s->title_is($perl_title);
  54. $s->switch_to_window('cpanorg');
  55. $s->title_is($cpan_title);
  56. $s->switch_to_window('perlorg');
  57. $s->title_is($perl_title);
  58. if ($record) {
  59. $s->remote_conn->dump_session_store("t/mock-recordings/$mock_file");
  60. }