Skip to content

Commit

Permalink
Split out fixtures tests (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Mar 17, 2019
1 parent b3cb17d commit 26af946
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 133 deletions.
11 changes: 5 additions & 6 deletions lib/PuppeteerSharp.Tests.DumpIO/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ public static async Task Main(string[] args)
{
Headless = true,
DumpIO = true,
ExecutablePath = args[1]
ExecutablePath = args[0]
};

using (var browser = await Puppeteer.LaunchAsync(options))
using (var page = await browser.NewPageAsync())
{
await page.EvaluateFunctionAsync("_dumpioTextToLog => console.log(_dumpioTextToLog)", args[0]);
}
var browser = await Puppeteer.LaunchAsync(options);
var page = await browser.NewPageAsync();
await page.CloseAsync();
await browser.CloseAsync();
}
}
}
146 changes: 146 additions & 0 deletions lib/PuppeteerSharp.Tests/PuppeteerTests/FixturesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using PuppeteerSharp.Helpers;
using PuppeteerSharp.Transport;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.PuppeteerTests
{
[Collection("PuppeteerLoaderFixture collection")]
public class FixturesTests : PuppeteerBaseTest
{
public FixturesTests(ITestOutputHelper output) : base(output) { }

[Fact]
public void ShouldDumpBrowserProcessStderr()
{
var success = false;
var process = GetTestAppProcess(
"PuppeteerSharp.Tests.DumpIO",
$"\"{new BrowserFetcher().RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath}\"");

process.ErrorDataReceived += (sender, e) =>
{
success |= e.Data != null && e.Data.Contains("DevTools listening on ws://");
};

process.Start();
process.BeginErrorReadLine();
process.WaitForExit();
Assert.True(success);
}

[Fact]
public async Task ShouldCloseTheBrowserWhenTheConnectedProcessCloses()
{
var browserClosedTaskWrapper = new TaskCompletionSource<bool>();
var chromiumProcess = new ChromiumProcess(
new BrowserFetcher().RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath,
new LaunchOptions { Headless = true },
TestConstants.LoggerFactory);

await chromiumProcess.StartAsync().ConfigureAwait(false);

var browser = await Puppeteer.ConnectAsync(new ConnectOptions
{
BrowserWSEndpoint = chromiumProcess.EndPoint
});

browser.Disconnected += (sender, e) =>
{
browserClosedTaskWrapper.SetResult(true);
};

KillProcess(chromiumProcess.Process.Id);

await browserClosedTaskWrapper.Task;
Assert.True(browser.IsClosed);
}

[Fact]
public async Task ShouldCloseTheBrowserWhenTheLaunchedProcessCloses()
{
var browserClosedTaskWrapper = new TaskCompletionSource<bool>();
var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }, TestConstants.LoggerFactory);

browser.Disconnected += (sender, e) =>
{
browserClosedTaskWrapper.SetResult(true);
};

KillProcess(browser.ChromiumProcess.Process.Id);

await browserClosedTaskWrapper.Task;
Assert.True(browser.IsClosed);
}

private void KillProcess(int pid)
{
var process = new Process();

//We need to kill the process tree manually
//See: https://github.com/dotnet/corefx/issues/26234
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
process.StartInfo.FileName = "taskkill";
process.StartInfo.Arguments = $"-pid {pid} -t -f";
}
else
{
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = $"-c \"kill -s 9 {pid}\"";
}

process.Start();
process.WaitForExit();
}

private Process GetTestAppProcess(string appName, string arguments)
{
var process = new Process();

#if NETCOREAPP
process.StartInfo.WorkingDirectory = GetSubprocessWorkingDir(appName);
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = $"{appName}.dll {arguments}";
#else
process.StartInfo.FileName = Path.Combine(GetSubprocessWorkingDir(appName), $"{appName}.exe");
process.StartInfo.Arguments = arguments;
#endif
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
return process;
}

