aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/google/android/mobly
diff options
context:
space:
mode:
authorSyaoran Kuo <syaoranx@google.com>2020-11-02 11:28:43 +0800
committerGitHub <noreply@github.com>2020-11-02 11:28:43 +0800
commitc5093aad6df3096498cb396da299793d8b13db56 (patch)
treef27cc3a308201a6aa0604313301c48e7540825ed /src/main/java/com/google/android/mobly
parentecc2ee7bd4e3425bb7b4f4cb9cdd6696a2be674e (diff)
downloadmobly-bundled-snippets-c5093aad6df3096498cb396da299793d8b13db56.tar.gz
Add snippet for UI hierarchy dump. (#132)
Diffstat (limited to 'src/main/java/com/google/android/mobly')
-rw-r--r--src/main/java/com/google/android/mobly/snippet/bundled/UiautomatorSnippet.java48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/UiautomatorSnippet.java b/src/main/java/com/google/android/mobly/snippet/bundled/UiautomatorSnippet.java
new file mode 100644
index 0000000..06b8b0c
--- /dev/null
+++ b/src/main/java/com/google/android/mobly/snippet/bundled/UiautomatorSnippet.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2020 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. 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 com.google.android.mobly.snippet.bundled;
+
+import androidx.test.platform.app.InstrumentationRegistry;
+import androidx.test.uiautomator.UiDevice;
+import com.google.android.mobly.snippet.Snippet;
+import com.google.android.mobly.snippet.rpc.Rpc;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * Snippet class exposing Android APIs in Uiautomator.
+ */
+public class UiautomatorSnippet implements Snippet {
+
+ private static final UiDevice device =
+ UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
+
+ @Rpc(description = "Dumps UI hierarchy XML and return as string.")
+ public String uiautomatorDumpWindowHierarchy() throws IOException {
+ String res = "";
+ OutputStream outStream = new ByteArrayOutputStream();
+ device.dumpWindowHierarchy(outStream);
+ res = outStream.toString();
+ outStream.close();
+ return res;
+ }
+
+ @Override
+ public void shutdown() {
+ }
+}