-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish.pl
executable file
·184 lines (140 loc) · 4.58 KB
/
fish.pl
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
#!/usr/bin/perl
#
# Read YAML play, generate Ruby driver, execute Ruby driver
#
# 2016 (c) Paul Tchistopolskii, http://www.pault.com
#
# License is BSD
#
use strict;
no warnings 'all';
use feature qw(switch);
use YAML::XS;
use Data::Dumper;
my $yml_play = shift || die "Use: $0 play.yml";
my $y = YAML::XS::LoadFile( $yml_play );
#print Dumper($y);
die "Expecting list of pirimitives, got: ", Dumper($y) if (ref($y) ne "ARRAY");
my $VS = "";
my %VH = ();
# Globals
my $Browser = "ie";
# Bcode
my @BB = ();
sub B{ my @b = @_; push @BB, \@b; }
my %c2m = (
'text-name-set' =>
sub { B('set_text_by_name', $VH{'name'}, $VH{'set'}) },
'textarea-name-set' =>
sub { B('set_textarea_by_name', $VH{'name'}, $VH{'set'}) },
'radio-name-set' =>
sub { B('set_radio_by_name', $VH{'name'}, $VH{'set'}) },
'checkbox-name-set' =>
sub { B('set_checkbox_by_name', $VH{'name'}, $VH{'set'}) },
'select-name-set' =>
sub { B('set_select_by_name', $VH{'name'}, $VH{'set'}) },
'button-name-click' =>
sub { B('click_button_by_name', $VH{'name'}, $VH{'click'}) },
'page-get' =>
sub { B('get_page', $VH{'get'}) }
);
sub BcontrolH($){
my $ctype = shift;
my $name = $VH{'name'};
foreach my $op ( keys %VH ){
next if ($op eq "name");
my $param = $VH{$op};
my $hoo_key = "$ctype-$op";
$hoo_key = "$ctype-name-$op" if ($name);
my $cback = $c2m{$hoo_key};
&$cback();
die "($hoo_key) bad control: ", Dumper(\%VH) if (!$cback);
}
}
my %p2p = (
'browser-' => sub { $Browser = $VS },
'page-' => sub { B('goto_page', $VS) },
'sleep-' => sub { B('sleep', $VS) },
'text-HASH' => sub { BcontrolH('text') },
'textarea-HASH' => sub { BcontrolH('textarea') },
'radio-HASH' => sub { BcontrolH('radio') },
'checkbox-HASH' => sub { BcontrolH('checkbox') },
'select-HASH' => sub { BcontrolH('select') },
'button-HASH' => sub { BcontrolH('button') },
'page-HASH' => sub { BcontrolH('page') }
);
# Pass 1
# Foreach element - validate it and produce Bcode
foreach my $e ( @{$y} ){
$VS = ""; %VH = ();
die "Bad element! Expected hash, got: ", Dumper($e)
if (ref($e) ne "HASH");
die "Bad element! Expected 'name -> value', got: ", Dumper($e)
if (scalar(keys %{$e}) != 1);
my ($K, $V) = ( (keys %{$e})[0], $e->{(keys %{$e})[0]} );
#print Dumper($e);
#print "TYP:" , ref($V) . "\n";
$VS = $V if (ref(\$V) eq "SCALAR");
%VH = %{$V} if (ref($V) eq "HASH");
die "Bad element!", Dumper(\$V) if ( !$VS && !%VH );
#print "$K -> VS: $VS VH:", Dumper(\%VH), "\n";
my $hoo_key = "$K-" . ref($V);
my $cback = $p2p{$hoo_key};
die "($hoo_key) bad element: ", Dumper($e) if (!$cback);
&$cback();
}
#print Dumper(\@BB);
# Pass 2. Dress the tree a little bit
my @browser_b = ( 'browser', $Browser );
unshift @BB, \@browser_b;
#print Dumper(\@BB);
# Pass 3. Generate
unlink("FishDriver.rb");
open(OUT, ">FishDriver.rb") || die "Can't write Fish Driver";
print OUT "require \"watir\"\n";
foreach my $e ( @BB ){
my ($cmd, $p1, $p2) = @{$e};
#print Dumper($e);
given( $cmd ){
when( 'browser' ) { print OUT "b = Watir::Browser.new :$p1\n";}
when( 'goto_page' ) { print OUT "b.goto \"$p1\"\n";}
when( 'sleep' ) { print OUT "sleep $p1\n"; }
when( 'set_text_by_name' ){
print OUT "b.text_field( :name => '$p1' ).set '$p2'\n";
}
when( 'set_textarea_by_name' ){
print OUT "b.textarea( :name => '$p1' ).set '$p2'\n";
}
when( 'set_radio_by_name' ){
print OUT "b.radio( :name => '$p1', :value => '$p2' ).set\n";
}
when( 'set_checkbox_by_name' ){
print OUT "b.checkbox( :name => '$p1', :value => '$p2' ).set\n";
}
when( 'set_select_by_name' ){
print OUT "b.select_list( :name => '$p1').select '$p2'\n";
}
when( 'click_button_by_name' ){
print OUT "b.button( :name => '$p1' ).click\n";
}
when( 'get_page' ){
if ($p1 eq "html") {
print OUT "puts b.html()\n";
} else {
print OUT "puts b.text\n";
}
}
default{
die "Unknown cmd: $cmd", Dumper($e);
}
}
print OUT "sleep 1\n" if ($cmd ne 'sleep');
}
print OUT "sleep 3\nb.close\n";
close(OUT);
# We develop on *nix, we run on win
if ($^O =~ m/win/i) {
system "ruby FishDriver.rb";
} else {
system "cat FishDriver.rb";
}