Skip to content

Commit

Permalink
Make the w3c actions available from the existing Actions classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Feb 20, 2017
1 parent e8a2a65 commit 84c847e
Show file tree
Hide file tree
Showing 18 changed files with 278 additions and 12 deletions.
7 changes: 6 additions & 1 deletion java/client/src/org/openqa/selenium/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,19 @@ public enum Keys implements CharSequence {
ZENKAKU_HANKAKU ('\uE040');

private final char keyCode;
private final int codePoint;

Keys(Keys key) {
this(key.charAt(0));
}

Keys(char keyCode) {
this.keyCode = keyCode;
this.codePoint = String.valueOf(keyCode).codePoints().findFirst().getAsInt();
}

public int getCodePoint() {
return codePoint;
}

public char charAt(int index) {
Expand Down Expand Up @@ -182,5 +188,4 @@ public static Keys getKeyFromUnicode(char key) {

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.List;

/**
* Releases the left mouse button
*
* @deprecated Use {@link Actions#release()}
*/
@Deprecated
public class ButtonReleaseAction extends MouseAction implements Action {

public ButtonReleaseAction(Mouse mouse, Locatable locationProvider) {
super(mouse, locationProvider);
}
Expand All @@ -41,4 +46,14 @@ public void perform() {
moveToLocation();
mouse.mouseUp(getActionLocation());
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
ImmutableList.Builder<Interaction> interactions = ImmutableList.builder();

moveToLocation(mouse, interactions);
interactions.add(mouse.createPointerUp(Button.LEFT.asArg()));

return interactions.build();
}
}
15 changes: 15 additions & 0 deletions java/client/src/org/openqa/selenium/interactions/ClickAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.List;

/**
* Clicks an element.
* @deprecated Use {@link Actions#click(WebElement)}
Expand All @@ -35,4 +39,15 @@ public void perform() {
moveToLocation();
mouse.click(getActionLocation());
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
ImmutableList.Builder<Interaction> interactions = ImmutableList.builder();

moveToLocation(mouse, interactions);
interactions.add(mouse.createPointerDown(Button.LEFT.asArg()));
interactions.add(mouse.createPointerUp(Button.LEFT.asArg()));

return interactions.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.List;

/**
* Presses the left mouse button without releasing it.
*
Expand All @@ -41,4 +45,13 @@ public void perform() {
moveToLocation();
mouse.mouseDown(getActionLocation());
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
ImmutableList.Builder<Interaction> interactions = ImmutableList.builder();

moveToLocation(mouse, interactions);

return interactions.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.openqa.selenium.interactions;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -26,8 +28,8 @@
* An action for aggregating actions and triggering all of them at the same time.
*
*/
public class CompositeAction implements Action {
private List<Action> actionsList = new ArrayList<>();
public class CompositeAction implements Action, IsInteraction {
private final List<Action> actionsList = new ArrayList<>();

public void perform() {
for (Action action : actionsList) {
Expand All @@ -36,6 +38,7 @@ public void perform() {
}

public CompositeAction addAction(Action action) {
Preconditions.checkNotNull(action, "Null actions are not supported.");
actionsList.add(action);
return this;
}
Expand All @@ -48,4 +51,20 @@ public CompositeAction addAction(Action action) {
int getNumberOfActions() {
return actionsList.size();
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
ImmutableList.Builder<Interaction> interactions = ImmutableList.builder();

for (Action action : actionsList) {
if (!(action instanceof IsInteraction)) {
throw new IllegalArgumentException(
String.format("Action must implement IsInteraction: %s", action));
}

interactions.addAll(((IsInteraction) action).asInteractions(mouse, keyboard));
}

return interactions.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.List;

/**
* Context-clicks an element
*
Expand All @@ -39,4 +43,15 @@ public void perform() {
moveToLocation();
mouse.contextClick(getActionLocation());
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
ImmutableList.Builder<Interaction> interactions = ImmutableList.builder();

moveToLocation(mouse, interactions);
interactions.add(mouse.createPointerDown(Button.RIGHT.asArg()));
interactions.add(mouse.createPointerUp(Button.RIGHT.asArg()));

return interactions.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.List;

/**
* Double-clicks an element.
*
Expand All @@ -39,4 +43,17 @@ public void perform() {
moveToLocation();
mouse.doubleClick(getActionLocation());
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
ImmutableList.Builder<Interaction> interactions = ImmutableList.builder();

moveToLocation(mouse, interactions);
interactions.add(mouse.createPointerDown(Button.LEFT.asArg()));
interactions.add(mouse.createPointerUp(Button.LEFT.asArg()));
interactions.add(mouse.createPointerDown(Button.LEFT.asArg()));
interactions.add(mouse.createPointerUp(Button.LEFT.asArg()));

return interactions.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.internal.SingleKeyAction;
import org.openqa.selenium.internal.Locatable;

import java.util.List;

/**
* Emulates key press only, without the release.
*
Expand All @@ -42,4 +46,14 @@ public void perform() {

keyboard.pressKey(key);
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
ImmutableList.Builder<Interaction> interactions = ImmutableList.builder();

optionallyClickElement(mouse, interactions);
interactions.add(keyboard.createKeyDown(key.getCodePoint()));

return interactions.build();
}
}
15 changes: 15 additions & 0 deletions java/client/src/org/openqa/selenium/interactions/KeyUpAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.Keys;
import org.openqa.selenium.interactions.internal.SingleKeyAction;
import org.openqa.selenium.internal.Locatable;

import java.util.List;

/**
* Emulates key release only, without the press.
*
Expand All @@ -41,4 +45,15 @@ public void perform() {

keyboard.releaseKey(key);
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
ImmutableList.Builder<Interaction> toReturn = ImmutableList.builder();

optionallyClickElement(mouse, toReturn);
toReturn.add(keyboard.createKeyUp(key.getCodePoint()));

return toReturn.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.List;

/**
* Moves the mouse to an element.
*
Expand All @@ -38,4 +42,13 @@ public MoveMouseAction(Mouse mouse, Locatable locationProvider) {
public void perform() {
mouse.mouseMove(getActionLocation());
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
ImmutableList.Builder<Interaction> interactions = ImmutableList.builder();

moveToLocation(mouse, interactions);

return interactions.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.PointerInput.Origin;
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.time.Duration;
import java.util.List;
import java.util.Optional;

/**
* Move the mouse to a location within the element provided. The coordinates provided specify the
* offset from the top-left corner of the element.
Expand All @@ -41,4 +48,19 @@ public MoveToOffsetAction(Mouse mouse, Locatable locationProvider, int x, int y)
public void perform() {
mouse.mouseMove(getActionLocation(), xOffset, yOffset);
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
Optional<WebElement> target = getTargetElement();

ImmutableList.Builder<Interaction> interactions = ImmutableList.builder();

interactions.add(mouse.createPointerMove(
Duration.ofMillis(500),
target.map(Origin::fromElement).orElse(Origin.pointer()),
xOffset,
yOffset));

return interactions.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@

package org.openqa.selenium.interactions;

import com.google.common.collect.ImmutableList;

import java.time.Duration;
import java.util.List;

/**
* Takes a pause.
*
* @deprecated 'Pause' is considered to be a bad design practice.
*/
@Deprecated
public class PauseAction implements Action {
public class PauseAction implements Action, IsInteraction {

private final long pause;

Expand All @@ -37,4 +42,9 @@ public void perform() {
} catch (InterruptedException e) {
}
}

@Override
public List<Interaction> asInteractions(PointerInput mouse, KeyInput keyboard) {
return ImmutableList.of(new Pause(keyboard, Duration.ofMillis(pause)));
}
}
Loading

0 comments on commit 84c847e

Please sign in to comment.