File: selenium-rc.t

package info (click to toggle)
libtest-www-selenium-perl 1.36-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 436 kB
  • ctags: 189
  • sloc: perl: 2,382; xml: 1,459; makefile: 8
file content (153 lines) | stat: -rw-r--r-- 4,583 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 50;
use Test::Exception;
use Test::Mock::LWP;

BEGIN {
    use lib 'lib';
    use_ok 'WWW::Selenium';
    use_ok 'Test::WWW::Selenium';
}

Good_usage: {
    my $sel = WWW::Selenium->new( host => 'localhost', 
                                  port => 4444, 
                                  browser => '*firefox', 
                                  browser_url => 'http://foo.com'
                                );
    isa_ok $sel, 'WWW::Selenium';
    is $sel->{host}, 'localhost';
    is $sel->{port}, 4444;
    is $sel->{browser_start_command}, '*firefox';
    is $sel->{browser_url}, 'http://foo.com';
    is $sel->{session_id}, undef;

    Start_up_selenium: {
        $Mock_resp->mock( content => sub {'OK,SESSION_ID'} );
        $sel->start;
        is $sel->{session_id}, 'SESSION_ID';
        req_ok('cmd=getNewBrowserSession&1=%2Afirefox&2=http%3A%2F%2Ffoo.com');
        $Mock_resp->mock( content => sub { 'OK' } );
        $sel->open;
    }

    Execute_command: {
        $Mock_resp->mock( content => sub { 'OK,Some Title' } );
        is $sel->get_title, 'Some Title';
        req_ok('cmd=getTitle&sessionId=SESSION_ID');
    }

    Close_down_selenium: {
        $Mock_resp->mock( content => sub { 'OK' } );
        $sel->stop;
        is $sel->{session_id}, undef;
        req_ok('cmd=testComplete&sessionId=SESSION_ID');
    }
}

No_browser_url: {
    throws_ok { WWW::Selenium->new } qr#browser_url is mandatory#;
}

Default_args: {
    my $sel = WWW::Selenium->new( browser_url => 'http://foo.com' );
    is $sel->{host}, 'localhost';
    is $sel->{port}, 4444;
    is $sel->{browser_start_command}, '*firefox';
    is $sel->{browser_url}, 'http://foo.com';
    is $sel->{session_id}, undef;
}

start_fails: {
    my $sel = WWW::Selenium->new( browser_url => 'http://foo.com' );
    $Mock_resp->mock( content => sub { 'Error: foo' } );
    throws_ok { $sel->start } qr#Error: foo#;
}

Failing_command: {
    my $sel = WWW::Selenium->new( browser_url => 'http://foo.com' );
    $Mock_resp->mock( content => sub { 'OK,SESSION_ID' } );
    $sel->start;
    $sel->open;
    $Mock_resp->mock( content => sub { 'Error: foo' } );
    throws_ok { $sel->get_title } qr#Error: foo#;
    $Mock_resp->mock( content => sub { 'OK' } );
}
 
Multi_values: {
    my $sel = WWW::Selenium->new( browser_url => 'http://foo.com' );
    $Mock_resp->mock( content => sub { 'OK,SESSION_ID' } );
    $sel->start;
    $sel->open;

    my %testcases = (
            'one,two,three' => [qw(one two three)],
            'one\\,two'      => ['one,two'],
    );
    my %skip_testcases = (
            'veni\, vidi\, vici,c:\\foo\\bar,c:\\I came\, I \\saw\\\, I conquered',
                            => ['veni, vidi, vici',
                                'c:\foo\bar',
                                'c:\I came, I \saw\, I conquered',
                               ],
            'one\\\\,two'    => ['one\\,two'], 
            'one\\\\\\,two'  => ['one\\', 'two'],
    );
    my $tester = sub {
        my $tests = shift;
        for my $k (keys %$tests) {
            $Mock_resp->mock( content => sub { "OK,$k" } );
            my $fields = [$sel->get_all_fields];
            is_deeply $fields, $tests->{$k}, "parsing $k";
        }
    };
    $tester->(\%testcases);
    TODO: {
        local $TODO = 'Need to fix get_string_array';
        $tester->(\%skip_testcases);
    }
}

Stop_called_twice: {
    my $sel = WWW::Selenium->new( browser_url => 'http://foo.com' );
    $Mock_resp->mock( content => sub { 'OK,SESSION_ID' } );
    $sel->start;
    is $sel->{session_id}, 'SESSION_ID';
    $Mock_resp->mock( content => sub { 'OK' } );
    $sel->stop;
    is $sel->{session_id}, undef;
    req_ok('cmd=testComplete&sessionId=SESSION_ID');
    $sel->stop;
    is_deeply $Mock_req->new_args, undef;
}

With_session_id: {
    my $sel = Test::WWW::Selenium->new(
        browser_url => 'http://foo.com',
        session_id  => 'MY_ID',
    );
    $Mock_resp->mock( content => sub { die "Should never be called!" } );
    $sel->start;
    is $sel->{session_id}, 'MY_ID';
    $Mock_resp->mock( content => sub { 'OK' } );
    $sel->stop;
    is $sel->{session_id}, undef;
    req_ok('cmd=testComplete&sessionId=MY_ID');
    $sel->stop;
    is_deeply $Mock_req->new_args, undef;
}

exit;


sub req_ok {
    my $content = shift;
    my $args = $Mock_req->new_args;
    is $args->[0], 'HTTP::Request';
    is $args->[1], 'POST';
    is $args->[2], "http://localhost:4444/selenium-server/driver/";
    is $args->[4], $content;
}