diff --git a/DESCRIPTION b/DESCRIPTION index 2daaf3d..0c5c432 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,7 +12,7 @@ Authors@R: c( person("John", "Harrison", , "johndharrison0@gmail.com", Depends: R (>= 3.0.0) Imports: - httr,jsonlite,xml2,magrittr,whisker + httr,jsonlite,xml2,magrittr,whisker,wdman Encoding: UTF-8 License: GPL-3 Suggests: diff --git a/NAMESPACE b/NAMESPACE index 9e4e08d..03b38b5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand S3method(print,rDriver) +S3method(print,spClientServer) S3method(print,wElement) export("%>%") export(acceptAlert) @@ -71,6 +72,7 @@ export(setWindowPosition) export(setWindowPositionOld) export(setWindowSize) export(setWindowSizeOld) +export(spDriver) export(status) export(switchToFrame) export(switchToParentFrame) @@ -89,5 +91,6 @@ importFrom(jsonlite,base64_dec) importFrom(jsonlite,fromJSON) importFrom(jsonlite,toJSON) importFrom(magrittr,"%>%") +importFrom(wdman,selenium) importFrom(whisker,whisker.render) importFrom(xml2,read_html) diff --git a/R/init.R b/R/init.R index 5076eaa..64db79d 100644 --- a/R/init.R +++ b/R/init.R @@ -180,7 +180,7 @@ wbElement <- function(elementId, remDr){ #' queryDriver <- function(verb = GET, url, source, drvID, ...){ - if(!identical(source, "newSession")){ + if(!(source %in% c("newSession", "status"))){ if(is.null(.e$sessionId[[drvID]])){ message("Driver id is not registered. Has the session been deleted?") message("Alternatively no session exists: diff --git a/R/spDriver.R b/R/spDriver.R new file mode 100644 index 0000000..b265e72 --- /dev/null +++ b/R/spDriver.R @@ -0,0 +1,105 @@ +#' Start a selenium server and browser +#' +#' @param port Port to run on +#' @param browser Which browser to start +#' @param version what version of Selenium Server to run. Default = "latest" +#' which runs the most recent version. To see other version currently +#' sourced run binman::list_versions("seleniumserver") +#' @param chromever what version of Chrome driver to run. Default = "latest" +#' which runs the most recent version. To see other version currently +#' sourced run binman::list_versions("chromedriver"), A value of NULL +#' excludes adding the chrome browser to Selenium Server. +#' @param geckover what version of Gecko driver to run. Default = "latest" +#' which runs the most recent version. To see other version currently +#' sourced run binman::list_versions("geckodriver"), A value of NULL +#' excludes adding the firefox browser to Selenium Server. +#' @param phantomver what version of PhantomJS to run. Default = "2.1.1" +#' which runs the most recent stable version. To see other version currently +#' sourced run binman::list_versions("phantomjs"), A value of NULL +#' excludes adding the PhantomJS headless browser to Selenium Server. +#' @param iedrver what version of IEDriverServer to run. Default = "latest" +#' which runs the most recent version. To see other version currently +#' sourced run binman::list_versions("iedriverserver"), A value of NULL +#' excludes adding the internet explorer browser to Selenium Server. +#' NOTE this functionality is Windows OS only. +#' @param verbose If TRUE, include status messages (if any) +#' @param check If TRUE check the versions of selenium available and the +#' versions of associated drivers (chromever, geckover, phantomver, +#' iedrver). If new versions are available they will be downloaded. +#' @param ... Additional arguments to pass to \code{\link{remoteDr}} +#' +#' @return A list containing a server and a client. The server is the object +#' returned by \code{\link[wdman]{selenium}} and the client is an object of class +#' \code{rDriver}. +#' @details This function is a wrapper around \code{\link[wdman]{selenium}}. +#' It provides a "shim" for the current issue running firefox on +#' Windows. For a more detailed set of functions for running binaries +#' relating to the Selenium/webdriver project see the +#' \code{\link[wdman]{wdman}} package. Both the client and server +#' are closed using a registered finalizer. +#' @export +#' @importFrom wdman selenium +#' +#' @examples +#' \dontrun{ +#' # start a chrome browser +#' sp <- spDriver() +#' remDr <- sp[["client"]] +#' remDr %>% go("http://www.google.com/ncr") +#' remDr %>% go("http://www.bbc.com") +#' remDr %>% deleteSession() +#' # stop the selenium server +#' sp[["server"]]$stop() +#' +#' # if user forgets to stop server it will be garbage collected. +#' sp <- spDriver() +#' rm(sp) +#' gc(sp) +#' } + +spDriver <- function(port = 4567L, + browser = c("chrome", "firefox", "phantomjs", + "internet explorer"), + version = "latest", + chromever = "latest", + geckover = "latest", + iedrver = NULL, + phantomver = "2.1.1", + verbose = TRUE, + check = TRUE, ...){ + browser <- match.arg(browser) + if(identical(browser, "internet explorer") && + !identical(.Platform[["OS.type"]], "windows")){ + stop("Internet Explorer is only available on Windows.") + } + selServ <- wdman::selenium(port = port, verbose = verbose, + version = version, + chromever = chromever, + geckover = geckover, + iedrver = iedrver, + phantomver = phantomver, + check = TRUE) + remDr <- remoteDr(browserName = browser, port = port, newSession = FALSE, + ...) + # check server status + res <- suppressMessages({ + tryCatch({remDr %>% status(list(noTry = 4, delay = 1000))}, + error = function(e){e}) + }) + if(inherits(res, "error")){ + warning("Cannot determine Selenium Server status") + } + remDr %>% newSession() + csEnv <- new.env() + csEnv[["server"]] <- selServ + csEnv[["client"]] <- remDr + clean <- function(e){ + chk <- suppressMessages( + tryCatch({e[["client"]]$close()}, error = function(e)e) + ) + e[["server"]]$stop() + } + reg.finalizer(csEnv, clean) + class(csEnv) <- c("spClientServer", class(csEnv)) + return(csEnv) +} diff --git a/R/utils.R b/R/utils.R index b714a2e..8bd2492 100644 --- a/R/utils.R +++ b/R/utils.R @@ -90,3 +90,12 @@ testWebElement <- function(x, remDr){ #' @importFrom magrittr %>% #' @usage lhs \%>\% rhs NULL + +#' @export +print.spClientServer <- function(x, ...){ + cat("$client\n") + print(x[["client"]], ...) + cat("\n$server\n") + print(x[["server"]][["process"]], ...) +} + diff --git a/README.md b/README.md index 53ac26f..ad00f85 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,8 @@ Get started using `seleniumPipes` you can look at the following example ``` library(seleniumPipes) -library(RSelenium) # start a server with utility function -selServ <- RSelenium::startServer() -remDr <- remoteDr() +sp <- spDriver() +remDr <- sp[["client"]] remDr %>% go(url = "http://www.google.com") remDr %>% go(url = "http://www.bbc.com") remDr %>% back() @@ -65,7 +64,7 @@ remDr %>% getPageSource() %>% xml_find_all("//frame") %>% xml_attr("name") # [1] "logo" "contents" "banner" remDr %>% deleteSession() -selServ$stop() +sp[["server]]$stop() ``` ### Piping diff --git a/man/spDriver.Rd b/man/spDriver.Rd new file mode 100644 index 0000000..0cbd108 --- /dev/null +++ b/man/spDriver.Rd @@ -0,0 +1,83 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/spDriver.R +\name{spDriver} +\alias{spDriver} +\title{Start a selenium server and browser} +\usage{ +spDriver(port = 4567L, browser = c("chrome", "firefox", "phantomjs", + "internet explorer"), version = "latest", chromever = "latest", + geckover = "latest", iedrver = NULL, phantomver = "2.1.1", + verbose = TRUE, check = TRUE, ...) +} +\arguments{ +\item{port}{Port to run on} + +\item{browser}{Which browser to start} + +\item{version}{what version of Selenium Server to run. Default = "latest" +which runs the most recent version. To see other version currently +sourced run binman::list_versions("seleniumserver")} + +\item{chromever}{what version of Chrome driver to run. Default = "latest" +which runs the most recent version. To see other version currently +sourced run binman::list_versions("chromedriver"), A value of NULL +excludes adding the chrome browser to Selenium Server.} + +\item{geckover}{what version of Gecko driver to run. Default = "latest" +which runs the most recent version. To see other version currently +sourced run binman::list_versions("geckodriver"), A value of NULL +excludes adding the firefox browser to Selenium Server.} + +\item{iedrver}{what version of IEDriverServer to run. Default = "latest" +which runs the most recent version. To see other version currently +sourced run binman::list_versions("iedriverserver"), A value of NULL +excludes adding the internet explorer browser to Selenium Server. +NOTE this functionality is Windows OS only.} + +\item{phantomver}{what version of PhantomJS to run. Default = "2.1.1" +which runs the most recent stable version. To see other version currently +sourced run binman::list_versions("phantomjs"), A value of NULL +excludes adding the PhantomJS headless browser to Selenium Server.} + +\item{verbose}{If TRUE, include status messages (if any)} + +\item{check}{If TRUE check the versions of selenium available and the +versions of associated drivers (chromever, geckover, phantomver, +iedrver). If new versions are available they will be downloaded.} + +\item{...}{Additional arguments to pass to \code{\link{remoteDr}}} +} +\value{ +A list containing a server and a client. The server is the object +returned by \code{\link[wdman]{selenium}} and the client is an object of class +\code{rDriver}. +} +\description{ +Start a selenium server and browser +} +\details{ +This function is a wrapper around \code{\link[wdman]{selenium}}. + It provides a "shim" for the current issue running firefox on + Windows. For a more detailed set of functions for running binaries + relating to the Selenium/webdriver project see the + \code{\link[wdman]{wdman}} package. Both the client and server + are closed using a registered finalizer. +} +\examples{ +\dontrun{ +# start a chrome browser +sp <- spDriver() +remDr <- sp[["client"]] +remDr \%>\% go("http://www.google.com/ncr") +remDr \%>\% go("http://www.bbc.com") +remDr \%>\% deleteSession() +# stop the selenium server +sp[["server"]]$stop() + +# if user forgets to stop server it will be garbage collected. +sp <- spDriver() +rm(sp) +gc(sp) +} +} +