Skip to content

Mykola mokhnach's actions params: The addition to the #756 #760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 12, 2017
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
335 changes: 234 additions & 101 deletions src/main/java/io/appium/java_client/TouchAction.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.android;

import io.appium.java_client.PerformsTouchActions;
import io.appium.java_client.TouchAction;


public class AndroidTouchAction extends TouchAction<AndroidTouchAction> {

public AndroidTouchAction(PerformsTouchActions performsTouchActions) {
super(performsTouchActions);
}

}
50 changes: 39 additions & 11 deletions src/main/java/io/appium/java_client/ios/IOSTouchAction.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.ios;

import static io.appium.java_client.touch.RelativeOffsetOption.useRelative;

import io.appium.java_client.PerformsTouchActions;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.RelativeOffsetOption;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.HasIdentity;


public class IOSTouchAction extends TouchAction {
public class IOSTouchAction extends TouchAction<IOSTouchAction> {

public IOSTouchAction(PerformsTouchActions performsTouchActions) {
super(performsTouchActions);
Expand All @@ -18,24 +35,35 @@ public IOSTouchAction(PerformsTouchActions performsTouchActions) {
* @param el element to tap.
* @param x x offset.
* @param y y offset.
* @return this TouchAction, for chaining.
* @return this IOSTouchAction, for chaining.
* @deprecated use {@link #tap(RelativeOffsetOption)} with count=2 instead.
*/
@Deprecated
public IOSTouchAction doubleTap(WebElement el, int x, int y) {
ActionParameter action = new ActionParameter("doubleTap", (HasIdentity) el);
action.addParameter("x", x);
action.addParameter("y", y);
parameterBuilder.add(action);
return this;
return doubleTap(useRelative(el, x, y));
}

/**
* Double taps an element, offset from upper left corner.
*
* @param el element to tap.
* @return this TouchAction, for chaining.
* @return this IOSTouchAction, for chaining.
* @deprecated use {@link #tap(RelativeOffsetOption)} with count=2 instead.
*/
@Deprecated
public IOSTouchAction doubleTap(WebElement el) {
ActionParameter action = new ActionParameter("doubleTap", (HasIdentity) el);
return doubleTap(useRelative(el));
}

/**
* Double taps using relative offset from an element.
*
* @param doubleTapOption is the relative offset parameter from the element
* @return self-reference
*/
public IOSTouchAction doubleTap(RelativeOffsetOption doubleTapOption) {
ActionParameter action = new ActionParameter("doubleTap",
doubleTapOption);
parameterBuilder.add(action);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.touch;

import static java.util.Optional.ofNullable;

import org.openqa.selenium.Point;

import java.util.Map;

public class AbsoluteOffsetOption extends ActionOptions<AbsoluteOffsetOption> {
private Point absoluteOffset = null;

/**
* It creates an instance of {@link AbsoluteOffsetOption } which takes absolute
* x and y offsets.
*
* @param xOffset the absolute distance from the left screen corner.
* @param yOffset the absolute distance from the top screen corner.
* @return a built option
*/
public static AbsoluteOffsetOption useAbsolute(int xOffset, int yOffset) {
return new AbsoluteOffsetOption().withAbsoluteOffset(xOffset, yOffset);
}

/**
* Set the absolute offset for the corresponding action.
*
* @param xOffset the absolute distance from the left screen corner.
* @param yOffset the absolute distance from the top screen corner.
* @return this instance for chaining.
*/
public AbsoluteOffsetOption withAbsoluteOffset(int xOffset, int yOffset) {
this.absoluteOffset = new Point(xOffset, yOffset);
//noinspection unchecked
return this;
}

@Override
protected void verify() {
ofNullable(absoluteOffset).orElseThrow(() -> new IllegalArgumentException(
"Absolute offset must not be defined"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why must not?

}

@Override
public Map<String, Object> build() {
final Map<String, Object> result = super.build();
ofNullable(absoluteOffset).ifPresent(point -> {
result.put("x", point.x);
result.put("y", point.y);
});
return result;
}
}
40 changes: 40 additions & 0 deletions src/main/java/io/appium/java_client/touch/ActionOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.touch;

import java.util.HashMap;
import java.util.Map;

public abstract class ActionOptions<T extends ActionOptions<T>> {
/**
* This method is automatically called before building
* options map to verify the consistency of the instance.
*
* @throws IllegalArgumentException if there are problems with this options map.
*/
protected abstract void verify();

/**
* Creates a map based on the provided options.
*
* @return options mapping.
*/
public Map<String, Object> build() {
verify();
return new HashMap<>();
}
}
60 changes: 60 additions & 0 deletions src/main/java/io/appium/java_client/touch/LongPressOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.touch;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Optional.ofNullable;

import java.time.Duration;
import java.util.Map;

public class LongPressOptions extends OptionsCombinedWithOffset<LongPressOptions> {
protected Duration duration = null;

/**
* It creates an empty instance of {@link LongPressOptions}.
*
* @return an empty instance of {@link LongPressOptions}
*/
public static LongPressOptions longPressOptions() {
return new LongPressOptions();
}

/**
* Set the long press duration.
*
* @param duration the value to set.
* Time resolution unit is 1 ms.
* @return this instance for chaining.
*/
public LongPressOptions withDuration(Duration duration) {
checkNotNull(duration);
checkArgument(duration.toMillis() >= 0,
"Duration value should be greater or equal to zero");
this.duration = duration;
return this;
}

@Override
public Map<String, Object> build() {
final Map<String, Object> result = super.build();
ofNullable(duration).ifPresent(durationParam ->
result.put("duration", durationParam.toMillis()));
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.appium.java_client.touch;

import static java.util.Optional.ofNullable;

import java.util.Map;

public abstract class OptionsCombinedWithOffset<T extends OptionsCombinedWithOffset>
extends ActionOptions<OptionsCombinedWithOffset<T>> {
private ActionOptions<?> offsetOption;

/**
* Some actions may require some absolute offset value to be performed.
* Invocation of this method replaces the result of previous invocation of
* the {@link #withOffset(RelativeOffsetOption)}
*
* @param offset is the values of required absolute offset from the left corner of a screen. *
* @return self-reference
*/
public T withOffset(AbsoluteOffsetOption offset) {
offsetOption = offset;
return (T) this;
}

/**
* Some actions may require some relative offset value to be performed.
* Invocation of this method replaces the result of previous invocation of
* the {@link #withOffset(AbsoluteOffsetOption)}
*
* @param offset is the values of required offset from the left corner of an element. *
* @return self-reference
*/
public T withOffset(RelativeOffsetOption offset) {
offsetOption = offset;
return (T) this;
}

protected void verify() {
ofNullable(offsetOption).orElseThrow(() ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should consider here, that useRelative(x,y) won't work as expected for press/tap/longTap. I think the best way to avoid it would be to extract element parameter from RelativeOffset into withElement like we discussed before

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main reason of this is that we anyway translate both relative and absolute offset into x,y JSON keys, so there is no way for the server do distinguish them (unless we start using W3C JSON format to represent the stuff, which is already awaiting in the queue).

new IllegalArgumentException("Some relative or absolute offset should "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

->Either relative or absolute

+ "be defined. Use one of withOffset methods"));
}

@Override
public Map<String, Object> build() {
final Map<String, Object> result = super.build();
result.putAll(offsetOption.build());
return result;
}
}
Loading