Skip to content

Commit

Permalink
Restore semantics of the Actions class
Browse files Browse the repository at this point in the history
This means that once `build()` is called, the internal state is
reset.
  • Loading branch information
shs96c committed Feb 20, 2017
1 parent a934e2d commit 5d7ab06
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions java/client/src/org/openqa/selenium/interactions/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -544,32 +544,24 @@ public Actions tick(Action action) {
}

/**
* A no-op left for legacy reasons.
* Generates a composite action containing all actions so far, ready to be performed (and
* resets the internal builder state, so subsequent calls to {@link #build()} will contain fresh
* sequences).
*
* @return the composite action
*/
public Action build() {
if (isBuildingActions()) {
CompositeAction toReturn = action;
action = new CompositeAction();
return toReturn;
}
return this::perform;
Action toReturn = new BuiltAction(driver, sequences, action);
action = new CompositeAction();
sequences.clear();
return toReturn;
}

/**
* Execute all the actions this represents.
* A convenience method for performing the actions without calling build() first.
*/
public void perform() {
try {
((Interactive) driver).perform(sequences.values());
sequences.clear();
} catch (ClassCastException | UnsupportedCommandException e) {
if (actionsException != null) {
throw actionsException;
}
// Fall back to the old way of doing things. Old Skool #ftw
action.perform();
action = new CompositeAction();
}
build().perform();
}

private Sequence getSequence(InputSource source) {
Expand All @@ -592,4 +584,26 @@ private Sequence getSequence(InputSource source) {
private boolean isBuildingActions() {
return jsonMouse != null || jsonKeyboard != null;
}

private static class BuiltAction implements Action {
private final WebDriver driver;
private final Map<InputSource, Sequence> sequences;
private final Action fallBack;

private BuiltAction(WebDriver driver, Map<InputSource, Sequence> sequences, Action fallBack) {
this.driver = driver;
this.sequences = sequences;
this.fallBack = fallBack;
}

@Override
public void perform() {
try {
((Interactive) driver).perform(sequences.values());
} catch (ClassCastException | UnsupportedCommandException e) {
// Fall back to the old way of doing things. Old Skool #ftw
fallBack.perform();
}
}
}
}

0 comments on commit 5d7ab06

Please sign in to comment.