diff --git a/R/find_browser.R b/R/find_browser.R new file mode 100644 index 00000000..8fd6a648 --- /dev/null +++ b/R/find_browser.R @@ -0,0 +1,98 @@ +# Tries to find a browser that we can use +find_browser <- function() { # nolint: cyclocomp_linter + if (is_mac()) { + path <- "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" + + if (file.exists(path)) { + return("chrome") + } else { + find_browser_linux() + } + } else if (is_windows()) { + find_browser_windows() + } else if (is_linux()) { + find_browser_linux() + } +} + +find_browser_linux <- function() { + possible_names <- c( + "google-chrome", + "google-chrome-stable", + "chromium-browser", + "chromium", + "google-chrome-beta", + "google-chrome-unstable", + "chrome" + ) + + for (path in possible_names) { + path <- Sys.which(path) + if (nzchar(path)) { + return("chrome") + } + } + + path <- Sys.which("firefox") + + if (nzchar(path)) { + "firefox" + } else { + NULL + } +} + +find_browser_windows <- function() { + path <- tryCatch( + { + path <- utils::readRegistry( + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe\\" + ) + path[["(Default)"]] + }, + error = function(e) { + NULL + } + ) + + if (!is.null(path)) { + return("chrome") + } + + path <- tryCatch( + { + path <- utils::readRegistry( + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe\\" + ) + path[["(Default)"]] + }, + error = function(e) { + NULL + } + ) + + if (!is.null(path)) { + return("firefox") + } + + batch_file <- + "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\iexplore.exe\\" + + path <- tryCatch( + { + path <- utils::readRegistry( + batch_file + ) + path[["(Default)"]] + }, + error = function(e) { + NULL + } + ) + + if (!is.null(path)) { + return("internet explorer") + } else { + NULL + } +} diff --git a/R/find_browser_and_version.R b/R/find_browser_and_version.R deleted file mode 100644 index 37e80fa0..00000000 --- a/R/find_browser_and_version.R +++ /dev/null @@ -1,201 +0,0 @@ -# No idea if this works -find_browser_and_version <- function() { # nolint: cyclocomp_linter - browser <- NULL - version <- NULL - - if (is_mac()) { - path <- "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" - - if (file.exists(path)) { - version <- tryCatch( - processx::run(path, "--product-version", timeout = 10)$stdout, - error = function(e) NULL - ) - - # The version of chromedriver depends on the chrome version - return(list( - browser = "chrome", - version = selenium_version(version, "chrome") - )) - } - - path <- Sys.which("firefox") - - if (nchar(path) != 0) { - return(list( - browser = "firefox", - version = "latest" - )) - } - } else if (is_windows()) { - path <- tryCatch({ - path <- utils::readRegistry( - "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe\\" - ) - path[["(Default)"]] - }, error = function(e) { - NULL - }) - - if (!is.null(path)) { - batch_file <- system.file( - "scripts/get_chrome_version.bat", - package = "selenider" - ) - version <- tryCatch(shell.exec(batch_file), error = function(e) NULL) - - return(list( - browser = "chrome", - version = selenium_version(version, "chrome") - )) - } - - path <- tryCatch({ - path <- utils::readRegistry( - "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\firefox.exe\\" - ) - path[["(Default)"]] - }, error = function(e) { - NULL - }) - - if (!is.null(path)) { - return(list( - browser = "firefox", - version = "latest" - )) - } - - batch_file <- - "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\iexplore.exe\\" - - path <- tryCatch({ - path <- utils::readRegistry( - batch_file - ) - path[["(Default)"]] - }, error = function(e) { - NULL - }) - - if (!is.null(path)) { - return(list( - browser = "internet explorer", - version = "latest" - )) - } - } else if (is_linux()) { - possible_names <- c( - "google-chrome", - "google-chrome-stable", - "chromium-browser", - "chromium", - "google-chrome-beta", - "google-chrome-unstable" - ) - - for (path in possible_names) { - path <- Sys.which(path) - if (nzchar(path)) { - version <- tryCatch( - processx::run(path, "--product-version")$stdout, - error = function(e) NULL - ) - - return(list( - browser = "chrome", - version = selenium_version(version, "chrome") - )) - } - } - - path <- Sys.which("firefox") - - if (nzchar(path)) { - return(list( - browser = "firefox", - version = "latest" - )) - } - } - - NULL -} - -get_browser_version <- function(x) { - if (x != "chrome") { - return("latest") - } - - if (is_mac()) { - path <- "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome" - - if (file.exists(path)) { - version <- tryCatch( - processx::run(path, "--product-version", timeout = 10)$stdout, - error = function(e) NULL - ) - - return(selenium_version(version, "chrome")) - } - } else if (is_windows()) { - path <- tryCatch({ - path <- utils::readRegistry( - "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe\\" - ) - path[["(Default)"]] - }, error = function(e) { - NULL - }) - - if (!is.null(path)) { - batch_file <- system.file( - "scripts/get_chrome_version.bat", - package = "selenider" - ) - version <- tryCatch( - shell.exec(batch_file), - error = function(e) NULL - ) - - return(selenium_version(version, "chrome")) - } - } else if (is_linux()) { - possible_names <- c( - "google-chrome", - "google-chrome-stable", - "chromium-browser", - "chromium", - "google-chrome-beta", - "google-chrome-unstable" - ) - - for (path in possible_names) { - path <- Sys.which(path) - if (nzchar(path)) { - version <- tryCatch( - processx::run(path, "--product-version")$stdout, - error = function(e) NULL - ) - return(selenium_version(version, "chrome")) - } - } - } - - "latest" -} - -selenium_version <- function(x, browser) { - if (is.null(x)) { - return("latest") - } - - # Very simple version matching for now: will only match numeric versions - version <- regmatches(x, regexpr("[0-9]+(?:\\.[0-9]+)+", x))[[1]][1] - - if (length(version) == 0) { - return("latest") - } - - version -} diff --git a/R/session.R b/R/session.R index 99e3d43a..3f793794 100644 --- a/R/session.R +++ b/R/session.R @@ -260,20 +260,12 @@ selenider_session <- function(session = getOption("selenider.session"), options <- check_options(session, options) - browser_version <- browser_and_version( + browser <- get_browser( session, browser = browser, driver = driver ) - browser <- browser_version$browser - - if (inherits(options, "selenium_options") && - inherits(options$server_options, "wdman_server_options") && # nolint: indentation_linter - is.null(options$server_options$version)) { - options$server_options$version <- browser_version$version - } - driver <- get_driver( browser = browser, options = options, @@ -475,38 +467,29 @@ check_session_dependencies <- function(session, session } -browser_and_version <- function(session, - browser, - driver, - call = rlang::caller_env()) { - version <- NULL +get_browser <- function(session, + browser, + driver, + call = rlang::caller_env()) { if (session == "chromote" && is.null(driver)) { if (!is.null(browser) && browser != "chrome") { warn_browser_chromote(call = call) } - browser <- "chrome" + "chrome" } else if (is.null(browser)) { if (is.null(driver) || is_selenium_server(driver)) { - bv <- find_browser_and_version() + browser <- find_browser() - if (is.null(bv)) { + if (is.null(browser)) { stop_default_browser(call = call) } - browser <- bv$browser - version <- bv$version + browser } } else { - if (browser != "phantomjs") { - version <- get_browser_version(browser) - } + browser } - - list( - browser = browser, - version = version - ) } #' Deprecated functions @@ -904,13 +887,11 @@ check_supplied_driver_list <- function(x, browser, options, call = rlang::caller stop_invalid_driver(x, is_list = TRUE, call = call) } else if (is.null(client)) { if (is.null(browser)) { - bv <- find_browser_and_version() + browser <- find_browser() if (is.null(bv)) { stop_default_browser(call = call) } - - browser <- bv$browser } client_options <- get_client_options(options, server, call = call) diff --git a/R/utils.R b/R/utils.R index 5c79df98..3c44e130 100644 --- a/R/utils.R +++ b/R/utils.R @@ -129,12 +129,12 @@ selenider_available_chromote <- function() { selenider_available_selenium <- function() { rlang::is_installed("selenium") && - !is.null(find_browser_and_version()$browser) + !is.null(find_browser()) } selenider_available_rselenium <- function() { rlang::is_installed("RSelenium") && - !is.null(find_browser_and_version()$browser) + !is.null(find_browser()) } #' @rdname selenider_available diff --git a/inst/scripts/get_chrome_version.bat b/inst/scripts/get_chrome_version.bat deleted file mode 100644 index 8ef3da8e..00000000 --- a/inst/scripts/get_chrome_version.bat +++ /dev/null @@ -1,27 +0,0 @@ -@ECHO OFF - -:: Look for machine-wide Chrome installs (stable, Beta, and Dev). -:: Get the name, running version (if an update is pending relaunch), and -:: installed version of each. -FOR %%A IN ( - {8A69D345-D564-463c-AFF1-A69D9E530F96}, - {8237E44A-0054-442C-B6B6-EA0509993955}, - {401C381F-E0DE-4B85-8BD8-3F3F14FBDA57}) DO ( - reg query HKLM\Software\Google\Update\Clients\%%A /v name /reg:32 2> NUL - reg query HKLM\Software\Google\Update\Clients\%%A /v opv /reg:32 2> NUL - reg query HKLM\Software\Google\Update\Clients\%%A /v pv /reg:32 2> NUL -) - -:: Look for Chrome installs in the current user's %LOCALAPPDATA% directory -:: (stable, Beta, Dev, and canary). -:: Get the name, running version (if an update is pending relaunch), and -:: installed version of each. -FOR %%A IN ( - {8A69D345-D564-463c-AFF1-A69D9E530F96}, - {8237E44A-0054-442C-B6B6-EA0509993955}, - {401C381F-E0DE-4B85-8BD8-3F3F14FBDA57}, - {4ea16ac7-fd5a-47c3-875b-dbf4a2008c20}) DO ( - reg query HKCU\Software\Google\Update\Clients\%%A /v name /reg:32 2> NUL - reg query HKCU\Software\Google\Update\Clients\%%A /v opv /reg:32 2> NUL - reg query HKCU\Software\Google\Update\Clients\%%A /v pv /reg:32 2> NUL -) diff --git a/inst/scripts/get_ie_version.bat b/inst/scripts/get_ie_version.bat deleted file mode 100644 index 706373fb..00000000 --- a/inst/scripts/get_ie_version.bat +++ /dev/null @@ -1,4 +0,0 @@ -@ECHO OFF - -reg query "HKLM\Software\Microsoft\Internet Explorer" /v svcVersion /reg:32 2> NUL -reg query "HKLM\Software\Microsoft\Internet Explorer" /v version /reg:32 2> NUL diff --git a/tests/testthat/test-find_browser.R b/tests/testthat/test-find_browser.R new file mode 100644 index 00000000..5a9871de --- /dev/null +++ b/tests/testthat/test-find_browser.R @@ -0,0 +1,3 @@ +test_that("find_browser() does not error", { + expect_no_error(find_browser()) +}) diff --git a/tests/testthat/test-find_browser_and_version.R b/tests/testthat/test-find_browser_and_version.R deleted file mode 100644 index 7bfa35c3..00000000 --- a/tests/testthat/test-find_browser_and_version.R +++ /dev/null @@ -1,3 +0,0 @@ -test_that("find_browser_and_version() works", { - expect_no_error(find_browser_and_version()) -})