Skip to content

Commit

Permalink
Add DumpScreenWinRT to return winrt XmlDocument
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed May 28, 2023
1 parent fe936cf commit a8163d4
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 7 deletions.
4 changes: 4 additions & 0 deletions AdvancedSharpAdbClient.Tests/Dummys/DummyAdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public Task ExecuteRemoteCommandAsync(string command, DeviceData device, IShellO

public Task<XmlDocument> DumpScreenAsync(DeviceData device, CancellationToken cancellationToken) => throw new NotImplementedException();

public string DumpScreenString(DeviceData device) => throw new NotImplementedException();

public Task<string> DumpScreenStringAsync(DeviceData device, CancellationToken cancellationToken) => throw new NotImplementedException();

public Element FindElement(DeviceData device, string xpath, TimeSpan timeout = default) => throw new NotImplementedException();

public Task<Element> FindElementAsync(DeviceData device, string xpath, CancellationToken cancellationToken) => throw new NotImplementedException();
Expand Down
29 changes: 27 additions & 2 deletions AdvancedSharpAdbClient/AdbClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,9 @@ public async Task<IEnumerable<string>> GetFeatureSetAsync(DeviceData device, Can
}

/// <inheritdoc/>
public async Task<XmlDocument> DumpScreenAsync(DeviceData device, CancellationToken cancellationToken = default)
public async Task<string> DumpScreenStringAsync(DeviceData device, CancellationToken cancellationToken = default)
{
XmlDocument doc = new();
EnsureDevice(device);
using IAdbSocket socket = adbSocketFactory(EndPoint);
await socket.SetDeviceAsync(device, cancellationToken);
await socket.SendAdbRequestAsync("shell:uiautomator dump /dev/tty", cancellationToken);
Expand All @@ -696,6 +696,30 @@ public async Task<XmlDocument> DumpScreenAsync(DeviceData device, CancellationTo
string xmlString = await Utilities.Run(reader.ReadToEnd, cancellationToken).ConfigureAwait(false);
#endif
xmlString = xmlString.Replace("Events injected: 1\r\n", "").Replace("UI hierchary dumped to: /dev/tty", "").Trim();
return xmlString;
}

/// <inheritdoc/>
public async Task<XmlDocument> DumpScreenAsync(DeviceData device, CancellationToken cancellationToken = default)
{
XmlDocument doc = new();
string xmlString = await DumpScreenStringAsync(device, cancellationToken);
if (!string.IsNullOrEmpty(xmlString)
&& !xmlString.StartsWith("ERROR")
&& !xmlString.StartsWith("java.lang.Exception"))
{
doc.LoadXml(xmlString);
return doc;
}
return null;
}

#if WINDOWS_UWP
/// <inheritdoc/>
public async Task<Windows.Data.Xml.Dom.XmlDocument> DumpScreenWinRTAsync(DeviceData device, CancellationToken cancellationToken = default)
{
Windows.Data.Xml.Dom.XmlDocument doc = new();
string xmlString = await DumpScreenStringAsync(device, cancellationToken);
if (!string.IsNullOrEmpty(xmlString)
&& !xmlString.StartsWith("ERROR")
&& !xmlString.StartsWith("java.lang.Exception"))
Expand All @@ -705,6 +729,7 @@ public async Task<XmlDocument> DumpScreenAsync(DeviceData device, CancellationTo
}
return null;
}
#endif

/// <inheritdoc/>
public async Task ClickAsync(DeviceData device, Cords cords, CancellationToken cancellationToken = default)
Expand Down
37 changes: 35 additions & 2 deletions AdvancedSharpAdbClient/AdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ public void InstallWrite(DeviceData device, Stream apk, string apkName, string s
/// <inheritdoc/>
public void InstallCommit(DeviceData device, string session)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
socket.SetDevice(device);

Expand All @@ -637,6 +639,8 @@ public void InstallCommit(DeviceData device, string session)
/// <inheritdoc/>
public IEnumerable<string> GetFeatureSet(DeviceData device)
{
EnsureDevice(device);

using IAdbSocket socket = adbSocketFactory(EndPoint);
socket.SendAdbRequest($"host-serial:{device.Serial}:features");

Expand All @@ -648,15 +652,39 @@ public IEnumerable<string> GetFeatureSet(DeviceData device)
}

/// <inheritdoc/>
public XmlDocument DumpScreen(DeviceData device)
public string DumpScreenString(DeviceData device)
{
XmlDocument doc = new();
EnsureDevice(device);
using IAdbSocket socket = adbSocketFactory(EndPoint);
socket.SetDevice(device);
socket.SendAdbRequest("shell:uiautomator dump /dev/tty");
AdbResponse response = socket.ReadAdbResponse();
using StreamReader reader = new(socket.GetShellStream(), Encoding);
string xmlString = reader.ReadToEnd().Replace("Events injected: 1\r\n", "").Replace("UI hierchary dumped to: /dev/tty", "").Trim();
return xmlString;
}

/// <inheritdoc/>
public XmlDocument DumpScreen(DeviceData device)
{
XmlDocument doc = new();
string xmlString = DumpScreenString(device);
if (!string.IsNullOrEmpty(xmlString)
&& !xmlString.StartsWith("ERROR")
&& !xmlString.StartsWith("java.lang.Exception"))
{
doc.LoadXml(xmlString);
return doc;
}
return null;
}

#if WINDOWS_UWP
/// <inheritdoc/>
public Windows.Data.Xml.Dom.XmlDocument DumpScreenWinRT(DeviceData device)
{
Windows.Data.Xml.Dom.XmlDocument doc = new();
string xmlString = DumpScreenString(device);
if (!string.IsNullOrEmpty(xmlString)
&& !xmlString.StartsWith("ERROR")
&& !xmlString.StartsWith("java.lang.Exception"))
Expand All @@ -666,6 +694,7 @@ public XmlDocument DumpScreen(DeviceData device)
}
return null;
}
#endif

/// <inheritdoc/>
public void Click(DeviceData device, Cords cords)
Expand Down Expand Up @@ -753,6 +782,8 @@ public bool IsAppRunning(DeviceData device, string packageName)
/// <inheritdoc/>
public AppStatus GetAppStatus(DeviceData device, string packageName)
{
EnsureDevice(device);

// Check if the app is in foreground
bool currentApp = IsCurrentApp(device, packageName);
if (currentApp)
Expand All @@ -773,6 +804,7 @@ public AppStatus GetAppStatus(DeviceData device, string packageName)
/// <inheritdoc/>
public Element FindElement(DeviceData device, string xpath, TimeSpan timeout = default)
{
EnsureDevice(device);
Stopwatch stopwatch = new();
stopwatch.Start();
while (timeout == TimeSpan.Zero || stopwatch.Elapsed < timeout)
Expand Down Expand Up @@ -808,6 +840,7 @@ public Element FindElement(DeviceData device, string xpath, TimeSpan timeout = d
/// <inheritdoc/>
public Element[] FindElements(DeviceData device, string xpath, TimeSpan timeout = default)
{
EnsureDevice(device);
Stopwatch stopwatch = new();
stopwatch.Start();
while (timeout == TimeSpan.Zero || stopwatch.Elapsed < timeout)
Expand Down
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient/AdvancedSharpAdbClient.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<FullTargets>False</FullTargets>
<FullTargets>True</FullTargets>
<NoWarn>CA2254</NoWarn>
</PropertyGroup>

Expand Down
21 changes: 20 additions & 1 deletion AdvancedSharpAdbClient/Interfaces/IAdbClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,28 @@ public partial interface IAdbClient
/// </summary>
/// <param name="device">The device for which to get the screen snapshot.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.</param>
/// <returns>An <see cref="Task"/> which return the Xml containing current hierarchy.</returns>
/// <returns>An <see cref="Task"/> which return a <see cref="string"/> containing current hierarchy.
/// Failed if start with <c>ERROR</c> or <c>java.lang.Exception</c>.</returns>
Task<string> DumpScreenStringAsync(DeviceData device, CancellationToken cancellationToken);

/// <summary>
/// Gets the current device screen snapshot asynchronously.
/// </summary>
/// <param name="device">The device for which to get the screen snapshot.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.</param>
/// <returns>An <see cref="Task"/> which return a <see cref="XmlDocument"/> containing current hierarchy.</returns>
Task<XmlDocument> DumpScreenAsync(DeviceData device, CancellationToken cancellationToken);

#if WINDOWS_UWP
/// <summary>
/// Gets the current device screen snapshot asynchronously.
/// </summary>
/// <param name="device">The device for which to get the screen snapshot.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.</param>
/// <returns>An <see cref="Task"/> which return a <see cref="Windows.Data.Xml.Dom.XmlDocument"/> containing current hierarchy.</returns>
Task<Windows.Data.Xml.Dom.XmlDocument> DumpScreenWinRTAsync(DeviceData device, CancellationToken cancellationToken);
#endif

/// <summary>
/// Clicks on the specified coordinates.
/// </summary>
Expand Down
19 changes: 18 additions & 1 deletion AdvancedSharpAdbClient/Interfaces/IAdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,26 @@ public partial interface IAdbClient
/// Gets the current device screen snapshot.
/// </summary>
/// <param name="device">The device for which to get the screen snapshot.</param>
/// <returns>Xml containing current hierarchy.</returns>
/// <returns>A <see cref="string"/> containing current hierarchy.
/// Failed if start with <c>ERROR</c> or <c>java.lang.Exception</c>.</returns>
string DumpScreenString(DeviceData device);

/// <summary>
/// Gets the current device screen snapshot.
/// </summary>
/// <param name="device">The device for which to get the screen snapshot.</param>
/// <returns>A <see cref="XmlDocument"/> containing current hierarchy.</returns>
XmlDocument DumpScreen(DeviceData device);

#if WINDOWS_UWP
/// <summary>
/// Gets the current device screen snapshot.
/// </summary>
/// <param name="device">The device for which to get the screen snapshot.</param>
/// <returns>A <see cref="Windows.Data.Xml.Dom.XmlDocument"/> containing current hierarchy.</returns>
Windows.Data.Xml.Dom.XmlDocument DumpScreenWinRT(DeviceData device);
#endif

/// <summary>
/// Clicks on the specified coordinates.
/// </summary>
Expand Down

0 comments on commit a8163d4

Please sign in to comment.