Created
November 23, 2016 01:24
-
-
Save MatisseHack/f50a036d660ca1a5455ee2a6128dbe2d to your computer and use it in GitHub Desktop.
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
| using System; | |
| using System.Collections.Generic; | |
| using System.Text; | |
| using System.Web.Script.Serialization; | |
| using Xamarin.UITest; | |
| namespace YourNamespace | |
| { | |
| public static class TreePrinterAppExtension | |
| { | |
| public static void PrintTree(this IApp app, bool showFullClassName = false, bool showInvisibleElements = false) | |
| { | |
| var builder = new StringBuilder(); | |
| var dump = app.TestServer.Get("/dump"); | |
| var root = new JavaScriptSerializer().Deserialize<DumpElement>(dump); | |
| FormatElement(builder, root, 0, showFullClassName, showInvisibleElements); | |
| Console.WriteLine(builder); | |
| } | |
| static void FormatElement(StringBuilder builder, DumpElement e, int depth, bool showFullClassName, bool showInvisibleElements) | |
| { | |
| for (int i = 0; i < depth * 2; i++) | |
| { | |
| builder.Append(' '); | |
| } | |
| if (!showFullClassName) | |
| { | |
| var lastDot = e.Type.LastIndexOf('.'); | |
| if (lastDot >= 0) | |
| { | |
| e.Type = e.Type.Substring(lastDot + 1); | |
| } | |
| } | |
| builder.Append($"[{e.Type}]"); | |
| if (e.Id != null) | |
| { | |
| builder.Append($" id:\"{e.Id}\""); | |
| } | |
| if (e.Label != null) | |
| { | |
| builder.Append($" label:\"{e.Label}\""); | |
| } | |
| if (e.Name != null) | |
| { | |
| builder.Append($" text:\"{e.Name}\""); | |
| } | |
| builder.Append('\n'); | |
| depth++; | |
| foreach (var child in e.Children) | |
| { | |
| if (child.Visible || showInvisibleElements) | |
| { | |
| FormatElement(builder, child, depth, showFullClassName, showInvisibleElements); | |
| } | |
| } | |
| depth--; | |
| } | |
| class DumpElement | |
| { | |
| public string Type { get; set; } | |
| public string Id { get; set; } | |
| public string Label { get; set; } | |
| public string Name { get; set; } | |
| public bool Visible { get; set; } | |
| public List<DumpElement> Children { get; set; } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment