Skip to content

Commit b3d1919

Browse files
committed
Add ability to add extensions to Chrome as well as other options
1 parent 8451208 commit b3d1919

1 file changed

Lines changed: 90 additions & 11 deletions

File tree

src/common/capabilities/chrome.rs

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use serde_json::{from_value, json, to_value, Value};
33

44
use crate::common::capabilities::desiredcapabilities::Capabilities;
55
use crate::error::WebDriverResult;
6+
use serde::de::DeserializeOwned;
7+
use std::path::Path;
68

79
#[derive(Debug, Clone, Serialize)]
810
#[serde(transparent)]
@@ -26,6 +28,54 @@ impl ChromeCapabilities {
2628
ChromeCapabilities::default()
2729
}
2830

31+
/// Add the specified Chrome option. This is a helper method for `add_chrome_arg()`.
32+
pub fn add_chrome_option<T>(&mut self, key: &str, value: T) -> WebDriverResult<()>
33+
where
34+
T: Serialize,
35+
{
36+
self.add_subkey("goog:chromeOptions", key, value)
37+
}
38+
39+
/// Get the specified Chrome option.
40+
pub fn get_chrome_option<T>(&self, key: &str) -> T
41+
where
42+
T: DeserializeOwned + Default,
43+
{
44+
from_value(self.capabilities["goog:chromeOptions"][key].clone()).unwrap_or_default()
45+
}
46+
47+
/// Get the current list of command-line arguments to `chromedriver` as a vec.
48+
pub fn get_args(&self) -> Vec<String> {
49+
self.get_chrome_option("args")
50+
}
51+
52+
/// Get the current list of Chrome extensions as a vec.
53+
/// Each item is a base64-encoded string containing the .CRX extension file contents.
54+
/// Use `add_extension()` to add a new extension file.
55+
pub fn get_extensions(&self) -> Vec<String> {
56+
self.get_chrome_option("extensions")
57+
}
58+
59+
/// Get the path to the chrome binary (if one was previously set).
60+
pub fn get_binary(&self) -> String {
61+
self.get_chrome_option("binary")
62+
}
63+
64+
/// Set the path to chrome binary to use.
65+
pub fn set_binary(&mut self, path: &str) -> WebDriverResult<()> {
66+
self.add_chrome_option("binary", path)
67+
}
68+
69+
/// Get the current debugger address (if one was previously set).
70+
pub fn get_debugger_address(&self) -> String {
71+
self.get_chrome_option("debuggerAddress")
72+
}
73+
74+
/// Set the debugger address.
75+
pub fn set_debugger_address(&mut self, address: &str) -> WebDriverResult<()> {
76+
self.add_chrome_option("debuggerAddress", address)
77+
}
78+
2979
/// Add the specified command-line argument to `chromedriver`. Eg. "--disable-local-storage"
3080
/// The full list of switches can be found here:
3181
/// [https://chromium.googlesource.com/chromium/src/+/master/chrome/common/chrome_switches.cc](https://chromium.googlesource.com/chromium/src/+/master/chrome/common/chrome_switches.cc)
@@ -38,6 +88,7 @@ impl ChromeCapabilities {
3888
self.add_chrome_option("args", to_value(args)?)
3989
}
4090

91+
/// Remove the specified Chrome command-line argument if it had been added previously.
4192
pub fn remove_chrome_arg(&mut self, arg: &str) -> WebDriverResult<()> {
4293
let mut args = self.get_args();
4394
if args.is_empty() {
@@ -48,24 +99,52 @@ impl ChromeCapabilities {
4899
}
49100
}
50101

51-
/// Add the specified chrome option. This is a helper method for `add_chrome_arg()`.
52-
pub fn add_chrome_option<T>(&mut self, key: &str, value: T) -> WebDriverResult<()>
53-
where
54-
T: Serialize,
55-
{
56-
self.add_subkey("goog:chromeOptions", key, value)
102+
/// Add a base64-encoded extension.
103+
pub fn add_encoded_extension(&mut self, extension_base64: &str) -> WebDriverResult<()> {
104+
let mut extensions = self.get_extensions();
105+
let ext_string = extension_base64.to_string();
106+
if !extensions.contains(&ext_string) {
107+
extensions.push(ext_string);
108+
}
109+
self.add_chrome_option("extensions", to_value(extensions)?)
57110
}
58111

59-
/// Get the current list of command-line arguments to `chromedriver` as a vec.
60-
pub fn get_args(&self) -> Vec<String> {
61-
from_value(self.capabilities["goog:chromeOptions"]["args"].clone()).unwrap_or_default()
112+
/// Remove the specified base64-encoded extension if it had been added previously.
113+
pub fn remove_encoded_extension(&mut self, extension_base64: &str) -> WebDriverResult<()> {
114+
let mut extensions = self.get_extensions();
115+
if extensions.is_empty() {
116+
Ok(())
117+
} else {
118+
extensions.retain(|v| v != extension_base64);
119+
self.add_chrome_option("extensions", to_value(extensions)?)
120+
}
121+
}
122+
123+
/// Add Chrome extension file. This will be a file with a .CRX extension.
124+
pub fn add_extension(&mut self, crx_file: &Path) -> WebDriverResult<()> {
125+
let contents = std::fs::read(crx_file)?;
126+
let b64_contents = base64::encode(contents);
127+
self.add_encoded_extension(&b64_contents)
128+
}
129+
130+
/// Remove the specified Chrome extension file if an identical extension had been added
131+
/// previously.
132+
pub fn remove_extension(&mut self, crx_file: &Path) -> WebDriverResult<()> {
133+
let contents = std::fs::read(crx_file)?;
134+
let b64_contents = base64::encode(contents);
135+
self.remove_encoded_extension(&b64_contents)
62136
}
63137

64138
/// Set the browser to run headless.
65139
pub fn set_headless(&mut self) -> WebDriverResult<()> {
66140
self.add_chrome_arg("--headless")
67141
}
68142

143+
/// Unset the headless option.
144+
pub fn unset_headless(&mut self) -> WebDriverResult<()> {
145+
self.remove_chrome_arg("--headless")
146+
}
147+
69148
/// Set disable web security.
70149
pub fn set_disable_web_security(&mut self) -> WebDriverResult<()> {
71150
self.add_chrome_arg("--disable-web-security")
@@ -76,12 +155,12 @@ impl ChromeCapabilities {
76155
self.remove_chrome_arg("--disable-web-security")
77156
}
78157

79-
/// Sets accept untrusted certs.
158+
/// Set ignore certificate errors.
80159
pub fn set_ignore_certificate_errors(&mut self) -> WebDriverResult<()> {
81160
self.add_chrome_arg("--ignore-certificate-errors")
82161
}
83162

84-
/// Unsets accept untrusted certs.
163+
/// Unset ignore certificate errors.
85164
pub fn unset_ignore_certificate_errors(&mut self) -> WebDriverResult<()> {
86165
self.remove_chrome_arg("--ignore-certificate-errors")
87166
}

0 commit comments

Comments
 (0)