Skip to content

Commit 3a34d66

Browse files
authored
[dotnet] [bidi] Serialize base64 encoded string directly to bytes (#16203)
1 parent f227da9 commit 3a34d66

File tree

6 files changed

+16
-14
lines changed

6 files changed

+16
-14
lines changed

dotnet/src/webdriver/BiDi/BrowsingContext/CaptureScreenshotCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Communication;
21+
using System;
2122
using System.Text.Json.Serialization;
2223

2324
namespace OpenQA.Selenium.BiDi.BrowsingContext;
@@ -56,9 +57,9 @@ public sealed record BoxClipRectangle(double X, double Y, double Width, double H
5657

5758
public sealed record ElementClipRectangle(Script.ISharedReference Element) : ClipRectangle;
5859

59-
public sealed record CaptureScreenshotResult(string Data) : EmptyResult
60+
public sealed record CaptureScreenshotResult(ReadOnlyMemory<byte> Data) : EmptyResult
6061
{
6162
public static implicit operator byte[](CaptureScreenshotResult captureScreenshotResult) => captureScreenshotResult.ToByteArray();
6263

63-
public byte[] ToByteArray() => System.Convert.FromBase64String(Data);
64+
public byte[] ToByteArray() => Data.ToArray();
6465
}

dotnet/src/webdriver/BiDi/BrowsingContext/PrintCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static implicit operator PrintPageRange(Range range)
112112
#endif
113113
}
114114

115-
public sealed record PrintResult(string Data) : EmptyResult
115+
public sealed record PrintResult(ReadOnlyMemory<byte> Data) : EmptyResult
116116
{
117-
public byte[] ToByteArray() => Convert.FromBase64String(Data);
117+
public byte[] ToByteArray() => Data.ToArray();
118118
}

dotnet/src/webdriver/BiDi/Network/BytesValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ namespace OpenQA.Selenium.BiDi.Network;
2828
public abstract record BytesValue
2929
{
3030
public static implicit operator BytesValue(string value) => new StringBytesValue(value);
31-
public static implicit operator BytesValue(byte[] value) => new Base64BytesValue(Convert.ToBase64String(value));
31+
public static implicit operator BytesValue(byte[] value) => new Base64BytesValue(value);
3232
}
3333

3434
public sealed record StringBytesValue(string Value) : BytesValue;
3535

36-
public sealed record Base64BytesValue(string Value) : BytesValue;
36+
public sealed record Base64BytesValue(ReadOnlyMemory<byte> Value) : BytesValue;

dotnet/src/webdriver/BiDi/WebExtension/InstallCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// </copyright>
1919

2020
using OpenQA.Selenium.BiDi.Communication;
21+
using System;
2122
using System.Text.Json.Serialization;
2223

2324
namespace OpenQA.Selenium.BiDi.WebExtension;
@@ -35,7 +36,7 @@ public abstract record ExtensionData;
3536

3637
public sealed record ExtensionArchivePath(string Path) : ExtensionData;
3738

38-
public sealed record ExtensionBase64Encoded(string Value) : ExtensionData;
39+
public sealed record ExtensionBase64Encoded(ReadOnlyMemory<byte> Value) : ExtensionData;
3940

4041
public sealed record ExtensionPath(string Path) : ExtensionData;
4142

dotnet/test/common/BiDi/BrowsingContext/BrowsingContextTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public async Task CanCaptureScreenshot()
250250
var screenshot = await context.CaptureScreenshotAsync();
251251

252252
Assert.That(screenshot, Is.Not.Null);
253-
Assert.That(screenshot.Data, Is.Not.Empty);
253+
Assert.That(screenshot.Data.Length, Is.Not.Zero);
254254
}
255255

256256
[Test]
@@ -263,7 +263,7 @@ public async Task CanCaptureScreenshotWithParameters()
263263
});
264264

265265
Assert.That(screenshot, Is.Not.Null);
266-
Assert.That(screenshot.Data, Is.Not.Empty);
266+
Assert.That(screenshot.Data.Length, Is.Not.Zero);
267267
}
268268

269269
[Test]
@@ -276,7 +276,7 @@ public async Task CanCaptureScreenshotOfViewport()
276276
});
277277

278278
Assert.That(screenshot, Is.Not.Null);
279-
Assert.That(screenshot.Data, Is.Not.Empty);
279+
Assert.That(screenshot.Data.Length, Is.Not.Zero);
280280
}
281281

282282
[Test]
@@ -292,7 +292,7 @@ public async Task CanCaptureScreenshotOfElement()
292292
});
293293

294294
Assert.That(screenshot, Is.Not.Null);
295-
Assert.That(screenshot.Data, Is.Not.Empty);
295+
Assert.That(screenshot.Data.Length, Is.Not.Zero);
296296
}
297297

298298
[Test]
@@ -313,6 +313,6 @@ public async Task CanPrintPage()
313313
var pdf = await context.PrintAsync();
314314

315315
Assert.That(pdf, Is.Not.Null);
316-
Assert.That(pdf.Data, Is.Not.Empty);
316+
Assert.That(pdf.Data.Length, Is.Not.Zero);
317317
}
318318
}

dotnet/test/common/BiDi/WebExtension/WebExtensionTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public async Task CanInstallBase64WebExtension()
6464
{
6565
var path = LocateRelativePath("common/extensions/webextensions-selenium-example.zip");
6666

67-
string base64 = Convert.ToBase64String(File.ReadAllBytes(path));
67+
var bytes = File.ReadAllBytes(path);
6868

69-
var result = await bidi.WebExtension.InstallAsync(new ExtensionBase64Encoded(base64));
69+
var result = await bidi.WebExtension.InstallAsync(new ExtensionBase64Encoded(bytes));
7070

7171
Assert.That(result, Is.Not.Null);
7272
Assert.That(result.Extension, Is.Not.Null);

0 commit comments

Comments
 (0)