Skip to content

Commit a433896

Browse files
Merge pull request #760 from TikhomirovSergey/mykola-mokhnach-actions_params
Mykola mokhnach's actions params: The addition to the #756
2 parents ec9f586 + 52492ec commit a433896

17 files changed

+1016
-167
lines changed

src/main/java/io/appium/java_client/TouchAction.java

Lines changed: 189 additions & 106 deletions
Large diffs are not rendered by default.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.android;
18+
19+
import io.appium.java_client.PerformsTouchActions;
20+
import io.appium.java_client.TouchAction;
21+
22+
23+
public class AndroidTouchAction extends TouchAction<AndroidTouchAction> {
24+
25+
public AndroidTouchAction(PerformsTouchActions performsTouchActions) {
26+
super(performsTouchActions);
27+
}
28+
29+
}

src/main/java/io/appium/java_client/ios/IOSTouchAction.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.appium.java_client.ios;
218

19+
import static io.appium.java_client.touch.offset.ElementOption.element;
20+
321
import io.appium.java_client.PerformsTouchActions;
422
import io.appium.java_client.TouchAction;
23+
import io.appium.java_client.touch.offset.ElementOption;
24+
import io.appium.java_client.touch.offset.PointOption;
525
import org.openqa.selenium.WebElement;
6-
import org.openqa.selenium.internal.HasIdentity;
7-
826

