public abstract class BrowserDriver extends Object implements BrowserDriverManager
Example of a simple test of checking google search using the default BrowserDriver and JUnit:
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import edu.uci.qa.browserdriver.BrowserDriver;
public class SimpleTest {
BrowserDriver driver = null;
@Before
public void createBrowser() {
driver = BrowserDriver.createBrowser();
}
@After
public void quitBrowser() {
driver.quit();
}
@Test
public void basicTest() throws Throwable {
driver.get("http://www.google.com");
driver.sendKeys(By.xpath("//*[ @name='q']"), "webdriver").submit();
driver.waitUntil(
ExpectedConditions.presenceOfElementLocated(
By.xpath("//*[ @id='rso' or @id='resultStats']")));
Assert.assertEquals("webdriver - Google Search", driver.getTitle());
}
}
org.openqa.selenium.WebDriver.ImeHandler, org.openqa.selenium.WebDriver.Navigation, org.openqa.selenium.WebDriver.Options, org.openqa.selenium.WebDriver.TargetLocator, org.openqa.selenium.WebDriver.Timeouts, org.openqa.selenium.WebDriver.Window
Modifier | Constructor and Description |
---|---|
protected |
BrowserDriver(Browser browser,
org.openqa.selenium.Capabilities capabilities) |
Modifier and Type | Method and Description |
---|---|
org.openqa.selenium.interactions.Actions |
actionBuilder()
This method grants access to a global action builder.
|
org.openqa.selenium.WebElement |
clear(org.openqa.selenium.By locator)
Waits for the visibility of the specified
locator for
ElementManager.defaultTimeoutInSeconds() at most, then attempts to clear the text entry value if this
element is a text entry element. |
org.openqa.selenium.WebElement |
clear(org.openqa.selenium.By locator,
long timeout)
Waits for the visibility of the specified
locator for at most timeout
in seconds at most, then attempts to clear the text entry value if this element is a text entry
element. |
org.openqa.selenium.WebElement |
click(org.openqa.selenium.By locator)
Waits for the clickability of the specified
locator for
ElementManager.defaultTimeoutInSeconds() at most, then attempts to click it. |
org.openqa.selenium.WebElement |
click(org.openqa.selenium.By locator,
long timeout)
Waits for the clickability of the specified
locator for specified
timeout at most in seconds, then attempts to click it. |
void |
close() |
void |
closeReport()
Closes the active report and sets it to null (to allow for setting another active report for
the browser).
|
static BrowserDriver |
createBrowser()
Creates a new BrowserDriver using the
Browser specified in the config, system
properties, environment variables, or default if neither is specified. |
static BrowserDriver |
createBrowser(Browser browser)
Creates a new BrowserDriver as the specified
Browser . |
static BrowserDriver |
createBrowser(Browser browser,
org.openqa.selenium.Capabilities capabilities)
Creates a new BrowserDriver as the specified
Browser . |
static BrowserDriver |
createBrowser(Browser browser,
URL url)
|
static BrowserDriver |
createBrowser(org.openqa.selenium.Capabilities capabilities)
Creates a new BrowserDriver using the
Browser specified in the config, system
properties, environment variables, or default if neither is specified. |
static BrowserDriver |
createBrowser(String browser)
Creates a new BrowserDriver as the specified browser alias.
|
static BrowserDriver |
createBrowser(String browser,
org.openqa.selenium.Capabilities capabilities)
Creates a new BrowserDriver as the specified browser alias.
|
static BrowserDriver |
createBrowser(String browser,
URL url)
Creates a new Remote BrowserDriver at the given
URL . |
static BrowserDriver |
createBrowser(URL url)
Creates a new Remote BrowserDriver at the given
URL . |
static RemoteBrowserDriver |
createBrowser(URL url,
org.openqa.selenium.Capabilities capabilities)
Creates a new
RemoteBrowserDriver at the given URL using the specified
Capabilities . |
String |
decrypt(String input) |
long |
defaultTimeoutInSeconds()
The default timeout time for the wait functions.
|
String |
encrypt(String input) |
void |
endTest()
Ends the test on the current active thread.
|
Object |
executeScript(String script,
Object... objects)
Executes JavaScript on the currently selected frame or window.
|
org.openqa.selenium.WebElement |
findElement(org.openqa.selenium.By by) |
List<org.openqa.selenium.WebElement> |
findElements(org.openqa.selenium.By by) |
void |
flush()
flushes the active report (writes all ended tests for this report file to html).
|
void |
get(String url) |
Browser |
getBrowser()
Returns the
Browser set by the BrowserDriver class instancing. |
org.openqa.selenium.Capabilities |
getCapabilities()
Gets the current browser's capabilities.
|
String |
getCurrentUrl() |
<T extends Manager> |
getManager(Class<T> clzz) |
String |
getPageSource() |
String |
getSessionId()
Gets the current browser's session id.
|
ReportsTest |
getTest() |
String |
getText(org.openqa.selenium.By locator)
Waits for the visibility of the specified
locator for
ElementManager.defaultTimeoutInSeconds() at most, then attempts to get the visible (i.e. |
String |
getText(org.openqa.selenium.By locator,
long timeout)
Waits for the visibility of the specified
locator for specified
timeout at most in seconds, then attempts to get the visible (i.e. |
String |
getTitle() |
String |
getVersion() |
String |
getWindowHandle() |
Set<String> |
getWindowHandles() |
boolean |
hasQuit()
.
|
boolean |
isPresent(org.openqa.selenium.By locator)
Searches immediately for the presence of the specified
locator , returning true or
false if found. |
boolean |
isPresent(org.openqa.selenium.By locator,
long timeout)
Waits for the presence of the specified
locator for specified timeout
at most in seconds, then returns true or false. |
org.slf4j.Logger |
logger() |
org.openqa.selenium.WebDriver.Options |
manage() |
org.openqa.selenium.WebDriver.Navigation |
navigate() |
protected abstract org.openqa.selenium.WebDriver |
newInstance(org.openqa.selenium.Capabilities capabilities) |
void |
postInstantiation()
Called immediately after the browser is instantiated.
|
void |
quit() |
Screenshot |
screenshot()
Takes a screenshot of the entire page.
|
ElementShot |
screenshot(org.openqa.selenium.By locator)
To be used when need to screenshot particular element.
|
Screenshot |
screenshot(com.assertthat.selenium_shutterbug.utils.web.ScrollStrategy scroll)
Takes a screenshot of the page using the specified scrolling strategy.
|
ElementShot |
screenshot(org.openqa.selenium.WebElement element)
To be used when need to screenshot particular element.
|
Screenshot |
screenshotViewport()
Takes a screenshot of the currently visible section of the page.
|
void |
scrollTo(org.openqa.selenium.By locator)
Scrolls the browser to the specified location.
|
void |
scrollTo(org.openqa.selenium.WebElement element)
Scrolls the browser to the specified location.
|
org.openqa.selenium.WebElement |
sendKeys(org.openqa.selenium.By locator,
String keysToSend)
Use this method to simulate typing into an element, which may set its value.
|
org.openqa.selenium.WebElement |
sendKeys(org.openqa.selenium.By locator,
String keysToSend,
long timeout)
Use this method to simulate typing into an element, which may set its value.
|
void |
setDefaultTimeout(long timeout) |
void |
setManager(Manager manager) |
void |
setReport(Report report)
Sets the active report for the driver to write to.
|
protected abstract boolean |
setupDriver() |
void |
sleep(long timeout)
Deprecated.
|
ReportsTest |
startTest(String testName)
Starts a new test on the driver's report with the specified name and a blank description.
|
ReportsTest |
startTest(String testName,
String description)
Starts a new test on the driver's report with the specified name and description.
|
org.openqa.selenium.WebDriver.TargetLocator |
switchTo() |
<V> V |
waitUntil(com.google.common.base.Function<? super org.openqa.selenium.WebDriver,V> condition)
Repeatedly applies this instance's input value to the given function until one of the following
occurs:
the function returns neither null nor false,
the function throws an un-ignored exception,
the timeout expires,
the current thread is interrupted
|
<V> V |
waitUntil(com.google.common.base.Function<? super org.openqa.selenium.WebDriver,V> condition,
long timeout)
Repeatedly applies this instance's input value to the given function until one of the following
occurs:
the function returns neither null nor false,
the function throws an un-ignored exception,
the timeout expires,
the current thread is interrupted
|
org.openqa.selenium.WebDriver |
webDriver() |
protected BrowserDriver(Browser browser, org.openqa.selenium.Capabilities capabilities)
protected abstract org.openqa.selenium.WebDriver newInstance(org.openqa.selenium.Capabilities capabilities)
protected abstract boolean setupDriver()
public static BrowserDriver createBrowser()
Browser
specified in the config, system
properties, environment variables, or default if neither is specified.public static BrowserDriver createBrowser(String browser)
Browser
instead.browser
- The alias of the browser you want to instantiate.public static BrowserDriver createBrowser(Browser browser)
Browser
.browser
- Browser
that the returned BrowserDriver should be.public static BrowserDriver createBrowser(org.openqa.selenium.Capabilities capabilities)
Browser
specified in the config, system
properties, environment variables, or default if neither is specified. Instantiates with the
specified Capabilities
.capabilities
- The custom Capabilities
to use when instantiating BrowserDriverCapabilities
public static BrowserDriver createBrowser(String browser, org.openqa.selenium.Capabilities capabilities)
Browser
instead. Instantiates with the specified
Capabilities
.browser
- The alias of the browser you want to instantiatecapabilities
- The custom Capabilities
to use when instantiating BrowserDriverCapabilities
public static BrowserDriver createBrowser(Browser browser, org.openqa.selenium.Capabilities capabilities)
Browser
. Instantiates using the specified
Capabilities
.browser
- Browser
that the returned BrowserDriver should be.capabilities
- The custom Capabilities
to use when instantiating BrowserDriverpublic static BrowserDriver createBrowser(URL url)
URL
. Uses Browser
specified in
the config, system properties, environment variables, or default if neither is specified.url
- The Grid URL
that you want the BrowserDriver to connect to.public static BrowserDriver createBrowser(String browser, URL url)
URL
. Uses the specified browser alias
as the Browser
. If browser does not exist, instantiation uses the default
Browser
instead.browser
- The alias of the browser you want to instantiateurl
- The Grid URL
that you want the BrowserDriver to connect to.public static BrowserDriver createBrowser(Browser browser, URL url)
public static RemoteBrowserDriver createBrowser(URL url, org.openqa.selenium.Capabilities capabilities)
RemoteBrowserDriver
at the given URL using the specified
Capabilities
.
NOTE: The Browser
will be determined by the specified
Capabilities
. The BrowserDriver class itself will be a generic
RemoteBrowserDriver
. If you want a more specific Browser
that is remote, use
createBrowser(Browser, URL)
or createBrowser(String, URL)
instead!
url
- The Grid URL
that you want the BrowserDriver to connect to.capabilities
- The custom Capabilities
to use when instantiating the
RemoteBrowserDriver
.RemoteBrowserDriver
RemoteBrowserDriver
public void setManager(Manager manager)
public org.openqa.selenium.WebDriver webDriver()
webDriver
in interface WebDriverManager
public void postInstantiation()
BrowserManager
postInstantiation
in interface BrowserManager
public void setReport(Report report)
ReportManager
ReportManager.getTest()
,
ReportManager.startTest(String)
, ReportManager.startTest(String, String)
, and ReportManager.endTest()
.
Note it will not set the report if the browser currently has an active one.
setReport
in interface ReportManager
report
- The report to set as the driver's active report.public ReportsTest startTest(String testName)
ReportManager
BrowserConfig.getSaveFolder()
reports
folder with the name set to the current date time.
Note the test is linked to the current thread id (used by ReportManager.getTest()
). If a test is
already active on this thread, a RuntimeException will be thrown.
startTest
in interface ReportManager
testName
- The name of the test created.ReportsTest
created.public ReportsTest startTest(String testName, String description)
ReportManager
BrowserConfig.getSaveFolder()
reports folder
with the name set to the current date time.
Note the test is linked to the current thread id (used by ReportManager.getTest()
). If a test is
already active on this thread, a RuntimeException will be thrown.
startTest
in interface ReportManager
testName
- The name of the test created.description
- The description to set for the test.ReportsTest
created.public ReportsTest getTest()
getTest
in interface ReportManager
public void endTest()
ReportManager
endTest
in interface ReportManager
public void flush()
ReportManager
Note, this has a tendency to be resource intensive since it does not append. When you flush a report, it rewrites the entire file. Try flushing as little as possible to avoid unneeded rewrites.
flush
in interface ReportManager
public void closeReport()
ReportManager
Closing a report runs an intrusive flush. That means it first flushes all ended tests and then force flushes any tests that were not closed.
closeReport
in interface ReportManager
public Screenshot screenshot()
ScreenshotManager
screenshot
in interface ScreenshotManager
Screenshot
instance.public Screenshot screenshot(com.assertthat.selenium_shutterbug.utils.web.ScrollStrategy scroll)
ScreenshotManager
screenshot
in interface ScreenshotManager
scroll
- ScrollStrategy
of how you need to scrollScreenshot
instancepublic ElementShot screenshot(org.openqa.selenium.By locator)
ScreenshotManager
screenshot
in interface ScreenshotManager
locator
- The By instance to be screenshottedpublic ElementShot screenshot(org.openqa.selenium.WebElement element)
ScreenshotManager
screenshot
in interface ScreenshotManager
element
- WebElement instance to be screenshottedpublic Screenshot screenshotViewport()
ScreenshotManager
screenshotViewport
in interface ScreenshotManager
Screenshot
instancepublic void setDefaultTimeout(long timeout)
setDefaultTimeout
in interface ElementManager
timeout
- How long (in seconds) you want the timeout set to for this browserDriver
instance.public long defaultTimeoutInSeconds()
ElementManager
defaultTimeoutInSeconds
in interface ElementManager
public <V> V waitUntil(com.google.common.base.Function<? super org.openqa.selenium.WebDriver,V> condition)
ElementManager
The wait timeout will be set to the ElementManager.defaultTimeoutInSeconds()
waitUntil
in interface ElementManager
V
- The function's expected return type.condition
- the parameter to pass to the ExpectedCondition
public <V> V waitUntil(com.google.common.base.Function<? super org.openqa.selenium.WebDriver,V> condition, long timeout)
ElementManager
waitUntil
in interface ElementManager
V
- The function's expected return type.condition
- the parameter to pass to the ExpectedCondition
timeout
- how long (in seconds) it waits for ExpectedCondition
before expiring.public boolean isPresent(org.openqa.selenium.By locator)
ElementManager
locator
, returning true or
false if found.isPresent
in interface ElementManager
locator
- The locating mechanism.public boolean isPresent(org.openqa.selenium.By locator, long timeout)
ElementManager
locator
for specified timeout
at most in seconds, then returns true or false.isPresent
in interface ElementManager
locator
- The locating mechanism.timeout
- how long (in seconds) it waits for the locator's presence before expiring.public Object executeScript(String script, Object... objects)
ElementManager
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.
executeScript
in interface ElementManager
script
- The JavaScript to executeobjects
- The arguments of the script. May be emptypublic org.openqa.selenium.WebElement click(org.openqa.selenium.By locator)
ElementManager
locator
for
ElementManager.defaultTimeoutInSeconds()
at most, then attempts to click it.click
in interface ElementManager
locator
- The locating mechanism.WebElement
that was clicked.public org.openqa.selenium.WebElement click(org.openqa.selenium.By locator, long timeout)
ElementManager
locator
for specified
timeout
at most in seconds, then attempts to click it.click
in interface ElementManager
locator
- The locating mechanism.timeout
- how long (in seconds) it waits for locator visibility before expiring.WebElement
that was clicked.public org.openqa.selenium.WebElement clear(org.openqa.selenium.By locator)
ElementManager
locator
for
ElementManager.defaultTimeoutInSeconds()
at most, then attempts to clear the text entry value if this
element is a text entry element. Has no effect on other elements.
Text entry elements are INPUT and TEXTAREA elements. Note that the events fired by this
event may not be as you'd expect. In particular, we don't fire any keyboard or mouse events.
If you want to ensure keyboard events are fired, consider using something like
ElementManager.sendKeys(By, String)
with the backspace key. To ensure you get a change event,
consider following with a call to ElementManager.sendKeys(By, String)
with the tab key.
clear
in interface ElementManager
locator
- the locating mechanism.WebElement
that was cleared.public org.openqa.selenium.WebElement clear(org.openqa.selenium.By locator, long timeout)
ElementManager
locator
for at most timeout
in seconds at most, then attempts to clear the text entry value if this element is a text entry
element. Has no effect on other elements.
Text entry elements are INPUT and TEXTAREA elements. Note that the events fired by this
event may not be as you'd expect. In particular, we don't fire any keyboard or mouse events.
If you want to ensure keyboard events are fired, consider using something like
ElementManager.sendKeys(By, String)
with the backspace key. To ensure you get a change event,
consider following with a call to ElementManager.sendKeys(By, String)
with the tab key.
clear
in interface ElementManager
locator
- the locating mechanism.timeout
- how long (in seconds) it waits for locator visibility before expiring.WebElement
that was cleared.public String getText(org.openqa.selenium.By locator)
ElementManager
locator
for
ElementManager.defaultTimeoutInSeconds()
at most, then attempts to get the visible (i.e. not hidden
by CSS) innerText of this element, including sub-elements, without any leading or trailing
whitespace.getText
in interface ElementManager
locator
- the locating mechanism.public String getText(org.openqa.selenium.By locator, long timeout)
ElementManager
locator
for specified
timeout
at most in seconds, then attempts to get the visible (i.e. not hidden by
CSS) innerText of this element, including sub-elements, without any leading or trailing
whitespace.getText
in interface ElementManager
locator
- the locating mechanism.timeout
- how long (in seconds) it waits for locator visibility before expiring.public org.openqa.selenium.WebElement sendKeys(org.openqa.selenium.By locator, String keysToSend)
ElementManager
locator
for at most ElementManager.defaultTimeoutInSeconds()
.sendKeys
in interface ElementManager
locator
- The locating mechanism.keysToSend
- character sequence to send to the elementWebElement
that the keys were sent topublic org.openqa.selenium.WebElement sendKeys(org.openqa.selenium.By locator, String keysToSend, long timeout)
ElementManager
locator
for at most timeout
in seconds.sendKeys
in interface ElementManager
locator
- The locating mechanism.keysToSend
- character sequence to send to the elementtimeout
- how long (in seconds) it waits for locator visibility before expiring.WebElement
that the keys were sent topublic void scrollTo(org.openqa.selenium.By locator)
ElementManager
scrollTo
in interface ElementManager
locator
- The location to scroll to.public void scrollTo(org.openqa.selenium.WebElement element)
ElementManager
scrollTo
in interface ElementManager
element
- The location to scroll to.public void get(String url)
get
in interface org.openqa.selenium.WebDriver
public String getCurrentUrl()
getCurrentUrl
in interface org.openqa.selenium.WebDriver
public String getTitle()
getTitle
in interface org.openqa.selenium.WebDriver
public List<org.openqa.selenium.WebElement> findElements(org.openqa.selenium.By by)
findElements
in interface org.openqa.selenium.SearchContext
findElements
in interface org.openqa.selenium.WebDriver
public org.openqa.selenium.WebElement findElement(org.openqa.selenium.By by)
findElement
in interface org.openqa.selenium.SearchContext
findElement
in interface org.openqa.selenium.WebDriver
public String getPageSource()
getPageSource
in interface org.openqa.selenium.WebDriver
public void close()
close
in interface org.openqa.selenium.WebDriver
public void quit()
quit
in interface org.openqa.selenium.WebDriver
public Set<String> getWindowHandles()
getWindowHandles
in interface org.openqa.selenium.WebDriver
public String getWindowHandle()
getWindowHandle
in interface org.openqa.selenium.WebDriver
public org.openqa.selenium.WebDriver.TargetLocator switchTo()
switchTo
in interface org.openqa.selenium.WebDriver
public org.openqa.selenium.WebDriver.Navigation navigate()
navigate
in interface org.openqa.selenium.WebDriver
public org.openqa.selenium.WebDriver.Options manage()
manage
in interface org.openqa.selenium.WebDriver
public org.slf4j.Logger logger()
logger
in interface WebDriverManager
@Deprecated public void sleep(long timeout) throws InterruptedException
BrowserManager
use waitUntil
instead since pausing threads is
not recommended by Selenium.
sleep
in interface BrowserManager
timeout
- How long the browser waits in milliseconds.InterruptedException
- thrown if sleep interrupts the thread.public boolean hasQuit()
BrowserManager
hasQuit
in interface BrowserManager
public org.openqa.selenium.interactions.Actions actionBuilder()
BrowserManager
Note if pending actions are left unbuilt/unperformed, adding new actions will be appended to the queue instead of starting a new action set.
actionBuilder
in interface BrowserManager
public Browser getBrowser()
BrowserManager
Browser
set by the BrowserDriver class instancing.getBrowser
in interface BrowserManager
Browser
of the browser (as a Usable
).public String getVersion()
getVersion
in interface BrowserManager
public String getSessionId()
BrowserManager
getSessionId
in interface BrowserManager
public org.openqa.selenium.Capabilities getCapabilities()
BrowserManager
getCapabilities
in interface BrowserManager
public String encrypt(String input)
encrypt
in interface EncryptionManager
public String decrypt(String input)
decrypt
in interface EncryptionManager
Copyright © 2019. All rights reserved.