Skip to content

Commit 8052484

Browse files
committed
feat: support new get events method
1 parent 43a4e79 commit 8052484

File tree

6 files changed

+130
-20
lines changed

6 files changed

+130
-20
lines changed

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,19 @@
1616

1717
package io.appium.java_client;
1818

19+
import static io.appium.java_client.MobileCommand.GET_EVENTS;
1920
import static io.appium.java_client.MobileCommand.LOG_EVENT;
2021

21-
import java.util.HashMap;
22+
import com.google.common.collect.ImmutableMap;
23+
import io.appium.java_client.serverevents.CommandEvent;
24+
import io.appium.java_client.serverevents.ServerEvent;
25+
import io.appium.java_client.serverevents.ServerEvents;
26+
import java.util.ArrayList;
27+
import java.util.List;
2228
import java.util.Map;
29+
import java.util.stream.Collectors;
30+
import org.openqa.selenium.json.Json;
31+
import org.openqa.selenium.remote.Response;
2332

2433
public interface LogsEvents extends ExecutesMethod {
2534

@@ -32,9 +41,44 @@ public interface LogsEvents extends ExecutesMethod {
3241
* @throws org.openqa.selenium.WebDriverException if there was a failure while executing the script
3342
*/
3443
default void logEvent(String vendor, String eventName) {
35-
Map<String, Object> data = new HashMap<>();
36-
data.put("vendor", vendor);
37-
data.put("event", eventName);
38-
execute(LOG_EVENT, data);
44+
execute(LOG_EVENT, ImmutableMap.of("vendor", vendor, "event", eventName));
45+
}
46+
47+
/**
48+
* Log a custom event on the Appium server.
49+
*
50+
* @since Appium 1.16
51+
* @return ServerEvents object wrapping up the various command and event timestamps
52+
* @throws org.openqa.selenium.WebDriverException if there was a failure while executing the script
53+
*/
54+
default ServerEvents getEvents() {
55+
Response response = execute(GET_EVENTS);
56+
ArrayList<CommandEvent> commands = new ArrayList<>();
57+
ArrayList<ServerEvent> events = new ArrayList<>();
58+
String jsonData = new Json().toJson(response.getValue());
59+
//noinspection unchecked
60+
Map<String, Object> value = (Map<String, Object>) response.getValue();
61+
for (String name : value.keySet()) {
62+
if (name.equals("commands")) {
63+
//noinspection unchecked
64+
ArrayList<Map<String, Object>> cmds = (ArrayList<Map<String, Object>>) value.get(name);
65+
for (Map<String, Object> cmd : cmds) {
66+
commands.add(new CommandEvent(
67+
(String) cmd.get("cmd"),
68+
((Long) cmd.get("startTime")).intValue(),
69+
((Long) cmd.get("endTime")).intValue()
70+
));
71+
}
72+
} else {
73+
//noinspection unchecked
74+
ArrayList<Long> timestamps = (ArrayList<Long>) value.get(name);
75+
List<Integer> intStamps = timestamps
76+
.stream()
77+
.map(Long::intValue)
78+
.collect(Collectors.toList());
79+
events.add(new ServerEvent(name, intStamps));
80+
}
81+
}
82+
return new ServerEvents(commands, events, jsonData);
3983
}
4084
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class MobileCommand {
5252
protected static final String GET_DEVICE_TIME;
5353
protected static final String GET_SESSION;
5454
protected static final String LOG_EVENT;
55+
protected static final String GET_EVENTS;
5556

5657
//region Applications Management
5758
protected static final String IS_APP_INSTALLED;
@@ -130,6 +131,7 @@ public class MobileCommand {
130131
GET_DEVICE_TIME = "getDeviceTime";
131132
GET_SESSION = "getSession";
132133
LOG_EVENT = "logCustomEvent";
134+
GET_EVENTS = "getLogEvents";
133135

134136
//region Applications Management
135137
IS_APP_INSTALLED = "isAppInstalled";
@@ -216,6 +218,10 @@ public class MobileCommand {
216218
postC("/session/:sessionId/appium/start_recording_screen"));
217219
commandRepository.put(STOP_RECORDING_SCREEN,
218220
postC("/session/:sessionId/appium/stop_recording_screen"));
221+
commandRepository.put(GET_EVENTS,
222+
postC("/session/:sessionId/appium/events"));
223+
commandRepository.put(LOG_EVENT,
224+
postC("/session/:sessionId/appium/log_event"));
219225

220226
//region Applications Management
221227
commandRepository.put(IS_APP_INSTALLED, postC("/session/:sessionId/appium/device/app_installed"));
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.appium.java_client.serverevents;
2+
3+
public class CommandEvent {
4+
public final String name;
5+
public Integer startTimestamp;
6+
public final Integer endTimestamp;
7+
8+
public CommandEvent(String name, Integer startTimestamp, Integer endTimestamp) {
9+
this.name = name;
10+
this.startTimestamp = startTimestamp;
11+
this.endTimestamp = endTimestamp;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.appium.java_client.serverevents;
2+
3+
import java.util.List;
4+
5+
public class ServerEvent {
6+
public final String name;
7+
public final List<Integer> occurrences;
8+
9+
public ServerEvent(String name, List<Integer> occurrences) {
10+
this.name = name;
11+
this.occurrences = occurrences;
12+
}
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.appium.java_client.serverevents;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.util.List;
7+
8+
public class ServerEvents {
9+
10+
public final List<CommandEvent> commands;
11+
public final List<ServerEvent> events;
12+
public final String jsonData;
13+
14+
public ServerEvents(List<CommandEvent> commands, List<ServerEvent> events, String jsonData) {
15+
this.commands = commands;
16+
this.events = events;
17+
this.jsonData = jsonData;
18+
}
19+
20+
public void save(Path output) throws IOException {
21+
Files.write(output, this.jsonData.getBytes());
22+
}
23+
}

src/test/java/io/appium/java_client/android/LogEventTest.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,38 @@
1616

1717
package io.appium.java_client.android;
1818

19-
import io.appium.java_client.driverscripts.ScriptOptions;
20-
import io.appium.java_client.driverscripts.ScriptType;
21-
import io.appium.java_client.driverscripts.ScriptValue;
22-
import org.junit.Test;
23-
24-
import java.util.Arrays;
25-
import java.util.List;
26-
import java.util.Map;
27-
28-
import static org.hamcrest.Matchers.equalTo;
29-
import static org.hamcrest.core.Is.is;
30-
import static org.junit.Assert.assertNotNull;
3119
import static org.junit.Assert.assertThat;
20+
import static org.junit.Assert.assertTrue;
21+
22+
import io.appium.java_client.serverevents.CommandEvent;
23+
import io.appium.java_client.serverevents.ServerEvent;
24+
import io.appium.java_client.serverevents.ServerEvents;
25+
import org.hamcrest.Matchers;
26+
import org.junit.Test;
3227

3328
public class LogEventTest extends BaseAndroidTest {
3429

3530
@Test
3631
public void verifyLoggingCustomEvents() {
3732
driver.logEvent("appium", "funEvent");
38-
Object value = driver.getSessionDetail("events");
39-
//noinspection unchecked
40-
assertNotNull(((Map<String, Object>) value).get("appium:funEvent"));
33+
ServerEvents events = driver.getEvents();
34+
boolean hasCustomEvent = false;
35+
boolean hasCommandName = false;
36+
for (ServerEvent event : events.events) {
37+
if (event.name.equals("appium:funEvent")) {
38+
assertThat(event.occurrences.get(0), Matchers.greaterThan(0));
39+
hasCustomEvent = true;
40+
break;
41+
}
42+
}
43+
for (CommandEvent cmd : events.commands) {
44+
if (cmd.name.equals("logCustomEvent")) {
45+
hasCommandName = true;
46+
break;
47+
}
48+
}
49+
assertTrue(hasCustomEvent);
50+
assertTrue(hasCommandName);
51+
assertThat(events.jsonData, Matchers.containsString("\"appium:funEvent\""));
4152
}
4253
}

0 commit comments

Comments
 (0)