Skip to content

Commit

Permalink
[dotnet] Remove System.Drawing.Common as package dependency (#12781)
Browse files Browse the repository at this point in the history
* Remove Microsoft.IdentityModel.Tokens as dependency

* Make it internal

* Remove System.Drawing.Common as dependency

* Move smoother, throw exception only for those who really uses image encoding/decoding

* Remove obsolete methods to save screenshots with encoding
  • Loading branch information
nvborisenko authored Nov 10, 2023
1 parent b0497bf commit 53874e6
Show file tree
Hide file tree
Showing 8 changed files with 2 additions and 109 deletions.
2 changes: 0 additions & 2 deletions dotnet/src/webdriver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ csharp_library(
deps = [
framework("nuget", "NETStandard.Library"),
framework("nuget", "Newtonsoft.Json"),
framework("nuget", "System.Drawing.Common"),
],
)

Expand Down Expand Up @@ -84,7 +83,6 @@ csharp_library(
deps = [
framework("nuget", "NETStandard.Library"),
framework("nuget", "Newtonsoft.Json"),
framework("nuget", "System.Drawing.Common"),
],
)

Expand Down
98 changes: 1 addition & 97 deletions dotnet/src/webdriver/Screenshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,10 @@
// </copyright>

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace OpenQA.Selenium
{
/// <summary>
/// File format for saving screenshots.
/// </summary>
[Obsolete("Support for decoding/encoding between different image formats is deprecated")]
public enum ScreenshotImageFormat
{
/// <summary>
/// W3C Portable Network Graphics image format.
/// </summary>
Png,

/// <summary>
/// Joint Photgraphic Experts Group image format.
/// </summary>
Jpeg,

/// <summary>
/// Graphics Interchange Format image format.
/// </summary>
Gif,

/// <summary>
/// Tagged Image File Format image format.
/// </summary>
Tiff,

/// <summary>
/// Bitmap image format.
/// </summary>
Bmp
}

/// <summary>
/// Represents an image of the page currently loaded in the browser.
/// </summary>
Expand All @@ -76,69 +42,7 @@ public Screenshot(string base64EncodedScreenshot) : base(base64EncodedScreenshot
/// <param name="fileName">The full path and file name to save the screenshot to.</param>
public override void SaveAsFile(string fileName)
{
this.SaveAsFile(fileName, ScreenshotImageFormat.Png);
}

/// <summary>
/// Saves the screenshot to a file, overwriting the file if it already exists.
/// </summary>
/// <param name="fileName">The full path and file name to save the screenshot to.</param>
/// <param name="format">A <see cref="ScreenshotImageFormat"/> value indicating the format
/// to save the image to.</param>
[Obsolete("Support for decoding/encoding between different image formats is deprecated; use SaveAsFile(string fileName) method instead")]
public void SaveAsFile(string fileName, ScreenshotImageFormat format)
{
using (MemoryStream imageStream = new MemoryStream(this.AsByteArray))
{
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
{
// Optimization: The byte array is already a PNG, so we can just
// write directly to the file. If the user wants to convert to
// another image format, we'll allow them to do so, but on certain
// framework versions (.NET 6 or above) this is likely to fail at
// runtime. It is unclear how many Selenium users are using this
// feature to convert the returned screenshot into a different image
// format. Future mitigations of this issue would need to take a
// dependency on an image processing library like ImageSharp or
// similar.
if (format == ScreenshotImageFormat.Png)
{
imageStream.WriteTo(fileStream);
}
else
{
using (Image screenshotImage = Image.FromStream(imageStream))
{
screenshotImage.Save(fileStream, ConvertScreenshotImageFormat(format));
}
}
}
}
}

private static ImageFormat ConvertScreenshotImageFormat(ScreenshotImageFormat format)
{
ImageFormat returnedFormat = ImageFormat.Png;
switch (format)
{
case ScreenshotImageFormat.Jpeg:
returnedFormat = ImageFormat.Jpeg;
break;

case ScreenshotImageFormat.Gif:
returnedFormat = ImageFormat.Gif;
break;

case ScreenshotImageFormat.Bmp:
returnedFormat = ImageFormat.Bmp;
break;

case ScreenshotImageFormat.Tiff:
returnedFormat = ImageFormat.Tiff;
break;
}

return returnedFormat;
File.WriteAllBytes(fileName, this.AsByteArray);
}
}
}
1 change: 0 additions & 1 deletion dotnet/src/webdriver/WebDriver.StrongNamed.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<dependencies>
<group targetFramework="netstandard2.0">
<dependency id="Newtonsoft.Json" version="13.0.1" exclude="Build,Analyzers" />
<dependency id="System.Drawing.Common" version="7.0.0" exclude="Build,Analyzers" />
</group>
</dependencies>
<frameworkAssemblies>
Expand Down
1 change: 0 additions & 1 deletion dotnet/src/webdriver/WebDriver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion dotnet/src/webdriver/WebDriver.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<dependencies>
<group targetFramework="netstandard2.0">
<dependency id="Newtonsoft.Json" version="13.0.1" exclude="Build,Analyzers" />
<dependency id="System.Drawing.Common" version="7.0.0" exclude="Build,Analyzers" />
</group>
</dependencies>
<frameworkAssemblies>
Expand Down
2 changes: 1 addition & 1 deletion dotnet/test/common/TakesScreenshotTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void GetScreenshotAsFile()

string filename = Path.Combine(Path.GetTempPath(), "snapshot" + new Random().Next().ToString() + ".png");
Screenshot screenImage = screenshotCapableDriver.GetScreenshot();
screenImage.SaveAsFile(filename, ScreenshotImageFormat.Png);
screenImage.SaveAsFile(filename);
Assert.That(File.Exists(filename), Is.True);
Assert.That(new FileInfo(filename).Length, Is.GreaterThan(0));
File.Delete(filename);
Expand Down
6 changes: 0 additions & 6 deletions dotnet/workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ def selenium_register_dotnet():
sha256 = "2b6b52556e27e1b7913f33eedeb95568110c746bd64afff74357f1683878323a",
)

import_nuget_package(
name = "system.drawing.common",
file = "third_party/dotnet/nuget/packages/system.drawing.common.7.0.0.nupkg",
sha256 = "b78141813321b8e039140db77e0d1640e1ae1f49e357b8495d24ff2e7fd99e4b",
)

import_nuget_package(
name = "moq",
file = "third_party/dotnet/nuget/packages/moq.4.12.0.nupkg",
Expand Down
Binary file not shown.

0 comments on commit 53874e6

Please sign in to comment.