public class JBrowserDriver extends org.openqa.selenium.remote.RemoteWebDriver implements Killable
See https://github.com/machinepublishers/jbrowserdriver#usage for basic usage info.
Licensed under the Apache License version 2.0.
org.openqa.selenium.remote.RemoteWebDriver.RemoteTargetLocator, org.openqa.selenium.remote.RemoteWebDriver.RemoteWebDriverOptions, org.openqa.selenium.remote.RemoteWebDriver.When
WebDriver.ImeHandler, WebDriver.Navigation, WebDriver.Options, WebDriver.TargetLocator, WebDriver.Timeouts, WebDriver.Window
Constructor and Description |
---|
JBrowserDriver()
Constructs a browser with default settings, UTC timezone, and no proxy.
|
JBrowserDriver(Capabilities capabilities)
Use
Settings.builder() ...buildCapabilities() to create settings to pass to this constructor. |
JBrowserDriver(Settings settings)
Use
Settings.builder() ...build() to create settings to pass to this constructor. |
Modifier and Type | Method and Description |
---|---|
File |
attachmentsDir() |
File |
cacheDir() |
void |
close()
Close the current window, quitting the browser if it's the last window currently open.
|
Object |
executeAsyncScript(String script,
Object... args)
Execute an asynchronous piece of JavaScript in the context of the currently selected frame or
window.
|
Object |
executeScript(String script,
Object... args)
Executes JavaScript in the context of the currently selected frame or window.
|
protected void |
finalize() |
WebElement |
findElement(By by)
Find the first
WebElement using the given method. |
WebElement |
findElementByClassName(String cssClass) |
WebElement |
findElementByCssSelector(String expr) |
WebElement |
findElementById(String id) |
WebElement |
findElementByLinkText(String text) |
WebElement |
findElementByName(String name) |
WebElement |
findElementByPartialLinkText(String text) |
WebElement |
findElementByTagName(String tagName) |
WebElement |
findElementByXPath(String expr) |
List<WebElement> |
findElements(By by)
Find all elements within the current page using the given mechanism.
|
List<WebElement> |
findElementsByClassName(String cssClass) |
List<WebElement> |
findElementsByCssSelector(String expr) |
List<WebElement> |
findElementsById(String id) |
List<WebElement> |
findElementsByLinkText(String text) |
List<WebElement> |
findElementsByName(String name) |
List<WebElement> |
findElementsByPartialLinkText(String text) |
List<WebElement> |
findElementsByTagName(String tagName) |
List<WebElement> |
findElementsByXPath(String expr) |
void |
get(String url)
Load a new web page in the current browser window.
|
Capabilities |
getCapabilities() |
org.openqa.selenium.remote.CommandExecutor |
getCommandExecutor() |
String |
getCurrentUrl()
Get a string representing the current URL that the browser is looking at.
|
org.openqa.selenium.remote.ErrorHandler |
getErrorHandler() |
org.openqa.selenium.remote.FileDetector |
getFileDetector() |
Keyboard |
getKeyboard() |
Mouse |
getMouse() |
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.
|
org.openqa.selenium.remote.SessionId |
getSessionId() |
int |
getStatusCode() |
String |
getTitle()
The title of the current page.
|
String |
getWindowHandle()
Return an opaque handle to this window that uniquely identifies it within this driver instance.
|
Set<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 to
WebDriver.switchTo() .WebDriver.Options.window() |
void |
init()
Optionally call this method if you want JavaFX initialized and the browser
window opened immediately.
|
void |
kill()
Attempt to forcibly kill this Killable at the OS level.
|
WebDriver.Options |
manage()
Gets the Option interface
|
File |
mediaDir() |
WebDriver.Navigation |
navigate()
An abstraction allowing the driver to access the browser's history and to navigate to a given
URL.
|
void |
pageWait()
Waits until requests are completed and idle for a certain
amount of time.
|
void |
quit()
Quits this driver, closing every associated window.
|
void |
reset()
Reset the state of the browser.
|
void |
reset(Capabilities capabilities)
Reset the state of the browser.
|
void |
reset(Settings settings)
Reset the state of the browser.
|
WebDriver.TargetLocator |
switchTo()
Send future commands to a different frame or window.
|
static List<String> |
test()
Run diagnostic tests.
|
execute, execute, findElement, findElements, getElementConverter, getExecuteMethod, getW3CStandardComplianceLevel, log, setCommandExecutor, setElementConverter, setErrorHandler, setFileDetector, setFoundBy, setLogLevel, setSessionId, startClient, startSession, startSession, stopClient, toString
public JBrowserDriver()
public JBrowserDriver(Capabilities capabilities)
Settings.builder()
...buildCapabilities() to create settings to pass to this constructor.
This constructor is mostly useful for Selenium Server itself to use.capabilities
- public JBrowserDriver(Settings settings)
Settings.builder()
...build() to create settings to pass to this constructor.settings
- public File attachmentsDir()
public File cacheDir()
public void close()
public Object executeAsyncScript(String script, Object... args)
synchronous 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:
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.setScriptTimeout(long, java.util.concurrent.TimeUnit)
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.
executeAsyncScript
in interface JavascriptExecutor
executeAsyncScript
in class org.openqa.selenium.remote.RemoteWebDriver
script
- The JavaScript to execute.args
- The arguments to the script. May be empty.WebDriver.Timeouts.setScriptTimeout(long, java.util.concurrent.TimeUnit)
public Object executeScript(String script, Object... args)
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:
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"
executeScript
in interface JavascriptExecutor
executeScript
in class org.openqa.selenium.remote.RemoteWebDriver
script
- The JavaScript to executeargs
- The arguments to the script. May be emptyprotected void finalize() throws Throwable
public WebElement findElement(By by)
WebElement
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.findElement
in interface SearchContext
findElement
in interface WebDriver
findElement
in class org.openqa.selenium.remote.RemoteWebDriver
by
- The locating mechanismBy
,
WebDriver.Timeouts
public WebElement findElementByClassName(String cssClass)
findElementByClassName
in interface FindsByClassName
findElementByClassName
in class org.openqa.selenium.remote.RemoteWebDriver
public WebElement findElementByCssSelector(String expr)
findElementByCssSelector
in interface FindsByCssSelector
findElementByCssSelector
in class org.openqa.selenium.remote.RemoteWebDriver
public WebElement findElementById(String id)
findElementById
in interface FindsById
findElementById
in class org.openqa.selenium.remote.RemoteWebDriver
public WebElement findElementByLinkText(String text)
findElementByLinkText
in interface FindsByLinkText
findElementByLinkText
in class org.openqa.selenium.remote.RemoteWebDriver
public WebElement findElementByName(String name)
findElementByName
in interface FindsByName
findElementByName
in class org.openqa.selenium.remote.RemoteWebDriver
public WebElement findElementByPartialLinkText(String text)
findElementByPartialLinkText
in interface FindsByLinkText
findElementByPartialLinkText
in class org.openqa.selenium.remote.RemoteWebDriver
public WebElement findElementByTagName(String tagName)
findElementByTagName
in interface FindsByTagName
findElementByTagName
in class org.openqa.selenium.remote.RemoteWebDriver
public WebElement findElementByXPath(String expr)
findElementByXPath
in interface FindsByXPath
findElementByXPath
in class org.openqa.selenium.remote.RemoteWebDriver
public List<WebElement> findElements(By by)
findElements
in interface SearchContext
findElements
in interface WebDriver
findElements
in class org.openqa.selenium.remote.RemoteWebDriver
by
- The locating mechanism to useWebElement
s, or an empty list if nothing matchesBy
,
WebDriver.Timeouts
public List<WebElement> findElementsByClassName(String cssClass)
findElementsByClassName
in interface FindsByClassName
findElementsByClassName
in class org.openqa.selenium.remote.RemoteWebDriver
public List<WebElement> findElementsByCssSelector(String expr)
findElementsByCssSelector
in interface FindsByCssSelector
findElementsByCssSelector
in class org.openqa.selenium.remote.RemoteWebDriver
public List<WebElement> findElementsById(String id)
findElementsById
in interface FindsById
findElementsById
in class org.openqa.selenium.remote.RemoteWebDriver
public List<WebElement> findElementsByLinkText(String text)
findElementsByLinkText
in interface FindsByLinkText
findElementsByLinkText
in class org.openqa.selenium.remote.RemoteWebDriver
public List<WebElement> findElementsByName(String name)
findElementsByName
in interface FindsByName
findElementsByName
in class org.openqa.selenium.remote.RemoteWebDriver
public List<WebElement> findElementsByPartialLinkText(String text)
findElementsByPartialLinkText
in interface FindsByLinkText
findElementsByPartialLinkText
in class org.openqa.selenium.remote.RemoteWebDriver
public List<WebElement> findElementsByTagName(String tagName)
findElementsByTagName
in interface FindsByTagName
findElementsByTagName
in class org.openqa.selenium.remote.RemoteWebDriver
public List<WebElement> findElementsByXPath(String expr)
findElementsByXPath
in interface FindsByXPath
findElementsByXPath
in class org.openqa.selenium.remote.RemoteWebDriver
public void get(String url)
WebDriver.Navigation.to(String)
.public Capabilities getCapabilities()
getCapabilities
in interface HasCapabilities
getCapabilities
in class org.openqa.selenium.remote.RemoteWebDriver
public org.openqa.selenium.remote.CommandExecutor getCommandExecutor()
getCommandExecutor
in class org.openqa.selenium.remote.RemoteWebDriver
public String getCurrentUrl()
getCurrentUrl
in interface WebDriver
getCurrentUrl
in class org.openqa.selenium.remote.RemoteWebDriver
public org.openqa.selenium.remote.ErrorHandler getErrorHandler()
getErrorHandler
in class org.openqa.selenium.remote.RemoteWebDriver
public org.openqa.selenium.remote.FileDetector getFileDetector()
getFileDetector
in class org.openqa.selenium.remote.RemoteWebDriver
public Keyboard getKeyboard()
getKeyboard
in interface HasInputDevices
getKeyboard
in class org.openqa.selenium.remote.RemoteWebDriver
public Mouse getMouse()
getMouse
in interface HasInputDevices
getMouse
in class org.openqa.selenium.remote.RemoteWebDriver
public String getPageSource()
getPageSource
in interface WebDriver
getPageSource
in class org.openqa.selenium.remote.RemoteWebDriver
public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException
For WebDriver extending TakesScreenshot, this makes a best effort depending on the browser to return the following in order of preference:
For WebElement extending TakesScreenshot, this makes a best effort depending on the browser to return the following in order of preference: - The entire content of the HTML element - The visisble portion of the HTML element
getScreenshotAs
in interface TakesScreenshot
getScreenshotAs
in class org.openqa.selenium.remote.RemoteWebDriver
X
- Return type for getScreenshotAs.outputType
- target type, @see OutputTypeWebDriverException
- on failure.public org.openqa.selenium.remote.SessionId getSessionId()
getSessionId
in class org.openqa.selenium.remote.RemoteWebDriver
public int getStatusCode()
public String getTitle()
public String getWindowHandle()
getWindowHandle
in interface WebDriver
getWindowHandle
in class org.openqa.selenium.remote.RemoteWebDriver
public Set<String> getWindowHandles()
WebDriver.switchTo()
.WebDriver.Options.window()
getWindowHandles
in interface WebDriver
getWindowHandles
in class org.openqa.selenium.remote.RemoteWebDriver
public void init()
public void kill()
public WebDriver.Options manage()
manage
in interface WebDriver
manage
in class org.openqa.selenium.remote.RemoteWebDriver
WebDriver.Options
public File mediaDir()
public WebDriver.Navigation navigate()
navigate
in interface WebDriver
navigate
in class org.openqa.selenium.remote.RemoteWebDriver
WebDriver.Navigation
that allows the selection of what to
do nextpublic void pageWait()
The behavior of this wait algorithm can be configured by
Settings.Builder.ajaxWait(long)
and
Settings.Builder.ajaxResourceTimeout(long)
.
public void quit()
public void reset()
public void reset(Capabilities capabilities)
Note: it's not possible to switch between headless and GUI mode. You must quit this browser and create a new instance.
capabilities
- Capabilities to take effect, superseding the original onespublic void reset(Settings settings)
Note: it's not possible to switch between headless and GUI mode. You must quit this browser and create a new instance.
settings
- New settings to take effect, superseding the original onespublic WebDriver.TargetLocator switchTo()
switchTo
in interface WebDriver
switchTo
in class org.openqa.selenium.remote.RemoteWebDriver
WebDriver.TargetLocator
Copyright © 2014-2017 Machine Publishers, LLC and the jBrowserDriver contributors.
Project homepage: https://github.com/machinepublishers/jbrowserdriver