Package com.crawljax.browser
Class WebDriverBackedEmbeddedBrowser
- java.lang.Object
-
- com.crawljax.browser.WebDriverBackedEmbeddedBrowser
-
- All Implemented Interfaces:
EmbeddedBrowser
public final class WebDriverBackedEmbeddedBrowser extends Object implements EmbeddedBrowser
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.crawljax.browser.EmbeddedBrowser
EmbeddedBrowser.BrowserType
-
-
Field Summary
Fields Modifier and Type Field Description private org.openqa.selenium.WebDriverbrowserprivate static intBROWSER_CLOSE_TIMEOUT_SECSprivate longcrawlWaitEventprivate longcrawlWaitReloadprivate com.google.common.collect.ImmutableSortedSet<String>filterAttributesprivate IgnoreFrameCheckerignoreFrameCheckerprivate static org.slf4j.LoggerLOGGERprivate intpixelDensityprivate booleanUSE_CDPChromeDeveloperTools are used to fetch event handler information
-
Constructor Summary
Constructors Modifier Constructor Description privateWebDriverBackedEmbeddedBrowser(org.openqa.selenium.WebDriver driver)Constructor without configuration values.privateWebDriverBackedEmbeddedBrowser(org.openqa.selenium.WebDriver driver, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitReload, long crawlWaitEvent)Constructor.privateWebDriverBackedEmbeddedBrowser(org.openqa.selenium.WebDriver driver, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitReload, long crawlWaitEvent, IgnoreFrameChecker ignoreFrameChecker)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private voidappendFrameContent(Element orig, Document document, String topFrame)private voidawaitForCloseTask(CompletableFuture<?> closeTask, int timeoutSeconds)private static org.openqa.selenium.remote.RemoteWebDriverbuildRemoteWebDriver(String hubUrl, org.openqa.selenium.remote.DesiredCapabilities desiredCapabilities)Private used static method for creation of a RemoteWebDriver.voidclose()Closes the browser.voidcloseOtherWindows()Closes all other windows.booleanelementExists(Identification identification)private booleanexceptionIsConnectionException(org.openqa.selenium.WebDriverException exception)private booleanexceptionIsInteractableException(org.openqa.selenium.WebDriverException exception)ObjectexecuteJavaScript(String code)Execute JavaScript in the browser.private StringfilterAttributes(String html)Filters attributes from the HTML string.booleanfireEventAndWait(Eventable eventable)Fires an event on an element using its identification.private booleanfireEventWait(org.openqa.selenium.WebElement webElement, Eventable eventable)Fires the event and waits for a specified time.org.openqa.selenium.WebDrivergetBrowser()BufferedImagegetCDPScreenshot()private longgetCrawlWaitEvent()private longgetCrawlWaitReload()StringgetCurrentUrl()StringgetDom()Deprecated.private DocumentgetDomTreeWithFrames()private DocumentgetDomTreeWithFrames_GoldStandards()private com.google.common.collect.ImmutableSortedSet<String>getFilterAttributes()StringgetFrameDom(String iframeIdentification)FormInputgetInputWithRandomValue(FormInput input)StringgetScreenShotAsBase64()BufferedImagegetScreenShotAsBufferedImage(int scrollTime)StringgetStrippedDom()Removes the stripped items fromPreCrawlConfiguration.getFilterAttributeNames().StringgetStrippedDomWithoutIframeContent()StringgetUnStrippedDom()Removes the stripped items fromPreCrawlConfiguration.getFilterAttributeNames().org.openqa.selenium.WebDrivergetWebDriver()org.openqa.selenium.WebElementgetWebElement(Identification identification)voidgoBack()Go back to the previous state.voidgoToUrl(URI url)Opens the url in the browser.voidhandlePopups()alert, prompt, and confirm behave as if the OK button is always clicked.booleaninput(Identification identification, String text)booleanisUSE_CDP()booleanisVisible(Identification identification)Determines whether the corresponding element is visible.private voidlocateFrameAndGetSource(Document document, String topFrame, Element frameElement)private voidquit()private voidremoveCanvasGeneratedByFirefoxDriverForScreenshots()voidsaveScreenShot(File file)voidsetPixelDensity(int pixelDensity)private voidsetRandomValues(FormInput input, org.openqa.selenium.WebElement webElement, Set<InputValue> values)voidsetUSE_CDP(boolean USE_CDP)private voidswitchToFrame(String frameIdentification)private <T> TtakeScreenShotOnBrowser(org.openqa.selenium.WebDriver driver, org.openqa.selenium.OutputType<T> outType)private voidthrowIfConnectionException(org.openqa.selenium.WebDriverException exception)private voidthrowIfNotInteractableException(org.openqa.selenium.WebDriverException exception)private StringtoUniformDOM(String html)static WebDriverBackedEmbeddedBrowserwithDriver(org.openqa.selenium.WebDriver driver)Create a WebDriver backed EmbeddedBrowser.static WebDriverBackedEmbeddedBrowserwithDriver(org.openqa.selenium.WebDriver driver, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitEvent, long crawlWaitReload)Create a WebDriver backed EmbeddedBrowser.static WebDriverBackedEmbeddedBrowserwithDriver(org.openqa.selenium.WebDriver driver, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitEvent, long crawlWaitReload, IgnoreFrameChecker ignoreFrameChecker)Create a WebDriver backed EmbeddedBrowser.static WebDriverBackedEmbeddedBrowserwithRemoteDriver(String hubUrl, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitEvent, long crawlWaitReload, IgnoreFrameChecker ignoreFrameChecker, org.openqa.selenium.remote.DesiredCapabilities desiredCapabilities)Create a RemoteWebDriver backed EmbeddedBrowser.static WebDriverBackedEmbeddedBrowserwithRemoteDriver(String hubUrl, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitEvent, long crawlWaitReload, org.openqa.selenium.remote.DesiredCapabilities desiredCapabilities)Create a RemoteWebDriver backed EmbeddedBrowser.static WebDriverBackedEmbeddedBrowserwithRemoteDriver(String hubUrl, org.openqa.selenium.remote.DesiredCapabilities desiredCapabilities)Create a RemoteWebDriver backed EmbeddedBrowser.private RuntimeExceptionwrapWebDriverExceptionIfConnectionException(org.openqa.selenium.WebDriverException exception)
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
-
BROWSER_CLOSE_TIMEOUT_SECS
private static final int BROWSER_CLOSE_TIMEOUT_SECS
- See Also:
- Constant Field Values
-
filterAttributes
private final com.google.common.collect.ImmutableSortedSet<String> filterAttributes
-
browser
private final org.openqa.selenium.WebDriver browser
-
pixelDensity
private int pixelDensity
-
USE_CDP
private boolean USE_CDP
ChromeDeveloperTools are used to fetch event handler information
-
crawlWaitEvent
private long crawlWaitEvent
-
crawlWaitReload
private long crawlWaitReload
-
ignoreFrameChecker
private IgnoreFrameChecker ignoreFrameChecker
-
-
Constructor Detail
-
WebDriverBackedEmbeddedBrowser
private WebDriverBackedEmbeddedBrowser(org.openqa.selenium.WebDriver driver)
Constructor without configuration values.- Parameters:
driver- The WebDriver to use.
-
WebDriverBackedEmbeddedBrowser
private WebDriverBackedEmbeddedBrowser(org.openqa.selenium.WebDriver driver, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitReload, long crawlWaitEvent)Constructor.- Parameters:
driver- The WebDriver to use.filterAttributes- the attributes to be filtered from DOM.crawlWaitReload- the period to wait after a reload.crawlWaitEvent- the period to wait after an event is fired.
-
WebDriverBackedEmbeddedBrowser
private WebDriverBackedEmbeddedBrowser(org.openqa.selenium.WebDriver driver, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitReload, long crawlWaitEvent, IgnoreFrameChecker ignoreFrameChecker)Constructor.- Parameters:
driver- The WebDriver to use.filterAttributes- the attributes to be filtered from DOM.crawlWaitReload- the period to wait after a reload.crawlWaitEvent- the period to wait after an event is fired.ignoreFrameChecker- the checker used to determine if a certain frame must be ignored.
-
-
Method Detail
-
withRemoteDriver
public static WebDriverBackedEmbeddedBrowser withRemoteDriver(String hubUrl, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitEvent, long crawlWaitReload, org.openqa.selenium.remote.DesiredCapabilities desiredCapabilities)
Create a RemoteWebDriver backed EmbeddedBrowser.- Parameters:
hubUrl- Url of the server.filterAttributes- the attributes to be filtered from DOM.crawlWaitReload- the period to wait after a reload.crawlWaitEvent- the period to wait after an event is fired.desiredCapabilities- the desired capabilities to use.- Returns:
- The EmbeddedBrowser.
-
withRemoteDriver
public static WebDriverBackedEmbeddedBrowser withRemoteDriver(String hubUrl, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitEvent, long crawlWaitReload, IgnoreFrameChecker ignoreFrameChecker, org.openqa.selenium.remote.DesiredCapabilities desiredCapabilities)
Create a RemoteWebDriver backed EmbeddedBrowser.- Parameters:
hubUrl- Url of the server.filterAttributes- the attributes to be filtered from DOM.crawlWaitReload- the period to wait after a reload.crawlWaitEvent- the period to wait after an event is fired.ignoreFrameChecker- the checker used to determine if a certain frame must be ignored.desiredCapabilities- the desired capabilities to use.- Returns:
- The EmbeddedBrowser.
-
withDriver
public static WebDriverBackedEmbeddedBrowser withDriver(org.openqa.selenium.WebDriver driver, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitEvent, long crawlWaitReload)
Create a WebDriver backed EmbeddedBrowser.- Parameters:
driver- The WebDriver to use.filterAttributes- the attributes to be filtered from DOM.crawlWaitReload- the period to wait after a reload.crawlWaitEvent- the period to wait after an event is fired.- Returns:
- The EmbeddedBrowser.
-
withDriver
public static WebDriverBackedEmbeddedBrowser withDriver(org.openqa.selenium.WebDriver driver, com.google.common.collect.ImmutableSortedSet<String> filterAttributes, long crawlWaitEvent, long crawlWaitReload, IgnoreFrameChecker ignoreFrameChecker)
Create a WebDriver backed EmbeddedBrowser.- Parameters:
driver- The WebDriver to use.filterAttributes- the attributes to be filtered from DOM.crawlWaitReload- the period to wait after a reload.crawlWaitEvent- the period to wait after an event is fired.ignoreFrameChecker- the checker used to determine if a certain frame must be ignored.- Returns:
- The EmbeddedBrowser.
-
withRemoteDriver
public static WebDriverBackedEmbeddedBrowser withRemoteDriver(String hubUrl, org.openqa.selenium.remote.DesiredCapabilities desiredCapabilities)
Create a RemoteWebDriver backed EmbeddedBrowser.- Parameters:
hubUrl- Url of the server.desiredCapabilities- the desired capabilities to use.- Returns:
- The EmbeddedBrowser.
-
buildRemoteWebDriver
private static org.openqa.selenium.remote.RemoteWebDriver buildRemoteWebDriver(String hubUrl, org.openqa.selenium.remote.DesiredCapabilities desiredCapabilities)
Private used static method for creation of a RemoteWebDriver. Taking care of the default Capabilities and using the HttpCommandExecutor.- Parameters:
hubUrl- the url of the hub to use.- Returns:
- the RemoteWebDriver instance.
-
withDriver
public static WebDriverBackedEmbeddedBrowser withDriver(org.openqa.selenium.WebDriver driver)
Create a WebDriver backed EmbeddedBrowser.- Parameters:
driver- The WebDriver to use.- Returns:
- The EmbeddedBrowser.
-
goToUrl
public void goToUrl(URI url)
Description copied from interface:EmbeddedBrowserOpens the url in the browser.- Specified by:
goToUrlin interfaceEmbeddedBrowser- Parameters:
url- The URL.
-
handlePopups
public void handlePopups()
alert, prompt, and confirm behave as if the OK button is always clicked.- Specified by:
handlePopupsin interfaceEmbeddedBrowser
-
fireEventWait
private boolean fireEventWait(org.openqa.selenium.WebElement webElement, Eventable eventable) throws org.openqa.selenium.ElementNotInteractableException, InterruptedExceptionFires the event and waits for a specified time.- Parameters:
webElement- the element to fire event on.eventable- The HTML event type (onclick, onmouseover, ...).- Returns:
- true if firing event is successful.
- Throws:
InterruptedException- when interrupted during the wait.org.openqa.selenium.ElementNotInteractableException
-
close
public void close()
Description copied from interface:EmbeddedBrowserCloses the browser.- Specified by:
closein interfaceEmbeddedBrowser
-
quit
private void quit()
-
awaitForCloseTask
private void awaitForCloseTask(CompletableFuture<?> closeTask, int timeoutSeconds) throws InterruptedException
- Throws:
InterruptedException
-
getDom
@Deprecated public String getDom()
Deprecated.- Specified by:
getDomin interfaceEmbeddedBrowser- Returns:
- implemented by
EmbeddedBrowser.getStrippedDom().
-
getStrippedDom
public String getStrippedDom()
Description copied from interface:EmbeddedBrowserRemoves the stripped items fromPreCrawlConfiguration.getFilterAttributeNames().- Specified by:
getStrippedDomin interfaceEmbeddedBrowser- Returns:
- the DOM string with all the iframe content.
-
getUnStrippedDom
public String getUnStrippedDom()
Description copied from interface:EmbeddedBrowserRemoves the stripped items fromPreCrawlConfiguration.getFilterAttributeNames().- Specified by:
getUnStrippedDomin interfaceEmbeddedBrowser- Returns:
- The dom without any elements stripped.
- See Also:
WebDriver.getPageSource()
-
toUniformDOM
private String toUniformDOM(String html)
- Parameters:
html- The html string.- Returns:
- uniform version of dom with predefined attributes stripped
-
filterAttributes
private String filterAttributes(String html)
Filters attributes from the HTML string.- Parameters:
html- The HTML to filter.- Returns:
- The filtered HTML string.
-
goBack
public void goBack()
Description copied from interface:EmbeddedBrowserGo back to the previous state.- Specified by:
goBackin interfaceEmbeddedBrowser
-
input
public boolean input(Identification identification, String text)
- Specified by:
inputin interfaceEmbeddedBrowser- Parameters:
identification- The identification object.text- The input.- Returns:
- true if succeeds.
-
fireEventAndWait
public boolean fireEventAndWait(Eventable eventable) throws org.openqa.selenium.ElementNotInteractableException, org.openqa.selenium.NoSuchElementException, InterruptedException
Fires an event on an element using its identification.- Specified by:
fireEventAndWaitin interfaceEmbeddedBrowser- Parameters:
eventable- The eventable.- Returns:
- true if it is able to fire the event successfully on the element.
- Throws:
InterruptedException- when interrupted during the wait.org.openqa.selenium.ElementNotInteractableExceptionorg.openqa.selenium.NoSuchElementException
-
executeJavaScript
public Object executeJavaScript(String code) throws CrawljaxException
Execute JavaScript in the browser.- Specified by:
executeJavaScriptin interfaceEmbeddedBrowser- Parameters:
code- The code to execute.- Returns:
- The return value of the JavaScript.
- Throws:
CrawljaxException- when javascript execution failed.
-
isVisible
public boolean isVisible(Identification identification)
Determines whether the corresponding element is visible.- Specified by:
isVisiblein interfaceEmbeddedBrowser- Parameters:
identification- The element to search for.- Returns:
- true if the element is visible
-
getCurrentUrl
public String getCurrentUrl()
- Specified by:
getCurrentUrlin interfaceEmbeddedBrowser- Returns:
- The current browser url.
-
closeOtherWindows
public void closeOtherWindows()
Description copied from interface:EmbeddedBrowserCloses all other windows.- Specified by:
closeOtherWindowsin interfaceEmbeddedBrowser
-
getDomTreeWithFrames
private Document getDomTreeWithFrames() throws CrawljaxException
- Returns:
- a Document object containing the contents of iframes as well.
- Throws:
CrawljaxException- if an exception is thrown.
-
getDomTreeWithFrames_GoldStandards
private Document getDomTreeWithFrames_GoldStandards() throws CrawljaxException
- Returns:
- a Document object containing the contents of iframes as well.
- Throws:
CrawljaxException- if an exception is thrown.
-
appendFrameContent
private void appendFrameContent(Element orig, Document document, String topFrame)
-
locateFrameAndGetSource
private void locateFrameAndGetSource(Document document, String topFrame, Element frameElement) throws org.openqa.selenium.NoSuchFrameException
- Throws:
org.openqa.selenium.NoSuchFrameException
-
switchToFrame
private void switchToFrame(String frameIdentification) throws org.openqa.selenium.NoSuchFrameException
- Throws:
org.openqa.selenium.NoSuchFrameException
-
getStrippedDomWithoutIframeContent
public String getStrippedDomWithoutIframeContent()
- Specified by:
getStrippedDomWithoutIframeContentin interfaceEmbeddedBrowser- Returns:
- the dom without the iframe contents.
- See Also:
EmbeddedBrowser.getStrippedDomWithoutIframeContent()
-
getInputWithRandomValue
public FormInput getInputWithRandomValue(FormInput input)
- Specified by:
getInputWithRandomValuein interfaceEmbeddedBrowser- Parameters:
input- the input to be filled.- Returns:
- FormInput with random value assigned if possible. If no values were set it returns
null
-
setRandomValues
private void setRandomValues(FormInput input, org.openqa.selenium.WebElement webElement, Set<InputValue> values)
-
getFrameDom
public String getFrameDom(String iframeIdentification)
- Specified by:
getFrameDomin interfaceEmbeddedBrowser- Parameters:
iframeIdentification- the iframe's name or id.- Returns:
- the DOM string of the corresponding iframe.
-
elementExists
public boolean elementExists(Identification identification)
- Specified by:
elementExistsin interfaceEmbeddedBrowser- Parameters:
identification- the identification of the element.- Returns:
- true if the element can be found in the DOM tree.
-
getWebElement
public org.openqa.selenium.WebElement getWebElement(Identification identification)
- Specified by:
getWebElementin interfaceEmbeddedBrowser- Parameters:
identification- the identification of the element.- Returns:
- the found element.
-
getWebDriver
public org.openqa.selenium.WebDriver getWebDriver()
- Specified by:
getWebDriverin interfaceEmbeddedBrowser- Returns:
- The selenium web driver.
-
getCrawlWaitEvent
private long getCrawlWaitEvent()
- Returns:
- the period to wait after an event.
-
getFilterAttributes
private com.google.common.collect.ImmutableSortedSet<String> getFilterAttributes()
- Returns:
- the list of attributes to be filtered from DOM.
-
getCrawlWaitReload
private long getCrawlWaitReload()
- Returns:
- the period to wait after a reload.
-
saveScreenShot
public void saveScreenShot(File file) throws CrawljaxException
- Specified by:
saveScreenShotin interfaceEmbeddedBrowser- Parameters:
file- the file to write the screenshot to (png).- Throws:
CrawljaxException- if saving screenshots is not supported by the implementing class.
-
takeScreenShotOnBrowser
private <T> T takeScreenShotOnBrowser(org.openqa.selenium.WebDriver driver, org.openqa.selenium.OutputType<T> outType) throws CrawljaxException- Throws:
CrawljaxException
-
getCDPScreenshot
public BufferedImage getCDPScreenshot()
-
getScreenShotAsBufferedImage
public BufferedImage getScreenShotAsBufferedImage(int scrollTime)
- Specified by:
getScreenShotAsBufferedImagein interfaceEmbeddedBrowser
-
getScreenShotAsBase64
public String getScreenShotAsBase64() throws CrawljaxException
- Specified by:
getScreenShotAsBase64in interfaceEmbeddedBrowser- Throws:
CrawljaxException
-
removeCanvasGeneratedByFirefoxDriverForScreenshots
private void removeCanvasGeneratedByFirefoxDriverForScreenshots()
-
getBrowser
public org.openqa.selenium.WebDriver getBrowser()
- Returns:
- the WebDriver used as an EmbeddedBrowser.
-
exceptionIsConnectionException
private boolean exceptionIsConnectionException(org.openqa.selenium.WebDriverException exception)
-
wrapWebDriverExceptionIfConnectionException
private RuntimeException wrapWebDriverExceptionIfConnectionException(org.openqa.selenium.WebDriverException exception)
-
throwIfConnectionException
private void throwIfConnectionException(org.openqa.selenium.WebDriverException exception)
-
throwIfNotInteractableException
private void throwIfNotInteractableException(org.openqa.selenium.WebDriverException exception)
-
exceptionIsInteractableException
private boolean exceptionIsInteractableException(org.openqa.selenium.WebDriverException exception)
-
setPixelDensity
public void setPixelDensity(int pixelDensity)
-
isUSE_CDP
public boolean isUSE_CDP()
-
setUSE_CDP
public void setUSE_CDP(boolean USE_CDP)
-
-