-
-
Notifications
You must be signed in to change notification settings - Fork 765
feat: add support for logCustomEvent command #1262
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import static io.appium.java_client.MobileCommand.GET_EVENTS; | ||
| import static io.appium.java_client.MobileCommand.LOG_EVENT; | ||
|
|
||
| import com.google.common.collect.ImmutableMap; | ||
| import io.appium.java_client.serverevents.CommandEvent; | ||
| import io.appium.java_client.serverevents.CustomEvent; | ||
| import io.appium.java_client.serverevents.TimedEvent; | ||
| import io.appium.java_client.serverevents.ServerEvents; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import org.openqa.selenium.json.Json; | ||
| import org.openqa.selenium.remote.Response; | ||
|
|
||
| public interface LogsEvents extends ExecutesMethod { | ||
|
|
||
| /** | ||
| * Log a custom event on the Appium server. | ||
| * | ||
| * @since Appium 1.16 | ||
| * @param event the event to log | ||
| * @throws org.openqa.selenium.WebDriverException if there was a failure while executing the script | ||
| */ | ||
| default void logEvent(CustomEvent event) { | ||
| execute(LOG_EVENT, ImmutableMap.of("vendor", event.getVendor(), "event", event.getEventName())); | ||
| } | ||
|
|
||
| /** | ||
| * Log a custom event on the Appium server. | ||
| * | ||
| * @since Appium 1.16 | ||
| * @return ServerEvents object wrapping up the various command and event timestamps | ||
| * @throws org.openqa.selenium.WebDriverException if there was a failure while executing the script | ||
| */ | ||
| default ServerEvents getEvents() { | ||
| Response response = execute(GET_EVENTS); | ||
| String jsonData = new Json().toJson(response.getValue()); | ||
|
|
||
| //noinspection unchecked | ||
| Map<String, Object> value = (Map<String, Object>) response.getValue(); | ||
|
|
||
| //noinspection unchecked | ||
| List<CommandEvent> commands = ((List<Map<String, Object>>) value.get("commands")) | ||
| .stream() | ||
| .map((Map<String, Object> cmd) -> new CommandEvent( | ||
| (String) cmd.get("cmd"), | ||
| ((Long) cmd.get("startTime")), | ||
| ((Long) cmd.get("endTime")) | ||
| )) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| List<TimedEvent> events = value.keySet().stream() | ||
| .filter((String name) -> !name.equals("commands")) | ||
| .map((String name) -> { | ||
| //noinspection unchecked | ||
| return new TimedEvent(name, (List<Long>) value.get(name)); | ||
| }) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return new ServerEvents(commands, events, jsonData); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/io/appium/java_client/serverevents/CommandEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package io.appium.java_client.serverevents; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class CommandEvent { | ||
| public final String name; | ||
| public final long startTimestamp; | ||
| public final long endTimestamp; | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
src/main/java/io/appium/java_client/serverevents/CustomEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package io.appium.java_client.serverevents; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class CustomEvent { | ||
| private String vendor; | ||
| private String eventName; | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/io/appium/java_client/serverevents/ServerEvents.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package io.appium.java_client.serverevents; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class ServerEvents { | ||
|
|
||
| public final List<CommandEvent> commands; | ||
| public final List<TimedEvent> events; | ||
| public final String jsonData; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should it be string or JSONObject?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. string, because its only purpose is to write out to disk. |
||
|
|
||
| public void save(Path output) throws IOException { | ||
| Files.write(output, this.jsonData.getBytes()); | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
src/main/java/io/appium/java_client/serverevents/TimedEvent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package io.appium.java_client.serverevents; | ||
|
|
||
| import java.util.List; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class TimedEvent { | ||
jlipps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public final String name; | ||
| public final List<Long> occurrences; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/test/java/io/appium/java_client/android/LogEventTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * 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 static org.junit.Assert.assertThat; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import io.appium.java_client.serverevents.CommandEvent; | ||
| import io.appium.java_client.serverevents.CustomEvent; | ||
| import io.appium.java_client.serverevents.TimedEvent; | ||
| import io.appium.java_client.serverevents.ServerEvents; | ||
| import org.hamcrest.Matchers; | ||
| import org.junit.Test; | ||
|
|
||
| public class LogEventTest extends BaseAndroidTest { | ||
|
|
||
| @Test | ||
| public void verifyLoggingCustomEvents() { | ||
| CustomEvent evt = new CustomEvent(); | ||
| evt.setEventName("funEvent"); | ||
| evt.setVendor("appium"); | ||
| driver.logEvent(evt); | ||
| ServerEvents events = driver.getEvents(); | ||
| boolean hasCustomEvent = events.events.stream().anyMatch((TimedEvent event) -> | ||
| event.name.equals("appium:funEvent") && | ||
| event.occurrences.get(0).intValue() > 0 | ||
| ); | ||
| boolean hasCommandName = events.commands.stream().anyMatch((CommandEvent event) -> | ||
| event.name.equals("logCustomEvent") | ||
| ); | ||
| assertTrue(hasCustomEvent); | ||
| assertTrue(hasCommandName); | ||
| assertThat(events.jsonData, Matchers.containsString("\"appium:funEvent\"")); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.