Class RemoteWebDriver
- java.lang.Object
-
- org.openqa.selenium.remote.RemoteWebDriver
-
- All Implemented Interfaces:
HasCapabilities
,Interactive
,JavascriptExecutor
,PrintsPage
,SearchContext
,TakesScreenshot
,HasVirtualAuthenticator
,WebDriver
- Direct Known Subclasses:
ChromiumDriver
,FirefoxDriver
,InternetExplorerDriver
,SafariDriver
public class RemoteWebDriver extends java.lang.Object implements WebDriver, JavascriptExecutor, HasCapabilities, HasVirtualAuthenticator, Interactive, PrintsPage, TakesScreenshot
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
RemoteWebDriver.RemoteTargetLocator
protected class
RemoteWebDriver.RemoteWebDriverOptions
static class
RemoteWebDriver.When
-
Nested classes/interfaces inherited from interface org.openqa.selenium.WebDriver
WebDriver.Navigation, WebDriver.Options, WebDriver.TargetLocator, WebDriver.Timeouts, WebDriver.Window
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
RemoteWebDriver()
RemoteWebDriver(java.net.URL remoteAddress, Capabilities capabilities)
RemoteWebDriver(java.net.URL remoteAddress, Capabilities capabilities, boolean enableTracing)
RemoteWebDriver(Capabilities capabilities)
RemoteWebDriver(Capabilities capabilities, boolean enableTracing)
RemoteWebDriver(CommandExecutor executor, Capabilities capabilities)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description VirtualAuthenticator
addVirtualAuthenticator(VirtualAuthenticatorOptions options)
Adds a virtual authenticator with the given options.static RemoteWebDriverBuilder
builder()
void
close()
Close the current window, quitting the browser if it's the last window currently open.protected Response
execute(java.lang.String command)
protected Response
execute(java.lang.String driverCommand, java.util.Map<java.lang.String,?> parameters)
protected Response
execute(CommandPayload payload)
java.lang.Object
executeAsyncScript(java.lang.String script, java.lang.Object... args)
Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window.java.lang.Object
executeScript(java.lang.String script, java.lang.Object... args)
Executes JavaScript in the context of the currently selected frame or window.protected WebElement
findElement(java.lang.String by, java.lang.String using)
Deprecated.Rely on usingBy.Remotable
insteadWebElement
findElement(By locator)
Find the firstWebElement
using the given method.protected java.util.List<WebElement>
findElements(java.lang.String by, java.lang.String using)
Deprecated.Rely on usingBy.Remotable
insteadjava.util.List<WebElement>
findElements(By locator)
Find all elements within the current page using the given mechanism.java.util.List<WebElement>
findElements(SearchContext context, java.util.function.BiFunction<java.lang.String,java.lang.Object,CommandPayload> findCommand, By locator)
void
get(java.lang.String url)
Load a new web page in the current browser window.Capabilities
getCapabilities()
CommandExecutor
getCommandExecutor()
java.lang.String
getCurrentUrl()
Get a string representing the current URL that the browser is looking at.protected JsonToWebElementConverter
getElementConverter()
ErrorHandler
getErrorHandler()
protected ExecuteMethod
getExecuteMethod()
FileDetector
getFileDetector()
java.lang.String
getPageSource()
Get the source of the last loaded page.<X> X
getScreenshotAs(OutputType<X> outputType)
Capture the screenshot and store it in the specified location.SessionId
getSessionId()
java.lang.String
getTitle()
Get the title of the current page.java.lang.String
getWindowHandle()
Return an opaque handle to this window that uniquely identifies it within this driver instance.java.util.Set<java.lang.String>
getWindowHandles()
Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them toWebDriver.switchTo()
.WebDriver.Options.window()
protected void
log(SessionId sessionId, java.lang.String commandName, java.lang.Object toLog, RemoteWebDriver.When when)
Override this to be notified at key points in the execution of a command.WebDriver.Options
manage()
Gets the Option interfaceWebDriver.Navigation
navigate()
An abstraction allowing the driver to access the browser's history and to navigate to a given URL.void
perform(java.util.Collection<Sequence> actions)
Pdf
print(PrintOptions printOptions)
void
quit()
Quits this driver, closing every associated window.void
removeVirtualAuthenticator(VirtualAuthenticator authenticator)
Removes a previously added virtual authenticator.void
resetInputState()
protected void
setCommandExecutor(CommandExecutor executor)
protected void
setElementConverter(JsonToWebElementConverter converter)
void
setErrorHandler(ErrorHandler handler)
void
setFileDetector(FileDetector detector)
Set the file detector to be used when sending keyboard input.protected void
setFoundBy(SearchContext context, WebElement element, java.lang.String by, java.lang.String using)
void
setLogLevel(java.util.logging.Level level)
Sets the RemoteWebDriver's client log level.protected void
setSessionId(java.lang.String opaqueKey)
protected void
startSession(Capabilities capabilities)
WebDriver.TargetLocator
switchTo()
Send future commands to a different frame or window.java.lang.String
toString()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.openqa.selenium.JavascriptExecutor
executeScript, getPinnedScripts, pin, unpin
-
-
-
-
Constructor Detail
-
RemoteWebDriver
protected RemoteWebDriver()
-
RemoteWebDriver
public RemoteWebDriver(Capabilities capabilities)
-
RemoteWebDriver
public RemoteWebDriver(Capabilities capabilities, boolean enableTracing)
-
RemoteWebDriver
public RemoteWebDriver(java.net.URL remoteAddress, Capabilities capabilities)
-
RemoteWebDriver
public RemoteWebDriver(java.net.URL remoteAddress, Capabilities capabilities, boolean enableTracing)
-
RemoteWebDriver
public RemoteWebDriver(CommandExecutor executor, Capabilities capabilities)
-
-
Method Detail
-
builder
@Beta public static RemoteWebDriverBuilder builder()
-
getSessionId
public SessionId getSessionId()
-
setSessionId
protected void setSessionId(java.lang.String opaqueKey)
-
startSession
protected void startSession(Capabilities capabilities)
-
getErrorHandler
public ErrorHandler getErrorHandler()
-
setErrorHandler
public void setErrorHandler(ErrorHandler handler)
-
getCommandExecutor
public CommandExecutor getCommandExecutor()
-
setCommandExecutor
protected void setCommandExecutor(CommandExecutor executor)
-
getCapabilities
public Capabilities getCapabilities()
- Specified by:
getCapabilities
in interfaceHasCapabilities
- Returns:
- The capabilities of the current driver.
-
get
public void get(java.lang.String url)
Description copied from interface:WebDriver
Load a new web page in the current browser window. This is done using an HTTP POST operation, and the method will block until the load is complete (with the default 'page load strategy'. This will follow redirects issued either by the server or as a meta-redirect from within the returned HTML. Should a meta-redirect "rest" for any duration of time, it is best to wait until this timeout is over, since should the underlying page change whilst your test is executing the results of future calls against this interface will be against the freshly loaded page. Synonym forWebDriver.Navigation.to(String)
.See W3C WebDriver specification for more details.
- Specified by:
get
in interfaceWebDriver
- Parameters:
url
- The URL to load. Must be a fully qualified URL- See Also:
PageLoadStrategy
-
getTitle
public java.lang.String getTitle()
Description copied from interface:WebDriver
Get the title of the current page.See W3C WebDriver specification for more details.
-
getCurrentUrl
public java.lang.String getCurrentUrl()
Description copied from interface:WebDriver
Get a string representing the current URL that the browser is looking at.See W3C WebDriver specification for more details.
- Specified by:
getCurrentUrl
in interfaceWebDriver
- Returns:
- The URL of the page currently loaded in the browser
-
getScreenshotAs
public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException
Description copied from interface:TakesScreenshot
Capture the screenshot and store it in the specified location.For a W3C-conformant WebDriver or WebElement, this behaves as stated in W3C WebDriver specification.
For a non-W3C-conformant WebDriver, this makes a best effort depending on the browser to return the following in order of preference:
- Entire page
- Current window
- Visible portion of the current frame
- The screenshot of the entire display containing the browser
- The entire content of the HTML element
- The visible portion of the HTML element
- Specified by:
getScreenshotAs
in interfaceTakesScreenshot
- Type Parameters:
X
- Return type for getScreenshotAs.- Parameters:
outputType
- target type, @see OutputType- Returns:
- Object in which is stored information about the screenshot.
- Throws:
WebDriverException
- on failure.
-
print
public Pdf print(PrintOptions printOptions) throws WebDriverException
- Specified by:
print
in interfacePrintsPage
- Throws:
WebDriverException
-
findElement
public WebElement findElement(By locator)
Description copied from interface:WebDriver
Find the firstWebElement
using the given method. This method is affected by the 'implicit wait' times in force at the time of execution. The findElement(..) invocation will return a matching row, or try again repeatedly until the configured timeout is reached.findElement should not be used to look for non-present elements, use
WebDriver.findElements(By)
and assert zero length response instead.See W3C WebDriver specification for more details.
- Specified by:
findElement
in interfaceSearchContext
- Specified by:
findElement
in interfaceWebDriver
- Parameters:
locator
- The locating mechanism to use- Returns:
- The first matching element on the current page
- See Also:
By
,WebDriver.Timeouts
-
findElements
public java.util.List<WebElement> findElements(By locator)
Description copied from interface:WebDriver
Find all elements within the current page using the given mechanism. This method is affected by the 'implicit wait' times in force at the time of execution. When implicitly waiting, this method will return as soon as there are more than 0 items in the found collection, or will return an empty list if the timeout is reached.See W3C WebDriver specification for more details.
- Specified by:
findElements
in interfaceSearchContext
- Specified by:
findElements
in interfaceWebDriver
- Parameters:
locator
- The locating mechanism to use- Returns:
- A list of all matching
WebElement
s, or an empty list if nothing matches - See Also:
By
,WebDriver.Timeouts
-
findElements
public java.util.List<WebElement> findElements(SearchContext context, java.util.function.BiFunction<java.lang.String,java.lang.Object,CommandPayload> findCommand, By locator)
-
findElement
@Deprecated protected WebElement findElement(java.lang.String by, java.lang.String using)
Deprecated.Rely on usingBy.Remotable
instead
-
findElements
@Deprecated protected java.util.List<WebElement> findElements(java.lang.String by, java.lang.String using)
Deprecated.Rely on usingBy.Remotable
instead
-
setFoundBy
protected void setFoundBy(SearchContext context, WebElement element, java.lang.String by, java.lang.String using)
-
getPageSource
public java.lang.String getPageSource()
Description copied from interface:WebDriver
Get the source of the last loaded page. If the page has been modified after loading (for example, by Javascript) there is no guarantee that the returned text is that of the modified page. Please consult the documentation of the particular driver being used to determine whether the returned text reflects the current state of the page or the text last sent by the web server. The page source returned is a representation of the underlying DOM: do not expect it to be formatted or escaped in the same way as the response sent from the web server. Think of it as an artist's impression.See W3C WebDriver specification for more details.
- Specified by:
getPageSource
in interfaceWebDriver
- Returns:
- The source of the current page
-
close
public void close()
Description copied from interface:WebDriver
Close the current window, quitting the browser if it's the last window currently open.See W3C WebDriver specification for more details.
-
quit
public void quit()
Description copied from interface:WebDriver
Quits this driver, closing every associated window.
-
getWindowHandles
public java.util.Set<java.lang.String> getWindowHandles()
Description copied from interface:WebDriver
Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them toWebDriver.switchTo()
.WebDriver.Options.window()
See W3C WebDriver specification for more details.
- Specified by:
getWindowHandles
in interfaceWebDriver
- Returns:
- A set of window handles which can be used to iterate over all open windows.
-
getWindowHandle
public java.lang.String getWindowHandle()
Description copied from interface:WebDriver
Return an opaque handle to this window that uniquely identifies it within this driver instance. This can be used to switch to this window at a later dateSee W3C WebDriver specification for more details.
- Specified by:
getWindowHandle
in interfaceWebDriver
- Returns:
- the current window handle
-
executeScript
public java.lang.Object executeScript(java.lang.String script, java.lang.Object... args)
Description copied from interface:JavascriptExecutor
Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function.Within the script, use
document
to refer to the current document. Note that local variables will not be available once the script has finished executing, though global variables will persist.If the script has a return value (i.e. if the script contains a
return
statement), then the following steps will be taken:- For an HTML element, this method returns a WebElement
- For a decimal, a Double is returned
- For a non-decimal number, a Long is returned
- For a boolean, a Boolean is returned
- For all other cases, a String is returned.
- For an array, return a List<Object> with each object following the rules above. We support nested lists.
- For a map, return a Map<String, Object> with values following the rules above.
- Unless the value is null or there is no return value, in which null is returned
Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"
- Specified by:
executeScript
in interfaceJavascriptExecutor
- Parameters:
script
- The JavaScript to executeargs
- The arguments to the script. May be empty- Returns:
- One of Boolean, Long, Double, String, List, Map or WebElement. Or null.
-
executeAsyncScript
public java.lang.Object executeAsyncScript(java.lang.String script, java.lang.Object... args)
Description copied from interface:JavascriptExecutor
Execute an asynchronous piece of JavaScript in the context of the currently selected frame or window. Unlike executingsynchronous JavaScript
, scripts executed with this method must explicitly signal they are finished by invoking the provided callback. This callback is always injected into the executed function as the last argument.The first argument passed to the callback function will be used as the script's result. This value will be handled as follows:
- For an HTML element, this method returns a WebElement
- For a number, a Long is returned
- For a boolean, a Boolean is returned
- For all other cases, a String is returned.
- For an array, return a List<Object> with each object following the rules above. We support nested lists.
- For a map, return a Map<String, Object> with values following the rules above.
- Unless the value is null or there is no return value, in which null is returned
The default timeout for a script to be executed is 0ms. In most cases, including the examples below, one must set the script timeout
WebDriver.Timeouts.scriptTimeout(java.time.Duration)
beforehand to a value sufficiently large enough.Example #1: Performing a sleep in the browser under test.
long start = System.currentTimeMillis(); ((JavascriptExecutor) driver).executeAsyncScript( "window.setTimeout(arguments[arguments.length - 1], 500);"); System.out.println( "Elapsed time: " + (System.currentTimeMillis() - start));
Example #2: Synchronizing a test with an AJAX application:
WebElement composeButton = driver.findElement(By.id("compose-button")); composeButton.click(); ((JavascriptExecutor) driver).executeAsyncScript( "var callback = arguments[arguments.length - 1];" + "mailClient.getComposeWindowWidget().onload(callback);"); driver.switchTo().frame("composeWidget"); driver.findElement(By.id("to")).sendKeys("bog@example.com");
Example #3: Injecting a XMLHttpRequest and waiting for the result:
Object response = ((JavascriptExecutor) driver).executeAsyncScript( "var callback = arguments[arguments.length - 1];" + "var xhr = new XMLHttpRequest();" + "xhr.open('GET', '/resource/data.json', true);" + "xhr.onreadystatechange = function() {" + " if (xhr.readyState == 4) {" + " callback(xhr.responseText);" + " }" + "};" + "xhr.send();"); JsonObject json = new JsonParser().parse((String) response); assertEquals("cheese", json.get("food").getAsString());
Script arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" variable.
- Specified by:
executeAsyncScript
in interfaceJavascriptExecutor
- Parameters:
script
- The JavaScript to execute.args
- The arguments to the script. May be empty.- Returns:
- One of Boolean, Long, String, List, Map, WebElement, or null.
- See Also:
WebDriver.Timeouts.scriptTimeout(java.time.Duration)
-
switchTo
public WebDriver.TargetLocator switchTo()
Description copied from interface:WebDriver
Send future commands to a different frame or window.- Specified by:
switchTo
in interfaceWebDriver
- Returns:
- A TargetLocator which can be used to select a frame or window
- See Also:
WebDriver.TargetLocator
-
navigate
public WebDriver.Navigation navigate()
Description copied from interface:WebDriver
An abstraction allowing the driver to access the browser's history and to navigate to a given URL.- Specified by:
navigate
in interfaceWebDriver
- Returns:
- A
WebDriver.Navigation
that allows the selection of what to do next
-
manage
public WebDriver.Options manage()
Description copied from interface:WebDriver
Gets the Option interface- Specified by:
manage
in interfaceWebDriver
- Returns:
- An option interface
- See Also:
WebDriver.Options
-
getElementConverter
protected JsonToWebElementConverter getElementConverter()
-
setElementConverter
protected void setElementConverter(JsonToWebElementConverter converter)
-
setLogLevel
public void setLogLevel(java.util.logging.Level level)
Sets the RemoteWebDriver's client log level.- Parameters:
level
- The log level to use.
-
execute
protected Response execute(CommandPayload payload)
-
execute
protected Response execute(java.lang.String driverCommand, java.util.Map<java.lang.String,?> parameters)
-
execute
protected Response execute(java.lang.String command)
-
getExecuteMethod
protected ExecuteMethod getExecuteMethod()
-
perform
public void perform(java.util.Collection<Sequence> actions)
- Specified by:
perform
in interfaceInteractive
-
resetInputState
public void resetInputState()
- Specified by:
resetInputState
in interfaceInteractive
-
addVirtualAuthenticator
public VirtualAuthenticator addVirtualAuthenticator(VirtualAuthenticatorOptions options)
Description copied from interface:HasVirtualAuthenticator
Adds a virtual authenticator with the given options.- Specified by:
addVirtualAuthenticator
in interfaceHasVirtualAuthenticator
- Returns:
- the new virtual authenticator.
-
removeVirtualAuthenticator
public void removeVirtualAuthenticator(VirtualAuthenticator authenticator)
Description copied from interface:HasVirtualAuthenticator
Removes a previously added virtual authenticator. The authenticator is no longer valid after removal, so no methods may be called.- Specified by:
removeVirtualAuthenticator
in interfaceHasVirtualAuthenticator
-
log
protected void log(SessionId sessionId, java.lang.String commandName, java.lang.Object toLog, RemoteWebDriver.When when)
Override this to be notified at key points in the execution of a command.- Parameters:
sessionId
- the session id.commandName
- the command that is being executed.toLog
- any data that might be interesting.when
- verb tense of "Execute" to prefix message
-
getFileDetector
public FileDetector getFileDetector()
-
setFileDetector
public void setFileDetector(FileDetector detector)
Set the file detector to be used when sending keyboard input. By default, this is set to a file detector that does nothing.- Parameters:
detector
- The detector to use. Must not be null.- See Also:
FileDetector
,LocalFileDetector
,UselessFileDetector
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-