Skip to content
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

feat: Get Performance Data & Performance Data Types #422

Merged
merged 8 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Appium.Net/Appium/Android/AndroidCommandExecutionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
//See the License for the specific language governing permissions and
//limitations under the License.

using System;
using OpenQA.Selenium.Appium.Interfaces;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

namespace OpenQA.Selenium.Appium.Android
{
Expand Down Expand Up @@ -108,6 +110,23 @@ public static string EndTestCoverage(IExecuteMethod executeMethod, string intent
public static void OpenNotifications(IExecuteMethod executeMethod) =>
executeMethod.Execute(AppiumDriverCommand.OpenNotifications);

#region Device Performance

public static object[] GetPerformanceDataTypes(IExecuteMethod executeMethod) =>
executeMethod.Execute(AppiumDriverCommand.GetPerformanceDataTypes).Value as object[];

public static object[] GetPerformanceData(IExecuteMethod executeMethod, string packageName,
string dataType) => executeMethod.Execute(AppiumDriverCommand.GetPerformanceData,
PrepareArguments(new[] {"packageName", "dataType"},
new object[] {packageName, dataType})).Value as object[];

public static object[] GetPerformanceData(IExecuteMethod executeMethod, string packageName,
string dataType, int dataReadTimeout) => executeMethod.Execute(AppiumDriverCommand.GetPerformanceData,
PrepareArguments(new[] {"packageName", "dataType", "dataReadTimeout"},
new object[] {packageName, dataType, dataReadTimeout})).Value as object[];

#endregion

public static bool IsLocked(IExecuteMethod executeMethod) =>
(bool) executeMethod.Execute(AppiumDriverCommand.IsLocked).Value;

Expand Down
27 changes: 24 additions & 3 deletions src/Appium.Net/Appium/Android/AndroidDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
using OpenQA.Selenium.Appium.Service;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenQA.Selenium.Appium.Android.Enums;

namespace OpenQA.Selenium.Appium.Android
{
public class AndroidDriver<W> : AppiumDriver<W>, IFindByAndroidUIAutomator<W>, IFindByAndroidDataMatcher<W>, IStartsActivity,
IHasNetworkConnection, IHasClipboard,
IHasNetworkConnection, IHasClipboard, IHasPerformanceData,
ISendsKeyEvents,
IPushesFiles, IHasSettings where W : IWebElement
{
Expand Down Expand Up @@ -213,7 +213,28 @@ public string EndTestCoverage(string intent, string path) =>

protected override RemoteWebElementFactory CreateElementFactory() => new AndroidElementFactory(this);

#region locking
#region Device Performance Data

public IList<object> GetPerformanceData(string packageName, string performanceDataType) =>
AndroidCommandExecutionHelper.GetPerformanceData(this, packageName, performanceDataType)
?.ToList();

public IList<object> GetPerformanceData(string packageName, string performanceDataType,
int dataReadAttempts)
{
if (dataReadAttempts < 1) throw new ArgumentException($"{nameof(dataReadAttempts)} must be greater than 0");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return AndroidCommandExecutionHelper.GetPerformanceData(this, packageName, performanceDataType, dataReadAttempts)
?.ToList();
}

public IList<string> GetPerformanceDataTypes() =>
AndroidCommandExecutionHelper.GetPerformanceDataTypes(this)
?.Cast<string>()
.ToList();

#endregion

#region Device Interactions

/**
* This method locks a device.
Expand Down
10 changes: 10 additions & 0 deletions src/Appium.Net/Appium/Android/Enums/PerformanceDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace OpenQA.Selenium.Appium.Android.Enums
{
public struct PerformanceDataType
{
public static string CpuInfo = "cpuinfo";
public static string MemoryInfo = "memoryinfo";
public static string BatteryInfo = "batteryinfo";
public static string NetworkInfo = "networkinfo";
}
}
85 changes: 85 additions & 0 deletions src/Appium.Net/Appium/Android/Interfaces/IHasPerformanceData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//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.

using System.Collections.Generic;
using OpenQA.Selenium.Appium.Android.Enums;

namespace OpenQA.Selenium.Appium.Android.Interfaces
{
public interface IHasPerformanceData
{
/// <summary>
/// Returns the information of the system state which is supported to read e.g. cpu, memory, network
/// </summary>
/// <param name="packageName">The package name of the application</param>
/// <param name="performanceDataType">The type of system state which you want to read, use <see cref="PerformanceDataType"/>
/// or call <see cref="GetPerformanceDataTypes"/> to find a list of the supported performance data types.</param>
/// <returns>
/// A table like list of the performance data, The first index of the table represents the type of data.
/// The remaining index represent the values of the data.
/// in case of battery info : [[power], [23]]
/// in case of memory info :
/// [[totalPrivateDirty, nativePrivateDirty, dalvikPrivateDirty, eglPrivateDirty, glPrivateDirty,
/// totalPss, nativePss, dalvikPss, eglPss, glPss, nativeHeapAllocatedSize, nativeHeapSize],
/// [18360, 8296, 6132, null, null, 42588, 8406, 7024, null, null, 26519, 10344]]
/// in case of network info :
/// [[bucketStart, activeTime, rxBytes, rxPackets, txBytes, txPackets, operations, bucketDuration,],
/// [1478091600000, null, 1099075, 610947, 928, 114362, 769, 0, 3600000],
/// [1478095200000, null, 1306300, 405997, 509, 46359, 370, 0, 3600000]]
/// in case of network info :
/// [[st, activeTime, rb, rp, tb, tp, op, bucketDuration],
/// [1478088000, null, null, 32115296, 34291, 2956805, 25705, 0, 3600],
/// [1478091600, null, null, 2714683, 11821, 1420564, 12650, 0, 3600],
/// [1478095200, null, null, 10079213, 19962, 2487705, 20015, 0, 3600],
/// [1478098800, null, null, 4444433, 10227, 1430356, 10493, 0, 3600]]
/// in case of cpu info : [[user, kernel], [0.9, 1.3]]
/// </returns>
IList<object> GetPerformanceData(string packageName, string performanceDataType);


/// <summary>
/// Returns the information of the system state which is supported to read e.g. cpu, memory, network
/// </summary>
/// <param name="packageName">The package name of the application</param>
/// <param name="performanceDataType">The type of system state which you want to read, use <see cref="PerformanceDataType"/>
/// or call <see cref="GetPerformanceDataTypes"/> to find a list of the supported performance data types.</param>
/// <param name="dataReadAttempts">The number of attempts to read data in the event of a data read failure (optional). Value must be greater than 0.</param>
/// <returns>
/// A table like list of the performance data, The first index of the table represents the type of data.
/// The remaining index represent the values of the data.
/// in case of battery info : [[power], [23]]
/// in case of memory info :
/// [[totalPrivateDirty, nativePrivateDirty, dalvikPrivateDirty, eglPrivateDirty, glPrivateDirty,
/// totalPss, nativePss, dalvikPss, eglPss, glPss, nativeHeapAllocatedSize, nativeHeapSize],
/// [18360, 8296, 6132, null, null, 42588, 8406, 7024, null, null, 26519, 10344]]
/// in case of network info :
/// [[bucketStart, activeTime, rxBytes, rxPackets, txBytes, txPackets, operations, bucketDuration,],
/// [1478091600000, null, 1099075, 610947, 928, 114362, 769, 0, 3600000],
/// [1478095200000, null, 1306300, 405997, 509, 46359, 370, 0, 3600000]]
/// in case of network info :
/// [[st, activeTime, rb, rp, tb, tp, op, bucketDuration],
/// [1478088000, null, null, 32115296, 34291, 2956805, 25705, 0, 3600],
/// [1478091600, null, null, 2714683, 11821, 1420564, 12650, 0, 3600],
/// [1478095200, null, null, 10079213, 19962, 2487705, 20015, 0, 3600],
/// [1478098800, null, null, 4444433, 10227, 1430356, 10493, 0, 3600]]
/// in case of cpu info : [[user, kernel], [0.9, 1.3]]
/// </returns>
IList<object> GetPerformanceData(string packageName, string performanceDataType, int dataReadAttempts);

/// <summary>
/// Returns the information types of the system state which is supported to read as like cpu, memory, network traffic, and battery
/// </summary>
IList<string> GetPerformanceDataTypes();
}
}
4 changes: 4 additions & 0 deletions src/Appium.Net/Appium/AppiumCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public class AppiumCommand
"/session/{sessionId}/appium/device/toggle_wifi"),
new AppiumCommand(CommandInfo.PostCommand, AppiumDriverCommand.ToggleLocationServices,
"/session/{sessionId}/appium/device/toggle_location_services"),
new AppiumCommand(CommandInfo.PostCommand, AppiumDriverCommand.GetPerformanceData,
"/session/{sessionId}/appium/getPerformanceData"),
new AppiumCommand(CommandInfo.PostCommand, AppiumDriverCommand.GetPerformanceDataTypes,
"/session/{sessionId}/appium/performanceData/types"),
new AppiumCommand(CommandInfo.PostCommand, AppiumDriverCommand.LaunchApp,
"/session/{sessionId}/appium/app/launch"),
new AppiumCommand(CommandInfo.PostCommand, AppiumDriverCommand.CloseApp,
Expand Down
6 changes: 5 additions & 1 deletion src/Appium.Net/Appium/AppiumDriverCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class AppiumDriverCommand
/// </summary>
public const string ToggleAirplaneMode = "toggleAirplaneMode";


/// <summary>
/// Toggle Network Data Command
/// </summary>
Expand Down Expand Up @@ -195,6 +194,10 @@ public class AppiumDriverCommand

public const string SetClipboard = "setClipboard";

public static string GetPerformanceData = "getPerformanceData";

public static string GetPerformanceDataTypes = "getSuppportedPerformanceDataTypes";

#endregion Appium Specific extensions to JSONWP Commands

#region TouchActions
Expand Down Expand Up @@ -309,6 +312,7 @@ public class AppiumDriverCommand
/// <summary>
/// Represents the fingerPrint command
public const string FingerPrint = "fingerPrint";

/// </summary>

public const string ReplaceValue = "replaceValue";
Expand Down
60 changes: 60 additions & 0 deletions test/integration/Android/Device/PerformanceDataTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using Appium.Net.Integration.Tests.helpers;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Android.Enums;

namespace Appium.Net.Integration.Tests.Android.Device
{
internal class PerformanceDataTests
{
private AppiumDriver<IWebElement> _driver;
private AppiumOptions _androidOptions;


[OneTimeSetUp]
public void SetUp()
{
_androidOptions = Caps.GetAndroidUIAutomatorCaps(Apps.Get(Apps.androidApiDemos));
_driver = new AndroidDriver<IWebElement>(
Env.ServerIsLocal() ? AppiumServers.LocalServiceUri : AppiumServers.RemoteServerUri,
_androidOptions);
_driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
}

[OneTimeTearDown]
public void TearDown()
{
_driver.Dispose();
}

[Test]
public void GetPerformanceDataTypesTest()
{
var androidDriver = _driver as AndroidDriver<IWebElement>;
Assert.IsNotNull(androidDriver.GetPerformanceDataTypes());
}

[Test]
public void GetPerformanceDataTest()
{
var androidDriver = _driver as AndroidDriver<IWebElement>;
var packageName = androidDriver?.CurrentPackage;

Assert.Multiple(() =>
{
Assert.That(androidDriver?.GetPerformanceData(packageName, PerformanceDataType.CpuInfo), Is.Not.Null.Or.Empty);
Assert.That(androidDriver?.GetPerformanceData(packageName, PerformanceDataType.CpuInfo, 15),
Is.Not.Null.Or.Empty);
Assert.That(androidDriver?.GetPerformanceData(packageName, PerformanceDataType.MemoryInfo, 5),
Is.Not.Null.Or.Empty);
Assert.That(androidDriver?.GetPerformanceData(packageName, PerformanceDataType.BatteryInfo, 5),
Is.Not.Null.Or.Empty);
Assert.That(androidDriver?.GetPerformanceData(packageName, PerformanceDataType.NetworkInfo, 5),
Is.Not.Null.Or.Empty);
});
}
}
}