Issue
I wonder if there is a way to observe all actions—in terms of "real" user actions such as clicks—that are being executed via a Selenium driver instance. Initially, I thought about creating a wrapper for WebDriver
which returns wrappers for WebElement
s, where I can observe methods like click()
or sendKeys(CharSequence...)
. Something like:
class WrappingDriver implements WebDriver {
private final WebDriver wrapped;
private final List<Consumer<Action>> consumers;
public WrappingDriver( final WebDriver wrapped, final List<Consumer<Action>> consumers ) {
this.wrapped = wrapped;
this.consumers = consumers;
}
@Override
public WebElement findElement( final By by ) {
return new WrappingElement( wrapped.findElement( by ), consumers );
}
@Override
public List<WebElement> findElements( final By by ) {
return wrapped.findElements( by ).stream() //
.map( element -> new WrappingElement( element, consumers ) ) //
.collect( Collectors.toList() );
}
// ...
}
And:
class WrappingElement implements WebElement {
private final WebElement wrapped;
private final List<Consumer<Action>> consumers;
public WrappingElement( final WebElement wrapped, final List<Consumer<Action>> consumers ) {
this.wrapped = wrapped;
this.consumers = consumers;
}
@Override
public void click() {
consumers.forEach( consumer -> ... );
wrapped.click();
}
@Override
public void sendKeys( final CharSequence... keysToSend ) {
consumers.forEach( consumer -> ... );
wrapped.sendKeys( keysToSend );
}
// ...
}
However, this doesn't work for, e.g., the new interactions APIs. Any suggestions?
EDIT: EventFiringWebDriver
, respectively, WebDriverEventListener
seems to be a good choice. But, if I understood correctly, there is currently no way to observe, e.g., submit()
? (At least this PR seems to be open since 2015.) I have asked for a way to observe all actions, therefore, I think Java Wait for a HTML element and record the mouse click through WebDriverEventListener doesn't exactly answer this question.
Solution
As of Selenium 4, there is an EventFiringDecorator
, which allows observing all WebDriver
events:
This decorator creates a wrapper around an arbitrary
WebDriver
instance that notifies registered listeners about events happening in this WebDriver and derived objects, such asWebElement
s andAlert
.
(Note that the class is marked as @Beta
.)
Answered By - beatngu13
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.