Skip to content

Commit

Permalink
Fixed #308, Unzipping of files and able to download ChromeDriver 64 b…
Browse files Browse the repository at this point in the history
…it version. (#311)

#308 - Fixed by replacing json library with newtonsoft.json.
Now downloads chromedriver 64 bit version as never downloaded this version only 32 bit.
Fixed unzip issue with dealing with folder names in the zip file.

Updated to .net framework 4.7.2 as 4.6.2 is no longer supported.
  • Loading branch information
garry-edmonds authored Feb 16, 2024
1 parent cbf2537 commit d2e7923
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion WebDriverManager.Tests/WebDriverManager.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<TargetFrameworks>net462;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
10 changes: 3 additions & 7 deletions WebDriverManager/Clients/ChromeForTestingClient.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;
using WebDriverManager.Models.Chrome;

namespace WebDriverManager.Clients
{
public static class ChromeForTestingClient
{
private static readonly string BaseUrl = "https://googlechromelabs.github.io/chrome-for-testing/";
private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};


private static HttpClient _httpClient;

private static HttpClient HttpClient
Expand Down Expand Up @@ -67,7 +63,7 @@ private static TResult GetResultFromHttpTask<TResult>(Task<HttpResponseMessage>
var readStringTask = Task.Run(() => httpTask.Result.Content.ReadAsStringAsync());
readStringTask.Wait();

return JsonSerializer.Deserialize<TResult>(readStringTask.Result, JsonSerializerOptions);
return JsonConvert.DeserializeObject<TResult>(readStringTask.Result);
}
}
}
21 changes: 11 additions & 10 deletions WebDriverManager/DriverConfigs/Impl/ChromeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using WebDriverManager.Clients;
using WebDriverManager.Helpers;
using WebDriverManager.Models.Chrome;
using Architecture = WebDriverManager.Helpers.Architecture;

namespace WebDriverManager.DriverConfigs.Impl
{
Expand Down Expand Up @@ -34,24 +35,24 @@ public virtual string GetName()

public virtual string GetUrl32()
{
return GetUrl();
return GetUrl(Architecture.X32);
}

public virtual string GetUrl64()
{
return GetUrl();
return GetUrl(Architecture.X64);
}

private string GetUrl()
private string GetUrl(Architecture architecture)
{
// Handle newer versions of chrome driver only being available for download via the Chrome for Testing API's
// whilst retaining backwards compatibility for older versions of chrome/chrome driver.
if (_chromeVersionInfo != null)
{
return GetUrlFromChromeForTestingApi();
return GetUrlFromChromeForTestingApi(architecture);
}

return GetUrlFromChromeStorage();
return GetUrlFromChromeStorage(architecture);
}

public virtual string GetBinaryName()
Expand Down Expand Up @@ -131,7 +132,7 @@ private static string GetVersionFromChromeStorage(string url)
/// Retrieves a download URL for a chrome driver from the https://chromedriver.storage.googleapis.com API's
/// </summary>
/// <returns>A chrome driver download URL</returns>
private string GetUrlFromChromeStorage()
private string GetUrlFromChromeStorage(Architecture architecture)
{
#if NETSTANDARD
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
Expand All @@ -155,8 +156,8 @@ private string GetUrlFromChromeStorage()
return $"{BaseVersionPatternUrl}chromedriver_linux64.zip";
}
#endif

return $"{BaseVersionPatternUrl}chromedriver_win32.zip";
var driverName = architecture == Architecture.X32 ? "chromedriver_win32.zip" : "chromedriver_win64.zip";
return $"{BaseVersionPatternUrl}{driverName}";
}

/// <summary>
Expand All @@ -180,9 +181,9 @@ private ChromeVersionInfo GetVersionFromChromeForTestingApi(string noRevisionVer
/// Retrieves a chrome driver download URL from Chrome for Testing API's
/// </summary>
/// <returns>A chrome driver download URL</returns>
private string GetUrlFromChromeForTestingApi()
private string GetUrlFromChromeForTestingApi(Architecture architecture)
{
var platform = "win32";
string platform = architecture == Architecture.X32 ? "win32" : "win64";

#if NETSTANDARD
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
Expand Down
19 changes: 14 additions & 5 deletions WebDriverManager/Services/Impl/BinaryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;

namespace WebDriverManager.Services.Impl
{
Expand Down Expand Up @@ -182,13 +183,21 @@ protected string UnZip(string path, string destination, string name)
return destination;
}

using (var zip = ZipFile.Open(path, ZipArchiveMode.Read))
using (var zip = new ZipFile(path))
{
foreach (var entry in zip.Entries)
foreach (ZipEntry zipEntry in zip)
{
if (entry.Name == name)
if (zipEntry.Name.EndsWith(name) && zipEntry.IsFile)
{
entry.ExtractToFile(destination, true);
byte[] buffer = new byte[4096];
Stream zipStream = zip.GetInputStream(zipEntry);

// Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
// of the file, but does not waste memory.
using (FileStream streamWriter = File.Create(destination))
{
StreamUtils.Copy(zipStream, streamWriter, buffer);
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions WebDriverManager/WebDriverManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net472;netstandard2.0;netstandard2.1</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.17.1</Version>
<Title>WebDriverManager.Net</Title>
Expand All @@ -20,8 +20,8 @@
<ItemGroup>
<PackageReference Include="AngleSharp" Version="1.1.0" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.Text.Json" Version="8.0.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
Expand All @@ -31,4 +31,8 @@
<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Net.Http" />
</ItemGroup>
</Project>

0 comments on commit d2e7923

Please sign in to comment.