Skip to content

Commit

Permalink
Implement element screenshot (#2376)
Browse files Browse the repository at this point in the history
* small progress

* More progress

* Remove unused file

* undo a change

* Fix xml comment

* it's javascript
  • Loading branch information
kblok authored Dec 13, 2023
1 parent 13d6704 commit 396eaf5
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 161 deletions.
32 changes: 14 additions & 18 deletions lib/PuppeteerSharp.DevicesFetcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@
using Newtonsoft.Json;
namespace PuppeteerSharp.DevicesFetcher
{
class Program
static class Program
{
const string DEVICES_URL = "https://raw.githubusercontent.com/puppeteer/puppeteer/master/src/common/DeviceDescriptors.ts";

static readonly string deviceDescriptorsOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptors.cs";
static readonly string deviceDescriptorNameOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptorName.cs";
private const string DevicesURL = "https://raw.githubusercontent.com/puppeteer/puppeteer/master/src/common/DeviceDescriptors.ts";
private const string DeviceDescriptorsOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptors.cs";
private const string DeviceDescriptorNameOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptorName.cs";

static async Task Main(string[] args)
{
var url = DEVICES_URL;
var url = DevicesURL;
if (args.Length > 0)
{
url = args[0];
}

Console.WriteLine($"GET {url}");
var text = await HttpGET(url).ConfigureAwait(false);
var text = await HttpGet(url).ConfigureAwait(false);

const string DeviceArray = "Device[] = [";
var startIndex = text.IndexOf(DeviceArray) + DeviceArray.Length;
var endIndex = text.IndexOf("];", startIndex);
const string deviceArray = "Device[] = [";
var startIndex = text.IndexOf(deviceArray, StringComparison.Ordinal) + deviceArray.Length;
var endIndex = text.IndexOf("];", startIndex, StringComparison.Ordinal);
var length = endIndex - startIndex;
text = "[" + text.Substring(startIndex, length) + "]";

Expand Down Expand Up @@ -86,7 +85,7 @@ public static class DeviceDescriptors
builder.AppendJoin(",\n", devices.Select(GenerateCsharpFromDevice));
builder.Append(end);

File.WriteAllText(deviceDescriptorsOutput, builder.ToString());
File.WriteAllText(DeviceDescriptorsOutput, builder.ToString());
}

static void WriteDeviceDescriptorName(IEnumerable<Device> devices)
Expand All @@ -104,17 +103,14 @@ public enum DeviceDescriptorName
}";

builder.Append(begin);
builder.AppendJoin(",", devices.Select(device =>
{
return $@"
builder.AppendJoin(",", devices.Select(device => $@"
/// <summary>
/// {device.Name}
/// </summary>
{DeviceNameToEnumValue(device)}";
}));
{DeviceNameToEnumValue(device)}"));
builder.Append(end);

File.WriteAllText(deviceDescriptorNameOutput, builder.ToString());
File.WriteAllText(DeviceDescriptorNameOutput, builder.ToString());
}

static string GenerateCsharpFromDevice(Device device)
Expand Down Expand Up @@ -156,7 +152,7 @@ static string DeviceNameToEnumValue(Device device)
return output.ToString();
}

static async Task<string> HttpGET(string url)
private static async Task<string> HttpGet(string url)
{
using var httpClient = new HttpClient();
return await httpClient.GetStringAsync(url).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ namespace PuppeteerSharp.Tests.QuerySelectorTests
{
public class ElementHandleQuerySelectorAllEvalTests : PuppeteerPageBaseTest
{
public ElementHandleQuerySelectorAllEvalTests(): base()
{
}

[PuppeteerTest("queryselector.spec.ts", "ElementHandle.$$eval", "should work")]
[PuppeteerTimeout]
public async Task ShouldWork()
Expand Down
15 changes: 7 additions & 8 deletions lib/PuppeteerSharp/BrowserFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public async Task<InstalledBrowser> DownloadAsync(string buildId)
var url = _downloadsUrl[Browser](Platform, buildId, BaseUrl);
var fileName = url.Split('/').Last();
var cache = new Cache(CacheDir);
var browserRoot = cache.GetBrowserRoot(Browser);
var archivePath = Path.Combine(CacheDir, fileName);
var downloadFolder = new DirectoryInfo(CacheDir);

Expand Down Expand Up @@ -287,7 +286,7 @@ private Task InstallDMGAsync(string dmgPath, string folderPath)
process.StartInfo.Arguments = $"attach -nobrowse -noautoopen \"{dmgPath}\"";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.OutputDataReceived += (sender, e) =>
process.OutputDataReceived += (_, e) =>
{
if (e.Data == null || mountAndCopyTcs.Task.IsCompleted)
{
Expand All @@ -310,11 +309,11 @@ private Task InstallDMGAsync(string dmgPath, string folderPath)
return;
}

using var process = new Process();
process.StartInfo.FileName = "cp";
process.StartInfo.Arguments = $"-R \"{appFile.FullName}\" \"{folderPath}\"";
process.Start();
process.WaitForExit();
using var copyProcess = new Process();
copyProcess.StartInfo.FileName = "cp";
copyProcess.StartInfo.Arguments = $"-R \"{appFile.FullName}\" \"{folderPath}\"";
copyProcess.Start();
copyProcess.WaitForExit();
mountAndCopyTcs.TrySetResult(true);
};

Expand Down Expand Up @@ -395,7 +394,7 @@ private async Task UnpackArchiveAsync(string archivePath, string outputPath, str

if (GetCurrentPlatform() == Platform.Linux)
{
var executables = new string[]
var executables = new[]
{
"chrome",
"chrome_crashpad_handler",
Expand Down
Loading

0 comments on commit 396eaf5

Please sign in to comment.