private string GetSubprocessWorkingDir(string dir)
{
#if DEBUG
var build = "Debug";
#else

var build = "Release";
#endif
#if NETCOREAPP
return Path.Combine(
TestUtils.FindParentDirectory("lib"),
dir,
"bin",
build,
"netcoreapp2.2");
#else
return Path.Combine(
TestUtils.FindParentDirectory("lib"),
dir,
"bin",
build,
"net471");
#endif
}
}
}
127 changes: 0 additions & 127 deletions lib/PuppeteerSharp.Tests/PuppeteerTests/PuppeteerLaunchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,70 +234,6 @@ public async Task ShouldNotOpenTwoChromesUsingTheSameLauncher()
}
}

[Fact]
public void ShouldDumpBrowserProcessStderr()
{
var dumpioTextToLog = "MAGIC_DUMPIO_TEST";
var success = false;
var process = GetTestAppProcess(
"PuppeteerSharp.Tests.DumpIO",
$"{dumpioTextToLog} \"{new BrowserFetcher().RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath}\"");

process.ErrorDataReceived += (sender, e) =>
{
success |= e.Data != null && e.Data.Contains(dumpioTextToLog);
};

process.Start();
process.BeginErrorReadLine();
process.WaitForExit();
Assert.True(success);
}

[Fact]
public async Task ShouldCloseTheBrowserWhenTheConnectedProcessCloses()
{
var browserClosedTaskWrapper = new TaskCompletionSource<bool>();
var chromiumProcess = new ChromiumProcess(
new BrowserFetcher().RevisionInfo(BrowserFetcher.DefaultRevision).ExecutablePath,
new LaunchOptions { Headless = true },
TestConstants.LoggerFactory);

await chromiumProcess.StartAsync().ConfigureAwait(false);

var browser = await Puppeteer.ConnectAsync(new ConnectOptions
{
BrowserWSEndpoint = chromiumProcess.EndPoint
});

browser.Disconnected += (sender, e) =>
{
browserClosedTaskWrapper.SetResult(true);
};

KillProcess(chromiumProcess.Process.Id);

await browserClosedTaskWrapper.Task;
Assert.True(browser.IsClosed);
}

[Fact]
public async Task ShouldCloseTheBrowserWhenTheLaunchedProcessCloses()
{
var browserClosedTaskWrapper = new TaskCompletionSource<bool>();
var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }, TestConstants.LoggerFactory);

browser.Disconnected += (sender, e) =>
{
browserClosedTaskWrapper.SetResult(true);
};

KillProcess(browser.ChromiumProcess.Process.Id);

await browserClosedTaskWrapper.Task;
Assert.True(browser.IsClosed);
}

[Fact]
public async Task ShouldWorkWithNoDefaultArguments()
{
Expand Down Expand Up @@ -433,68 +369,5 @@ public async Task ShouldSupportCustomTransport()
Assert.True(customTransportCreated);
}
}

private Process GetTestAppProcess(string appName, string arguments)
{
var process = new Process();

#if NETCOREAPP
process.StartInfo.WorkingDirectory = GetSubprocessWorkingDir(appName);
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = $"{appName}.dll {arguments}";
#else
process.StartInfo.FileName = Path.Combine(GetSubprocessWorkingDir(appName), $"{appName}.exe");
process.StartInfo.Arguments = arguments;
#endif
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
return process;
}

private string GetSubprocessWorkingDir(string dir)
{
#if DEBUG
var build = "Debug";
#else

var build = "Release";
#endif
#if NETCOREAPP
return Path.Combine(
TestUtils.FindParentDirectory("lib"),
dir,
"bin",
build,
"netcoreapp2.2");
#else
return Path.Combine(
TestUtils.FindParentDirectory("lib"),
dir,
"bin",
build,
"net471");
#endif
}

private void KillProcess(int pid)
{
var process = new Process();

//We need to kill the process tree manually
//See: https://github.com/dotnet/corefx/issues/26234
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
process.StartInfo.FileName = "taskkill";
process.StartInfo.Arguments = $"-pid {pid} -t -f";
}
else
{
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = $"-c \"kill -s 9 {pid}\"";
}

process.Start();
process.WaitForExit();
}
}
}

0 comments on commit 26af946

Please sign in to comment.