From 5d7ab06913d7d06b6d5a9341a7946f2191096ff4 Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Mon, 20 Feb 2017 14:18:29 +0000 Subject: [PATCH] Restore semantics of the Actions class This means that once `build()` is called, the internal state is reset. --- .../openqa/selenium/interactions/Actions.java | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/java/client/src/org/openqa/selenium/interactions/Actions.java b/java/client/src/org/openqa/selenium/interactions/Actions.java index e1b80f94aeb18..fb4ac59a65230 100644 --- a/java/client/src/org/openqa/selenium/interactions/Actions.java +++ b/java/client/src/org/openqa/selenium/interactions/Actions.java @@ -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) { @@ -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 sequences; + private final Action fallBack; + + private BuiltAction(WebDriver driver, Map 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(); + } + } + } }