Skip to content

Commit c26cddb

Browse files
authored
[DevTools] Performance monitoring (#216) +semver: feature
1 parent 4e2727c commit c26cddb

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

Aquality.Selenium/src/Aquality.Selenium/Aquality.Selenium.xml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Newtonsoft.Json.Linq;
2+
using OpenQA.Selenium.DevTools.V85.Performance;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace Aquality.Selenium.Browsers
9+
{
10+
/// <summary>
11+
/// Implementation of version-independent performance DevTools commands as extensions for <see cref="DevToolsHandling"/>.
12+
/// For more information, see <see href="https://chromedevtools.github.io/devtools-protocol/tot/Performance/"/>.
13+
/// </summary>
14+
public static class DevToolsPerformanceExtensions
15+
{
16+
/// <summary>
17+
/// Disable collecting and reporting metrics.
18+
/// </summary>
19+
/// <param name="devTools">Current instance of <see cref="DevToolsHandling"/>.</param>
20+
/// <returns>A task for asynchronous command.</returns>
21+
public static async Task DisablePerfomanceMonitoring(this DevToolsHandling devTools)
22+
{
23+
await devTools.SendCommand(new DisableCommandSettings());
24+
}
25+
26+
/// <summary>
27+
/// Enable collecting and reporting metrics.
28+
/// </summary>
29+
/// <param name="devTools">Current instance of <see cref="DevToolsHandling"/>.</param>
30+
/// <param name="timeDomain">Time domain to use for collecting and reporting duration metrics.
31+
/// Allowed Values: timeTicks, threadTicks. </param>
32+
/// <returns>A task for asynchronous command.</returns>
33+
public static async Task EnablePerfomanceMonitoring(this DevToolsHandling devTools, string timeDomain = null)
34+
{
35+
await devTools.SendCommand(new EnableCommandSettings { TimeDomain = timeDomain });
36+
}
37+
38+
/// <summary>
39+
/// Retrieve current values of run-time metrics.
40+
/// </summary>
41+
/// <param name="devTools">Current instance of <see cref="DevToolsHandling"/>.</param>
42+
/// <returns>A task for asynchronous command with current values for run-time metrics as result.</returns>
43+
public static async Task<IDictionary<string, double>> GetPerformanceMetrics(this DevToolsHandling devTools)
44+
{
45+
JToken result = await devTools.SendCommand(new GetMetricsCommandSettings());
46+
return (result["metrics"] as JArray)
47+
.ToDictionary(item => item["name"].ToString(), item => double.Parse(item["value"].ToString(), CultureInfo.InvariantCulture));
48+
}
49+
}
50+
}

Aquality.Selenium/tests/Aquality.Selenium.Tests/Integration/DevToolsEmulationTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void setAction(long width, long height, bool isMobile, double scaleFactor)
7878

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

180180
Assert.DoesNotThrowAsync(() => DevTools.SetTouchEmulationEnabled(true), "Should be possible to enable touch emulation");
@@ -188,7 +188,8 @@ public void Should_BePossibleTo_SetTouchEmulationEnabled_AndDisabled()
188188
public void Should_BePossibleTo_SetEmulatedMedia()
189189
{
190190
const string emulatedMedia = "projection";
191-
string getMediaType() => AqualityServices.Browser.ExecuteScriptFromFile<string>("Resources.GetMediaType.js");
191+
192+
static string getMediaType() => AqualityServices.Browser.ExecuteScriptFromFile<string>("Resources.GetMediaType.js");
192193
var initialValue = getMediaType();
193194
Assume.That(initialValue, Does.Not.Contain(emulatedMedia), "Initial media type should differ from value to be set");
194195

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Aquality.Selenium.Browsers;
2+
using NUnit.Framework;
3+
using OpenQA.Selenium.DevTools.V96.Performance;
4+
using System.Collections.Generic;
5+
6+
namespace Aquality.Selenium.Tests.Integration
7+
{
8+
internal class DevToolsPerformanceTests : UITest
9+
{
10+
private static DevToolsHandling DevTools => AqualityServices.Browser.DevTools;
11+
12+
[Test]
13+
public void Should_BePossibleTo_CollectPerformanceMetrics()
14+
{
15+
Assert.DoesNotThrowAsync(() => DevTools.EnablePerfomanceMonitoring(), "Should be possible to enable performance monitoring");
16+
17+
AqualityServices.Browser.GoTo("http://www.google.com");
18+
IDictionary<string, double> metrics = null;
19+
Assert.DoesNotThrowAsync(async () => metrics = await DevTools.GetPerformanceMetrics(), "Should be possible to get performance metrics");
20+
CollectionAssert.IsNotEmpty(metrics, "Some metrics should be returned");
21+
22+
AqualityServices.Browser.Refresh();
23+
IDictionary<string, double> otherMetrics = DevTools.GetPerformanceMetrics().GetAwaiter().GetResult();
24+
CollectionAssert.AreNotEqual(otherMetrics, metrics, "Some additional metrics should have been collected");
25+
26+
Assert.DoesNotThrowAsync(() => DevTools.DisablePerfomanceMonitoring(), "Should be possible to disable performance monitoring");
27+
AqualityServices.Browser.Refresh();
28+
metrics = DevTools.GetPerformanceMetrics().GetAwaiter().GetResult();
29+
CollectionAssert.IsEmpty(metrics, "Metrics should have not been collected after performance monitoring have been disabled");
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)