-
Notifications
You must be signed in to change notification settings - Fork 0
/
selenese
executable file
·265 lines (202 loc) · 5.26 KB
/
selenese
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/perl -CSAD
# Copyright 2016, Paul Johnson (paul@pjcj.net) http://www.pjcj.net
# This software is free. It is licensed under the same terms as Perl itself.
use 5.18.2;
use warnings;
use utf8::all;
use Data::Dumper;
use Getopt::Long;
use HTML::TreeBuilder;
use IO::HTML;
use Path::Tiny;
my $Indent = 1;
our $O = {};
my $Defaults = {
debug => 0,
debug_sub => sub { say "@_" if $O->{debug} },
driver => "PhantomJS",
info_sub => sub { say "@_" },
prove_options => "",
run => 1,
test_file => "./test.pl",
verbose => 0,
};
$Data::Dumper::Indent = 1;
$Data::Dumper::Purity = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Deparse = 1;
sub get_options {
my $o = {};
die "Bad option" unless GetOptions($o, qw(
conf=s
debug!
driver=s
prove_options=s
run!
test_file=s
verbose!
));
our $CONF = {};
if (my $file = $o->{conf}) {
unless (my $ret = do $file) {
warn "couldn't parse $file: $@" if $@;
warn "couldn't do $file: $!" unless defined $ret;
warn "couldn't run $file" unless $ret;
}
}
%$O = (%$Defaults, %$CONF, %$o);
$O->{begin}->() if $O->{begin};
$O->{info_sub}->("Options:", Dumper $O) if $O->{verbose};
}
sub fh {
state $fn = $O->{test_file};
state $fh;
unless ($fh) {
open $fh, ">", $fn or die "Can't open $fn: $!";
}
$fh
}
sub write_header {
my $o = Data::Dumper->Dump([$O], ["O"]);
$o =~ s/^/ /mg;
print { fh } <<"EOH";
#!$^X
use 5.18.2;
use warnings;
use utf8::all;
use Selenese;
use Test::More;
my \$O;
sub main {
select STDERR; \$|++;
select STDOUT; \$|++;
$o
my \$s = Selenese->new(
\%\$O
);
\$O->{selenese} = \$s;
my \$r = sub {
my \$ret = \$s->run_command(\@_) or return;
\$O->{info_sub}->("Command failed: \@_");
exit \$ret;
};
EOH
}
sub write_footer {
print { fh } <<'EOH';
$r->("quit");
done_testing;
0
}
main
EOH
}
sub write_line {
my ($str) = @_;
print { fh } (" " x ($Indent * 4)), $str, "\n";
}
sub tree {
my ($file) = @_;
my $tree = HTML::TreeBuilder->new_from_file(html_file($file));
$O->{debug_sub}->(tree => $tree);
$tree
}
sub commands {
my ($file) = @_;
$O->{info_sub}->("Generating sub-tests from $file") if $O->{verbose};
my $tree = tree($file);
my $tbody = $tree->find("tbody") or return;
my @cmds;
for my $tr ($tbody->find("tr", "~comment")) {
my @cmd = map $_->[0], grep defined, map $_->content, $tr->find("td");
$O->{debug_sub}->(cmd => \@cmd);
push @cmds, \@cmd;
}
@cmds
}
sub quote_params {
my (@params) = @_;
$O->{debug_sub}->(quote_params => \@params);
join ", ", map "q{$_}", map s/\\/\\\\/gr, grep defined, @params
}
my $Cmds = {
while => sub {
my ($expr) = @_;
write_line("while (\$s->run_command(_eval => " . quote_params($expr) . ")) {");
$Indent++;
},
endWhile => sub {
$Indent--;
write_line("}");
},
};
sub write_suite {
my ($file) = @_;
$O->{info_sub}->("Generating tests from $file") if $O->{verbose};
my $tree = tree($file);
$tree->dump if $O->{debug};
my $tbody = $tree->find("tbody") or return;
$O->{debug_sub}->(tbody => $tbody);
my $base_dir = path($file)->parent;
my @test_files =
map path($base_dir, $_->attr("href"))->realpath->stringify,
grep defined,
map $_->find("td")->find("a"),
$tbody->find("tr");
write_line(qq(note "$file";));
for my $test_file (@test_files) {
my $test = $test_file =~ s/.*\///r;
write_line(qq(subtest "$test" => sub {));
$Indent++;
write_line(qq(note "$base_dir/$test";));
for my $command (commands($test_file)) {
$command = $O->{map}->($test, $command) if $O->{map};
next unless $command;
my ($cmd, @params) = @$command;
my $sub = $Cmds->{$cmd};
if ($sub) {
$sub->(@params);
} else {
@params = ("") unless @params;
write_line("\$r->($cmd => " . quote_params(@params) . ");");
}
}
$Indent--;
write_line("};\n");
}
}
sub main {
get_options;
write_header;
for my $suite (@ARGV) {
write_suite($suite);
}
write_footer;
return 0 unless $O->{run};
system "prove $O->{prove_options} $O->{test_file}";
$? == -1 ? 1 : $? & 127 ? $? & 127 : $? >> 8
}
exit main
__END__
=head1 NAME
selenese - Run Selenium IDE tests without the IDE
=head1 SYNOPSIS
selenese
=head1 DESCRIPTION
Run Selenium IDE tests without the IDE.
=head1 OPTIONS
The following command line options are supported:
-h -help - show help
-i -info - show documentation
-v -version - show version
=head1 DETAILS
=head1 EXIT STATUS
The following exit values are returned:
0 All operations were completed successfully.
>0 An error occurred.
=head1 SEE ALSO
=head1 BUGS
=head1 LICENCE
Copyright 2016, Paul Johnson (paul@pjcj.net) http://www.pjcj.net
This software is free. It is licensed under the same terms as Perl itself.
=cut