Skip to content

Commit

Permalink
[rust] Save discovered browser version (network request) in local met…
Browse files Browse the repository at this point in the history
…adata (#12443)
  • Loading branch information
bonigarcia authored Jul 31, 2023
1 parent 2516656 commit c0317a5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
48 changes: 44 additions & 4 deletions rust/src/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,43 @@ impl SeleniumManager for ChromeManager {
}

fn request_browser_version(&mut self) -> Result<Option<String>, Box<dyn Error>> {
Ok(Some(self.request_latest_browser_version_from_cft()?))
let browser_name = self.browser_name;
let browser_version;
let major_browser_version = self.get_major_browser_version();
let mut metadata = get_metadata(self.get_logger());

// First, browser version is checked in the local metadata
match get_browser_version_from_metadata(
&metadata.browsers,
browser_name,
&major_browser_version,
) {
Some(version) => {
self.get_logger().trace(format!(
"Browser with valid TTL. Getting {} version from metadata",
browser_name
));
browser_version = version;
self.set_browser_version(browser_version.clone());
}
_ => {
// If not in metadata, discover version using Chrome for Testing (CfT) endpoints
browser_version = self.request_latest_browser_version_from_cft()?;

let browser_ttl = self.get_browser_ttl();
if browser_ttl > 0 {
metadata.browsers.push(create_browser_metadata(
browser_name,
&major_browser_version,
&browser_version,
browser_ttl,
));
write_metadata(&metadata, self.get_logger());
}
}
}

Ok(Some(browser_version))
}

fn get_driver_url(&mut self) -> Result<String, Box<dyn Error>> {
Expand Down Expand Up @@ -563,6 +599,7 @@ impl SeleniumManager for ChromeManager {

if !self.is_browser_version_unstable()
&& !self.is_browser_version_stable()
&& !self.is_browser_version_empty()
&& major_browser_version_int < MIN_CHROME_VERSION_CFT
{
return Err(format_three_args(
Expand Down Expand Up @@ -590,15 +627,18 @@ impl SeleniumManager for ChromeManager {
}
_ => {
// If not in metadata, discover version using Chrome for Testing (CfT) endpoints
if self.is_browser_version_stable() {
if self.is_browser_version_stable() || self.is_browser_version_empty() {
browser_version = self.request_latest_browser_version_from_cft()?;
} else {
browser_version = self.request_fixed_browser_version_from_cft()?;
}
self.set_browser_version(browser_version.clone());

let browser_ttl = self.get_browser_ttl();
if browser_ttl > 0 && !self.is_browser_version_stable() {
if browser_ttl > 0
&& !self.is_browser_version_empty()
&& !self.is_browser_version_stable()
{
metadata.browsers.push(create_browser_metadata(
browser_name,
&major_browser_version,
Expand Down Expand Up @@ -626,7 +666,7 @@ impl SeleniumManager for ChromeManager {
let browser_url = if let Some(url) = self.browser_url.clone() {
url
} else {
if self.is_browser_version_stable() {
if self.is_browser_version_stable() || self.is_browser_version_empty() {
self.request_latest_browser_version_from_cft()?;
} else {
self.request_fixed_browser_version_from_cft()?;
Expand Down
8 changes: 6 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ pub trait SeleniumManager {
self.get_browser_name(),
major_online_browser_version,
));
self.set_browser_version(version);
}
}
} else if !major_browser_version.is_empty()
Expand Down Expand Up @@ -433,9 +434,12 @@ pub trait SeleniumManager {
|| browser_version.eq_ignore_ascii_case(CANARY)
}

fn is_browser_version_empty(&self) -> bool {
self.get_browser_version().is_empty()
}

fn is_browser_version_stable(&self) -> bool {
let browser_version = self.get_browser_version();
browser_version.is_empty() || browser_version.eq_ignore_ascii_case(STABLE)
self.get_browser_version().eq_ignore_ascii_case(STABLE)
}

fn resolve_driver(&mut self) -> Result<PathBuf, Box<dyn Error>> {
Expand Down

0 comments on commit c0317a5

Please sign in to comment.