Skip to content

Commit

Permalink
[DevTools] Performance monitoring (#216) +semver: feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mialeska authored Feb 28, 2022
1 parent 4e2727c commit c26cddb
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Newtonsoft.Json.Linq;
using OpenQA.Selenium.DevTools.V85.Performance;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;

namespace Aquality.Selenium.Browsers
{
/// <summary>
/// Implementation of version-independent performance DevTools commands as extensions for <see cref="DevToolsHandling"/>.
/// For more information, see <see href="https://chromedevtools.github.io/devtools-protocol/tot/Performance/"/>.
/// </summary>
public static class DevToolsPerformanceExtensions
{
/// <summary>
/// Disable collecting and reporting metrics.
/// </summary>
/// <param name="devTools">Current instance of <see cref="DevToolsHandling"/>.</param>
/// <returns>A task for asynchronous command.</returns>
public static async Task DisablePerfomanceMonitoring(this DevToolsHandling devTools)
{
await devTools.SendCommand(new DisableCommandSettings());
}

/// <summary>
/// Enable collecting and reporting metrics.
/// </summary>
/// <param name="devTools">Current instance of <see cref="DevToolsHandling"/>.</param>
/// <param name="timeDomain">Time domain to use for collecting and reporting duration metrics.
/// Allowed Values: timeTicks, threadTicks. </param>
/// <returns>A task for asynchronous command.</returns>
public static async Task EnablePerfomanceMonitoring(this DevToolsHandling devTools, string timeDomain = null)
{
await devTools.SendCommand(new EnableCommandSettings { TimeDomain = timeDomain });
}

/// <summary>
/// Retrieve current values of run-time metrics.
/// </summary>
/// <param name="devTools">Current instance of <see cref="DevToolsHandling"/>.</param>
/// <returns>A task for asynchronous command with current values for run-time metrics as result.</returns>
public static async Task<IDictionary<string, double>> GetPerformanceMetrics(this DevToolsHandling devTools)
{
JToken result = await devTools.SendCommand(new GetMetricsCommandSettings());
return (result["metrics"] as JArray)
.ToDictionary(item => item["name"].ToString(), item => double.Parse(item["value"].ToString(), CultureInfo.InvariantCulture));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void setAction(long width, long height, bool isMobile, double scaleFactor)

private static void CheckDeviceMetricsOverride(Action<long, long, bool, double> setAction)
{
long getWindowHeight() => AqualityServices.Browser.ExecuteScriptFromFile<long>("Resources.GetWindowSize.js");
static long getWindowHeight() => AqualityServices.Browser.ExecuteScriptFromFile<long>("Resources.GetWindowSize.js");
var initialValue = getWindowHeight();
Assume.That(initialValue, Is.Not.EqualTo(DeviceModeSettingHeight), "To check that override works, initial value should differ from the new one");
setAction(DeviceModeSettingWidth, DeviceModeSettingHeight, DeviceModeSettingMobile, DeviceModeSettingDeviceScaleFactor);
Expand Down Expand Up @@ -174,7 +174,7 @@ public void Should_BePossibleTo_SetScriptExecutionDisabled_AndEnableAgain()
[Test]
public void Should_BePossibleTo_SetTouchEmulationEnabled_AndDisabled()
{
bool isTouchEnabled() => AqualityServices.Browser.ExecuteScriptFromFile<bool>("Resources.IsTouchEnabled.js");
static bool isTouchEnabled() => AqualityServices.Browser.ExecuteScriptFromFile<bool>("Resources.IsTouchEnabled.js");
Assume.That(isTouchEnabled, Is.False, "Touch should be initially disabled");

Assert.DoesNotThrowAsync(() => DevTools.SetTouchEmulationEnabled(true), "Should be possible to enable touch emulation");
Expand All @@ -188,7 +188,8 @@ public void Should_BePossibleTo_SetTouchEmulationEnabled_AndDisabled()
public void Should_BePossibleTo_SetEmulatedMedia()
{
const string emulatedMedia = "projection";
string getMediaType() => AqualityServices.Browser.ExecuteScriptFromFile<string>("Resources.GetMediaType.js");

static string getMediaType() => AqualityServices.Browser.ExecuteScriptFromFile<string>("Resources.GetMediaType.js");
var initialValue = getMediaType();
Assume.That(initialValue, Does.Not.Contain(emulatedMedia), "Initial media type should differ from value to be set");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Aquality.Selenium.Browsers;
using NUnit.Framework;
using OpenQA.Selenium.DevTools.V96.Performance;
using System.Collections.Generic;

namespace Aquality.Selenium.Tests.Integration
{
internal class DevToolsPerformanceTests : UITest
{
private static DevToolsHandling DevTools => AqualityServices.Browser.DevTools;

[Test]
public void Should_BePossibleTo_CollectPerformanceMetrics()
{
Assert.DoesNotThrowAsync(() => DevTools.EnablePerfomanceMonitoring(), "Should be possible to enable performance monitoring");

AqualityServices.Browser.GoTo("http://www.google.com");
IDictionary<string, double> metrics = null;
Assert.DoesNotThrowAsync(async () => metrics = await DevTools.GetPerformanceMetrics(), "Should be possible to get performance metrics");
CollectionAssert.IsNotEmpty(metrics, "Some metrics should be returned");

AqualityServices.Browser.Refresh();
IDictionary<string, double> otherMetrics = DevTools.GetPerformanceMetrics().GetAwaiter().GetResult();
CollectionAssert.AreNotEqual(otherMetrics, metrics, "Some additional metrics should have been collected");

Assert.DoesNotThrowAsync(() => DevTools.DisablePerfomanceMonitoring(), "Should be possible to disable performance monitoring");
AqualityServices.Browser.Refresh();
metrics = DevTools.GetPerformanceMetrics().GetAwaiter().GetResult();
CollectionAssert.IsEmpty(metrics, "Metrics should have not been collected after performance monitoring have been disabled");
}
}
}

0 comments on commit c26cddb

Please sign in to comment.