forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirefox.rs
315 lines (285 loc) · 12.2 KB
/
firefox.rs
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use crate::config::ManagerConfig;
use reqwest::Client;
use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;
use crate::config::ARCH::{ARM64, X32};
use crate::config::OS::{LINUX, MACOS, WINDOWS};
use crate::downloads::read_redirect_from_link;
use crate::files::{compose_driver_path_in_cache, BrowserPath};
use crate::metadata::{
create_driver_metadata, get_driver_version_from_metadata, get_metadata, write_metadata,
};
use crate::{
create_http_client, Logger, SeleniumManager, BETA, DASH_VERSION, DEV, NIGHTLY,
OFFLINE_REQUEST_ERR_MSG, REG_CURRENT_VERSION_ARG, STABLE,
};
pub const FIREFOX_NAME: &str = "firefox";
pub const GECKODRIVER_NAME: &str = "geckodriver";
const DRIVER_URL: &str = "https://github.com/mozilla/geckodriver/releases/";
const LATEST_RELEASE: &str = "latest";
pub struct FirefoxManager {
pub browser_name: &'static str,
pub driver_name: &'static str,
pub config: ManagerConfig,
pub http_client: Client,
pub log: Logger,
}
impl FirefoxManager {
pub fn new() -> Result<Box<Self>, Box<dyn Error>> {
let browser_name = FIREFOX_NAME;
let driver_name = GECKODRIVER_NAME;
let config = ManagerConfig::default(browser_name, driver_name);
let default_timeout = config.timeout.to_owned();
let default_proxy = &config.proxy;
Ok(Box::new(FirefoxManager {
browser_name,
driver_name,
http_client: create_http_client(default_timeout, default_proxy)?,
config,
log: Logger::default(),
}))
}
}
impl SeleniumManager for FirefoxManager {
fn get_browser_name(&self) -> &str {
self.browser_name
}
fn get_http_client(&self) -> &Client {
&self.http_client
}
fn set_http_client(&mut self, http_client: Client) {
self.http_client = http_client;
}
fn get_browser_path_map(&self) -> HashMap<BrowserPath, &str> {
HashMap::from([
(
BrowserPath::new(WINDOWS, STABLE),
r#"Mozilla Firefox\firefox.exe"#,
),
(
BrowserPath::new(WINDOWS, BETA),
r#"Mozilla Firefox\firefox.exe"#,
),
(
BrowserPath::new(WINDOWS, DEV),
r#"Firefox Developer Edition\firefox.exe"#,
),
(
BrowserPath::new(WINDOWS, NIGHTLY),
r#"Firefox Nightly\firefox.exe"#,
),
(
BrowserPath::new(MACOS, STABLE),
r#"/Applications/Firefox.app/Contents/MacOS/firefox"#,
),
(
BrowserPath::new(MACOS, BETA),
r#"/Applications/Firefox.app/Contents/MacOS/firefox"#,
),
(
BrowserPath::new(MACOS, DEV),
r#"/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox"#,
),
(
BrowserPath::new(MACOS, NIGHTLY),
r#"/Applications/Firefox Nightly.app/Contents/MacOS/firefox"#,
),
(BrowserPath::new(LINUX, STABLE), "/usr/bin/firefox"),
(BrowserPath::new(LINUX, BETA), "/usr/bin/firefox"),
(BrowserPath::new(LINUX, DEV), "/usr/bin/firefox"),
(BrowserPath::new(LINUX, NIGHTLY), "/usr/bin/firefox-trunk"),
])
}
fn discover_browser_version(&mut self) -> Result<Option<String>, Box<dyn Error>> {
self.discover_general_browser_version(
r#"HKCU\Software\Mozilla\Mozilla Firefox"#,
REG_CURRENT_VERSION_ARG,
DASH_VERSION,
)
}
fn get_driver_name(&self) -> &str {
self.driver_name
}
fn request_driver_version(&mut self) -> Result<String, Box<dyn Error>> {
let major_browser_version_binding = self.get_major_browser_version();
let major_browser_version = major_browser_version_binding.as_str();
let mut metadata = get_metadata(self.get_logger(), self.get_cache_path()?);
match get_driver_version_from_metadata(
&metadata.drivers,
self.driver_name,
major_browser_version,
) {
Some(driver_version) => {
self.log.trace(format!(
"Driver TTL is valid. Getting {} version from metadata",
&self.driver_name
));
Ok(driver_version)
}
_ => {
self.assert_online_or_err(OFFLINE_REQUEST_ERR_MSG)?;
let latest_url = format!("{}{}", DRIVER_URL, LATEST_RELEASE);
let driver_version =
read_redirect_from_link(self.get_http_client(), latest_url, self.get_logger())?;
let driver_ttl = self.get_driver_ttl();
if driver_ttl > 0 && !major_browser_version.is_empty() {
metadata.drivers.push(create_driver_metadata(
major_browser_version,
self.driver_name,
&driver_version,
driver_ttl,
));
write_metadata(&metadata, self.get_logger(), self.get_cache_path()?);
}
Ok(driver_version)
}
}
}
fn request_browser_version(&mut self) -> Result<Option<String>, Box<dyn Error>> {
Ok(None)
}
fn get_driver_url(&mut self) -> Result<String, Box<dyn Error>> {
let driver_version = self.get_driver_version();
let os = self.get_os();
let arch = self.get_arch();
// As of 0.32.0, geckodriver ships aarch64 binaries for Linux and Windows
// https://github.com/mozilla/geckodriver/releases/tag/v0.32.0
let minor_driver_version = self
.get_minor_version(driver_version)?
.parse::<i32>()
.unwrap_or_default();
let driver_label = if WINDOWS.is(os) {
if X32.is(arch) {
"win32.zip"
} else if ARM64.is(arch) && minor_driver_version > 31 {
"win-aarch64.zip"
} else {
"win64.zip"
}
} else if MACOS.is(os) {
if ARM64.is(arch) {
"macos-aarch64.tar.gz"
} else {
"macos.tar.gz"
}
} else if X32.is(arch) {
"linux32.tar.gz"
} else if ARM64.is(arch) && minor_driver_version > 31 {
"linux-aarch64.tar.gz"
} else {
"linux64.tar.gz"
};
Ok(format!(
"{}download/v{}/{}-v{}-{}",
DRIVER_URL, driver_version, self.driver_name, driver_version, driver_label
))
}
fn get_driver_path_in_cache(&self) -> Result<PathBuf, Box<dyn Error>> {
let driver_version = self.get_driver_version();
let os = self.get_os();
let arch = self.get_arch();
let minor_driver_version = self
.get_minor_version(driver_version)
.unwrap_or_default()
.parse::<i32>()
.unwrap_or_default();
let arch_folder = if WINDOWS.is(os) {
if X32.is(arch) {
"win32"
} else if ARM64.is(arch) && minor_driver_version > 31 {
"win-arm64"
} else {
"win64"
}
} else if MACOS.is(os) {
if ARM64.is(arch) {
"mac-arm64"
} else {
"mac64"
}
} else if X32.is(arch) {
"linux32"
} else if ARM64.is(arch) && minor_driver_version > 31 {
"linux-arm64"
} else {
"linux64"
};
Ok(compose_driver_path_in_cache(
self.get_cache_path()?,
self.driver_name,
os,
arch_folder,
driver_version,
))
}
fn get_config(&self) -> &ManagerConfig {
&self.config
}
fn get_config_mut(&mut self) -> &mut ManagerConfig {
&mut self.config
}
fn set_config(&mut self, config: ManagerConfig) {
self.config = config;
}
fn get_logger(&self) -> &Logger {
&self.log
}
fn set_logger(&mut self, log: Logger) {
self.log = log;
}
fn download_browser(&mut self) -> Result<Option<PathBuf>, Box<dyn Error>> {
Ok(None)
}
}
#[cfg(test)]
mod unit_tests {
use super::*;
#[test]
fn test_driver_url() {
let mut firefox_manager = FirefoxManager::new().unwrap();
let data = vec!(
vec!("0.32.0", "linux", "x86", "https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-linux32.tar.gz"),
vec!("0.32.0", "linux", "x86_64", "https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-linux64.tar.gz"),
vec!("0.32.0", "linux", "aarch64", "https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-linux-aarch64.tar.gz"),
vec!("0.32.0", "windows", "x86", "https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-win32.zip"),
vec!("0.32.0", "windows", "x86_64", "https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-win64.zip"),
vec!("0.32.0", "windows", "aarch64", "https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-win-aarch64.zip"),
vec!("0.32.0", "macos", "x86", "https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-macos.tar.gz"),
vec!("0.32.0", "macos", "x86_64", "https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-macos.tar.gz"),
vec!("0.32.0", "macos", "aarch64", "https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-macos-aarch64.tar.gz"),
vec!("0.31.0", "linux", "x86", "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-linux32.tar.gz"),
vec!("0.31.0", "linux", "x86_64", "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-linux64.tar.gz"),
vec!("0.31.0", "linux", "aarch64", "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-linux64.tar.gz"),
vec!("0.31.0", "windows", "x86", "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-win32.zip"),
vec!("0.31.0", "windows", "x86_64", "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-win64.zip"),
vec!("0.31.0", "windows", "aarch64", "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-win64.zip"),
vec!("0.31.0", "macos", "x86", "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-macos.tar.gz"),
vec!("0.31.0", "macos", "x86_64", "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-macos.tar.gz"),
vec!("0.31.0", "macos", "aarch64", "https://github.com/mozilla/geckodriver/releases/download/v0.31.0/geckodriver-v0.31.0-macos-aarch64.tar.gz"),
);
data.iter().for_each(|d| {
firefox_manager.set_driver_version(d.first().unwrap().to_string());
firefox_manager.set_os(d.get(1).unwrap().to_string());
firefox_manager.set_arch(d.get(2).unwrap().to_string());
let driver_url = firefox_manager.get_driver_url().unwrap();
assert_eq!(d.get(3).unwrap().to_string(), driver_url);
});
}
}