9-
public class IOSTouchAction extends TouchAction {
27+
public class IOSTouchAction extends TouchAction<IOSTouchAction> {
1028

1129
public IOSTouchAction(PerformsTouchActions performsTouchActions) {
1230
super(performsTouchActions);
@@ -18,24 +36,35 @@ public IOSTouchAction(PerformsTouchActions performsTouchActions) {
1836
* @param el element to tap.
1937
* @param x x offset.
2038
* @param y y offset.
21-
* @return this TouchAction, for chaining.
39+
* @return this IOSTouchAction, for chaining.
40+
* @deprecated use {@link #doubleTap(PointOption)} with count=2 instead.
2241
*/
42+
@Deprecated
2343
public IOSTouchAction doubleTap(WebElement el, int x, int y) {
24-
ActionParameter action = new ActionParameter("doubleTap", (HasIdentity) el);
25-
action.addParameter("x", x);
26-
action.addParameter("y", y);
27-
parameterBuilder.add(action);
28-
return this;
44+
return doubleTap(element(el, x, y));
2945
}
3046

3147
/**
3248
* Double taps an element, offset from upper left corner.
3349
*
3450
* @param el element to tap.
35-
* @return this TouchAction, for chaining.
51+
* @return this IOSTouchAction, for chaining.
52+
* @deprecated use {@link #doubleTap(PointOption)} with count=2 instead.
3653
*/
54+
@Deprecated
3755
public IOSTouchAction doubleTap(WebElement el) {
38-
ActionParameter action = new ActionParameter("doubleTap", (HasIdentity) el);
56+
return doubleTap(element(el));
57+
}
58+
59+
/**
60+
* Double taps using coordinates.
61+
*
62+
* @param doubleTapOption see {@link PointOption} and {@link ElementOption}..
63+
* @return self-reference
64+
*/
65+
public IOSTouchAction doubleTap(PointOption doubleTapOption) {
66+
ActionParameter action = new ActionParameter("doubleTap",
67+
doubleTapOption);
3968
parameterBuilder.add(action);
4069
return this;
4170
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.touch;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
public abstract class ActionOptions<T extends ActionOptions<T>> {
23+
/**
24+
* This method is automatically called before building
25+
* options map to verify the consistency of the instance.
26+
*
27+
* @throws IllegalArgumentException if there are problems with this options map.
28+
*/
29+
protected abstract void verify();
30+
31+
/**
32+
* Creates a map based on the provided options.
33+
*
34+
* @return options mapping.
35+
*/
36+
public Map<String, Object> build() {
37+
verify();
38+
return new HashMap<>();
39+
}
40+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.touch;
18+
19+
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
import static java.util.Optional.ofNullable;
22+
23+
import io.appium.java_client.touch.offset.AbstractOptionCombinedWithPosition;
24+
25+
import java.time.Duration;
26+
import java.util.Map;
27+
28+
public class LongPressOptions extends AbstractOptionCombinedWithPosition<LongPressOptions> {
29+
protected Duration duration = null;
30+
31+
/**
32+
* It creates an empty instance of {@link LongPressOptions}.
33+
*
34+
* @return an empty instance of {@link LongPressOptions}
35+
*/
36+
public static LongPressOptions longPressOptions() {
37+
return new LongPressOptions();
38+
}
39+
40+
/**
41+
* Set the long press duration.
42+
*
43+
* @param duration the value to set.
44+
* Time resolution unit is 1 ms.
45+
* @return this instance for chaining.
46+
*/
47+
public LongPressOptions withDuration(Duration duration) {
48+
checkNotNull(duration);
49+
checkArgument(duration.toMillis() >= 0,
50+
"Duration value should be greater or equal to zero");
51+
this.duration = duration;
52+
return this;
53+
}
54+
55+
@Override
56+
public Map<String, Object> build() {
57+
final Map<String, Object> result = super.build();
58+
ofNullable(duration).ifPresent(durationParam ->
59+
result.put("duration", durationParam.toMillis()));
60+
return result;
61+
}
62+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.touch;
18+
19+
import static com.google.common.base.Preconditions.checkArgument;
20+
import static java.util.Optional.ofNullable;
21+
22+
import io.appium.java_client.touch.offset.AbstractOptionCombinedWithPosition;
23+
24+
import java.util.Map;
25+
26+
public class TapOptions extends AbstractOptionCombinedWithPosition<TapOptions> {
27+
private Integer tapsCount = null;
28+
29+
/**
30+
* It creates an empty instance of {@link TapOptions}.
31+
*
32+
* @return the empty instance of {@link TapOptions}
33+
*/
34+
public static TapOptions tapOptions() {
35+
return new TapOptions();
36+
}
37+
38+
/**
39+
* Set the count of taps to perform.
40+
*
41+
* @param tapsCount the taps count to perform.
42+
* The value should be greater than zero.
43+
* @return this instance for chaining.
44+
*/
45+
public TapOptions withTapsCount(int tapsCount) {
46+
checkArgument(tapsCount > 0, "Taps count should be greater than zero");
47+
this.tapsCount = tapsCount;
48+
return this;
49+
}
50+
51+
@Override
52+
public Map<String, Object> build() {
53+
final Map<String, Object> result = super.build();
54+
ofNullable(tapsCount).ifPresent(integer -> result.put("count", integer));
55+
return result;
56+
}
57+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.touch;
18+
19+
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
import static java.time.Duration.ofMillis;
22+
23+
import java.time.Duration;
24+
import java.util.Map;
25+
26+
public class WaitOptions extends ActionOptions<WaitOptions> {
27+
protected Duration duration = ofMillis(0);
28+
29+
/**
30+
* Creates and instance of {@link WaitOptions}.
31+
*
32+
* @param duration is the duration of the waiting.
33+
* @return a built option.
34+
*/
35+
public static WaitOptions waitOptions(Duration duration) {
36+
return new WaitOptions().withDuration(duration);
37+
}
38+
39+
/**
40+
* Set the wait duration.
41+
*
42+
* @param duration the value to set.
43+
* Time resolution unit is 1 ms.
44+
* @return this instance for chaining.
45+
*/
46+
public WaitOptions withDuration(Duration duration) {
47+
checkNotNull(duration);
48+
checkArgument(duration.toMillis() >= 0,
49+
"Duration value should be greater or equal to zero");
50+
this.duration = duration;
51+
return this;
52+
}
53+
54+
@Override
55+
protected void verify() {
56+
//there is nothing to check
57+
}
58+
59+
@Override
60+
public Map<String, Object> build() {
61+
final Map<String, Object> result = super.build();
62+
result.put("ms", duration.toMillis());
63+
return result;
64+
}
65+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.appium.java_client.touch.offset;
2+
3+
import static java.util.Optional.ofNullable;
4+
5+
import io.appium.java_client.touch.ActionOptions;
6+
7+
import java.util.Map;
8+
9+
public abstract class AbstractOptionCombinedWithPosition<T extends AbstractOptionCombinedWithPosition<T>>
10+
extends ActionOptions<AbstractOptionCombinedWithPosition<T>> {
11+
private ActionOptions<?> positionOption;
12+
13+
/**
14+
* Some actions may require coordinates. Invocation of this method
15+
* replaces the result of previous {@link #withElement(ElementOption)} invocation.
16+
*
17+
* @param positionOption required coordinates. *
18+
* @return self-reference
19+
*/
20+
public T withPosition(PointOption positionOption) {
21+
this.positionOption = positionOption;
22+
return (T) this;
23+
}
24+
25+
/**
26+
* Most of touch action may use position which is relative to some element. In order to unify
27+
* this behaviour this method was added. Invocation of this method
28+
* replaces the result of previous {@link #withPosition(PointOption)} invocation.
29+
*
30+
* @param element required position which is relative to some element
31+
* @return self-reference
32+
*/
33+
public T withElement(ElementOption element) {
34+
positionOption = element;
35+
return (T) this;
36+
}
37+
38+
protected void verify() {
39+
ofNullable(positionOption).orElseThrow(() ->
40+
new IllegalArgumentException("Some coordinates or an offset from an element should "
41+
+ "be defined. Use withPosition or withElement methods"));
42+
}
43+
44+
@Override
45+
public Map<String, Object> build() {
46+
final Map<String, Object> result = super.build();
47+
result.putAll(positionOption.build());
48+
return result;
49+
}
50+
}

0 commit comments

Comments
 (0)