-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathselenium.pm
196 lines (158 loc) · 5.3 KB
/
selenium.pm
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
# SUSE's openQA tests
#
# Copyright 2017-2018 SUSE LLC
# SPDX-License-Identifier: FSFAP
# Summary: Support for Selenium
# Maintainer: Ondrej Holecek [email protected]>
package selenium;
use 5.018;
use warnings;
use base 'Exporter';
use Exporter;
our @EXPORT = qw(
add_chromium_repos
install_chromium
enable_selenium_port
selenium_driver
wait_for_page_to_load
wait_for_link
wait_for_text
wait_for_xpath
select_input
);
use testapi;
use utils 'zypper_call';
use opensusebasetest;
use version_utils 'is_sle';
use Selenium::Remote::Driver;
use Selenium::Chrome;
use Selenium::Waiter 'wait_until';
use Selenium::Remote::WDKeys;
my $port = 4444;
=head1 openQA - selenium webdriver support
This Selenium module exports subroutines helpers to integrate C<Selenium::Remote::Driver>
into openQA tests
Note: Selenium module works only when openvswitch networking is used!
Chromedriver and chromium are installed on SUT, firewall is enabled. OpenQA worker then initiate
connection to SUT and returns C<Selenium::Remote::Driver> to be used in tests.
Usage:
use selenium;
add_chromium_repos; # for SLES12
install_chromium;
enable_selenium_port;
my $driver = selenium_driver;
...
=cut
sub add_chromium_repos {
my $ret = zypper_call("se chromedriver", exitcode => [0, 104]);
if ($ret == 104) {
if (is_sle('<15')) {
zypper_call(
'--gpg-auto-import-keys ar -fc http://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-12/standard/openSUSE:Backports:SLE-12.repo');
zypper_call(
'--gpg-auto-import-keys ar -fc http://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-12-SP1/standard/openSUSE:Backports:SLE-12-SP1.repo'
);
zypper_call(
'--gpg-auto-import-keys ar -fc http://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-12-SP2/standard/openSUSE:Backports:SLE-12-SP2.repo'
);
zypper_call(
'--gpg-auto-import-keys ar -fc http://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-12-SP3/standard/openSUSE:Backports:SLE-12-SP3.repo'
);
}
elsif (is_sle('>=15')) {
zypper_call(
'--gpg-auto-import-keys ar -fc http://download.opensuse.org/repositories/openSUSE:/Backports:/SLE-15/standard/openSUSE:Backports:SLE-15.repo');
}
zypper_call('--gpg-auto-import-keys ref');
}
}
sub install_chromium {
zypper_call('in chromium chromedriver');
}
sub enable_selenium_port {
assert_script_run('systemctl stop ' . opensusebasetest::firewall());
}
my $driver;
sub selenium_driver {
return $driver if $driver;
die "Selenium support works only with openvswitch and tap devices" unless check_var('NICTYPE', 'tap');
my @mac_parts = split(':', get_var('NICMAC'));
my $sut = "10.1." . hex($mac_parts[4]) . '.' . hex($mac_parts[5]);
select_console('x11');
x11_start_program('xterm');
assert_screen('xterm');
enter_cmd("/usr/lib64/chromium/chromedriver --port=$port --url-base=wd/hub --whitelisted-ips | tee /dev/$serialdev");
save_screenshot;
wait_serial(qr(Starting ChromeDriver .* on port $port));
save_screenshot;
# HACK: this connection is only possible because the SUT initiated
# connection to 10.0.2.2 before (in script_output) so the openvswitch
# routing tables are filled
$driver = Selenium::Chrome->new(remote_server_addr => $sut, port => $port);
# https://github.com/teodesian/Selenium-Remote-Driver/issues/367
$driver->{is_wd3} = 0;
return $driver;
}
sub wait_for_page_to_load {
my ($timeout) = @_;
return wait_until {
$driver->execute_script("return document.readyState") eq 'complete'
}, timeout => $timeout;
}
sub wait_for {
my ($type, $target, @args) = @_;
my %args = (
-tries => 5,
-wait => 1,
-reload_after_tries => 5,
@args
);
die 'Unknown wait type' unless grep(/$type/, qw(text link xpath));
diag "waiting for ${type}: ${target}";
my $i = 0;
while ($i < $args{-tries}) {
save_screenshot;
my $element;
if ($type eq 'text') {
return 1 if $driver->get_page_source() =~ /$target/;
}
elsif ($type eq 'link') {
$element = $driver->find_element_by_partial_link_text($target);
}
elsif ($type eq 'xpath') {
$element = $driver->find_element_by_xpath($target);
}
if ($element) {
$driver->execute_script("arguments[0].scrollIntoView(false);", $element);
sleep 1;
save_screenshot;
return $element;
}
if (($i > 0) && ($i % $args{-reload_after_tries} == 0)) {
print "reload\n";
$driver->refresh();
wait_for_page_to_load;
}
sleep $args{-wait};
$i++;
}
print $driver->get_page_source();
die "$target not found on the page";
}
sub wait_for_link {
return wait_for('link', @_);
}
sub wait_for_text {
return wait_for('text', @_);
}
sub wait_for_xpath {
return wait_for('xpath', @_);
}
sub select_input {
my ($id) = @_;
$driver->mouse_move_to_location(element => wait_for_xpath("//input[\@id=\'$id\']"));
$driver->click();
$driver->send_keys_to_active_element(KEYS->{control}, 'a');
$driver->send_keys_to_active_element(KEYS->{control});
}
1;