Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wdman support #21

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Authors@R: c( person("John", "Harrison", , "[email protected]",
Depends:
R (>= 3.0.0)
Imports:
httr,jsonlite,xml2,magrittr,whisker
httr,jsonlite,xml2,magrittr,whisker,wdman
Encoding: UTF-8
License: GPL-3
Suggests:
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(print,rDriver)
S3method(print,spClientServer)
S3method(print,wElement)
export("%>%")
export(acceptAlert)
Expand Down Expand Up @@ -71,6 +72,7 @@ export(setWindowPosition)
export(setWindowPositionOld)
export(setWindowSize)
export(setWindowSizeOld)
export(spDriver)
export(status)
export(switchToFrame)
export(switchToParentFrame)
Expand All @@ -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)
2 changes: 1 addition & 1 deletion R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
105 changes: 105 additions & 0 deletions R/spDriver.R
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"]], ...)
}

7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
83 changes: 83 additions & 0 deletions man/spDriver.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.