Skip to content

Commit

Permalink
Tests: Implement the Chrome bit (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
fgiuliani authored Jul 11, 2020
1 parent 5527263 commit 2196ff4
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 23 deletions.
12 changes: 10 additions & 2 deletions lib/PuppeteerSharp.Tests/BrowserTests/UserAgentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ public UserAgentTests(ITestOutputHelper output) : base(output)
[Fact]
public async Task ShouldIncludeWebKit()
{
var userAgent = await Browser.GetUserAgentAsync();
string userAgent = await Browser.GetUserAgentAsync();
Assert.NotEmpty(userAgent);
Assert.Contains("WebKit", userAgent);

if (TestConstants.IsChrome)
{
Assert.Contains("WebKit", userAgent);
}
else
{
Assert.Contains("Gecko", userAgent);
}
}
}
}
12 changes: 10 additions & 2 deletions lib/PuppeteerSharp.Tests/BrowserTests/VersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ public VersionTests(ITestOutputHelper output) : base(output)
[Fact]
public async Task ShouldReturnWhetherWeAreInHeadless()
{
var version = await Browser.GetVersionAsync();
string version = await Browser.GetVersionAsync();
Assert.NotEmpty(version);
Assert.Equal(TestConstants.DefaultBrowserOptions().Headless, version.StartsWith("Headless"));

if (TestConstants.IsChrome)
{
Assert.Equal(TestConstants.DefaultBrowserOptions().Headless, version.StartsWith("Headless"));
}
else
{
Assert.StartsWith("Firefox/", version);
}
}
}
}
10 changes: 9 additions & 1 deletion lib/PuppeteerSharp.Tests/ElementHandleTests/BoundingBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ await Page.SetViewportAsync(new ViewPortOptions
var nestedFrame = childFrame.ChildFrames.Last();
var elementHandle = await nestedFrame.QuerySelectorAsync("div");
var box = await elementHandle.BoundingBoxAsync();
Assert.Equal(new BoundingBox(28, 260, 264, 18), box);

if (TestConstants.IsChrome)
{
Assert.Equal(new BoundingBox(28, 260, 264, 18), box);
}
else
{
Assert.Equal(new BoundingBox(28, 182, 254, 18), box);
}
}

[Fact]
Expand Down
3 changes: 2 additions & 1 deletion lib/PuppeteerSharp.Tests/JSHandleTests/JsonValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public async Task ShouldThrowForCircularObjects()
var windowHandle = await Page.EvaluateExpressionHandleAsync("window");
var exception = await Assert.ThrowsAsync<MessageException>(()
=> windowHandle.JsonValueAsync());
Assert.Contains("Object reference chain is too long", exception.Message);

Assert.Contains(TestConstants.IsChrome ? "Object reference chain is too long" : "Object is not serializable", exception.Message);
}
}
}
11 changes: 10 additions & 1 deletion lib/PuppeteerSharp.Tests/NetworkTests/NetworkEventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ public async Task PageEventsRequestFailed()
Assert.Contains("one-style.css", failedRequests[0].Url);
Assert.Null(failedRequests[0].Response);
Assert.Equal(ResourceType.StyleSheet, failedRequests[0].ResourceType);
Assert.Equal("net::ERR_FAILED", failedRequests[0].Failure);

if (TestConstants.IsChrome)
{
Assert.Equal("net::ERR_FAILED", failedRequests[0].Failure);
}
else
{
Assert.Equal("NS_ERROR_FAILURE", failedRequests[0].Failure);
}

Assert.NotNull(failedRequests[0].Frame);
}

Expand Down
10 changes: 9 additions & 1 deletion lib/PuppeteerSharp.Tests/NetworkTests/RequestHeadersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ public RequestHeadersTests(ITestOutputHelper output) : base(output)
public async Task ShouldWork()
{
var response = await Page.GoToAsync(TestConstants.EmptyPage);
Assert.Contains("Chrome", response.Request.Headers["User-Agent"]);

if (TestConstants.IsChrome)
{
Assert.Contains("Chrome", response.Request.Headers["User-Agent"]);
}
else
{
Assert.Contains("Firefox", response.Request.Headers["User-Agent"]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,15 @@ public async Task ShouldFailNavigationWhenAbortingMainResource()
Page.Request += async (sender, e) => await e.Request.AbortAsync();
var exception = await Assert.ThrowsAsync<NavigationException>(
() => Page.GoToAsync(TestConstants.EmptyPage));
Assert.Contains("net::ERR_FAILED", exception.Message);

if (TestConstants.IsChrome)
{
Assert.Contains("net::ERR_FAILED", exception.Message);
}
else
{
Assert.Contains("NS_ERROR_FAILURE", exception.Message);
}
}

[Fact]
Expand Down Expand Up @@ -379,7 +387,15 @@ public async Task ShouldBeAbleToAbortRedirects()
return e.message;
}
}");
Assert.Contains("Failed to fetch", result);

if (TestConstants.IsChrome)
{
Assert.Contains("Failed to fetch", result);
}
else
{
Assert.Contains("NetworkError", result);
}
}

[Fact]
Expand Down
10 changes: 9 additions & 1 deletion lib/PuppeteerSharp.Tests/PageTests/CloseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ public async Task ShouldRunBeforeunloadIfAskedFor()
Page.Dialog += async (sender, e) =>
{
Assert.Equal(DialogType.BeforeUnload, e.Dialog.DialogType);
Assert.Equal(string.Empty, e.Dialog.Message);
Assert.Equal(string.Empty, e.Dialog.DefaultValue);

if (TestConstants.IsChrome)
{
Assert.Equal(string.Empty, e.Dialog.Message);
}
else
{
Assert.Equal("This page is asking you to confirm that you want to leave - data you have entered may not be saved.", e.Dialog.Message);
}

await e.Dialog.Accept();
dialogTask.TrySetResult(true);
};
Expand Down
10 changes: 9 additions & 1 deletion lib/PuppeteerSharp.Tests/PageTests/Events/ConsoleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@ public async Task ShouldTriggerCorrectLog()
await Page.EvaluateFunctionAsync("async url => fetch(url).catch(e => {})", TestConstants.EmptyPage);
var message = await messageTask.Task;
Assert.Contains("No 'Access-Control-Allow-Origin'", message.Text);
Assert.Equal(ConsoleType.Error, message.Type);

if (TestConstants.IsChrome)
{
Assert.Equal(ConsoleType.Error, message.Type);
}
else
{
Assert.Equal(ConsoleType.Warning, message.Type);
}
}

[Fact]
Expand Down
50 changes: 45 additions & 5 deletions lib/PuppeteerSharp.Tests/PageTests/GotoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ public async Task ShouldFailWhenServerReturns204()
});
var exception = await Assert.ThrowsAnyAsync<PuppeteerException>(
() => Page.GoToAsync(TestConstants.EmptyPage));
Assert.Contains("net::ERR_ABORTED", exception.Message);

if (TestConstants.IsChrome)
{
Assert.Contains("net::ERR_ABORTED", exception.Message);
}
else
{
Assert.Contains("NS_BINDING_ABORTED", exception.Message);
}
}

[Fact]
Expand Down Expand Up @@ -118,7 +126,15 @@ public async Task ShouldNavigateToEmptyPage(WaitUntilNavigation waitUntil)
public async Task ShouldFailWhenNavigatingToBadUrl()
{
var exception = await Assert.ThrowsAnyAsync<Exception>(async () => await Page.GoToAsync("asdfasdf"));
Assert.Contains("Cannot navigate to invalid URL", exception.Message);

if (TestConstants.IsChrome)
{
Assert.Contains("Cannot navigate to invalid URL", exception.Message);
}
else
{
Assert.Contains("Invalid url", exception.Message);
}
}

[Fact]
Expand All @@ -129,21 +145,45 @@ public async Task ShouldFailWhenNavigatingToBadSSL()
Page.RequestFailed += (sender, e) => Assert.NotNull(e.Request);

var exception = await Assert.ThrowsAnyAsync<Exception>(async () => await Page.GoToAsync(TestConstants.HttpsPrefix + "/empty.html"));
Assert.Contains("net::ERR_CERT_AUTHORITY_INVALID", exception.Message);

if (TestConstants.IsChrome)
{
Assert.Contains("net::ERR_CERT_AUTHORITY_INVALID", exception.Message);
}
else
{
Assert.Contains("SSL_ERROR_UNKNOWN", exception.Message);
}
}

[Fact]
public async Task ShouldFailWhenNavigatingToBadSSLAfterRedirects()
{
var exception = await Assert.ThrowsAnyAsync<Exception>(async () => await Page.GoToAsync(TestConstants.HttpsPrefix + "/redirect/2.html"));
Assert.Contains("net::ERR_CERT_AUTHORITY_INVALID", exception.Message);

if (TestConstants.IsChrome)
{
Assert.Contains("net::ERR_CERT_AUTHORITY_INVALID", exception.Message);
}
else
{
Assert.Contains("SSL_ERROR_UNKNOWN", exception.Message);
}
}

[Fact]
public async Task ShouldFailWhenMainResourcesFailedToLoad()
{
var exception = await Assert.ThrowsAnyAsync<Exception>(async () => await Page.GoToAsync("http://localhost:44123/non-existing-url"));
Assert.Contains("net::ERR_CONNECTION_REFUSED", exception.Message);

if (TestConstants.IsChrome)
{
Assert.Contains("net::ERR_CONNECTION_REFUSED", exception.Message);
}
else
{
Assert.Contains("NS_ERROR_CONNECTION_REFUSED", exception.Message);
}
}

[Fact]
Expand Down
37 changes: 31 additions & 6 deletions lib/PuppeteerSharp.Tests/PuppeteerTests/PuppeteerLaunchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ public async Task UserDataDirArgument()
{
var launcher = new Launcher(TestConstants.LoggerFactory);
var options = TestConstants.DefaultBrowserOptions();
options.Args = options.Args.Concat(new[] { $"--user-data-dir=\"{userDataDir}\"" }).ToArray();

if (TestConstants.IsChrome)
{
options.Args = options.Args.Concat(new[] { $"--user-data-dir=\"{userDataDir}\"" }).ToArray();
}
else
{
options.Args = options.Args.Concat(new string[] { "-profile", userDataDir.ToString() }).ToArray();
}

using (var browser = await launcher.LaunchAsync(options))
{
Expand Down Expand Up @@ -170,18 +178,35 @@ await page.EvaluateExpressionAsync(
}

[Fact]
public void ShouldReturnTheDefaultChromeArguments()
public void ShouldReturnTheDefaultArguments()
{
Assert.Contains("--no-first-run", Puppeteer.GetDefaultArgs());
Assert.Contains("--headless", Puppeteer.GetDefaultArgs());
Assert.DoesNotContain("--headless", Puppeteer.GetDefaultArgs(new LaunchOptions
{
Headless = false
}));
Assert.Contains("--user-data-dir=\"foo\"", Puppeteer.GetDefaultArgs(new LaunchOptions

if (TestConstants.IsChrome)
{
UserDataDir = "foo"
}));
Assert.Contains("--no-first-run", Puppeteer.GetDefaultArgs());
Assert.Contains("--user-data-dir=\"foo\"", Puppeteer.GetDefaultArgs(new LaunchOptions
{
UserDataDir = "foo"
}));
}
else
{
Assert.Contains("--no-remote", Puppeteer.GetDefaultArgs());
Assert.Contains("--foreground", Puppeteer.GetDefaultArgs());
Assert.Contains("--profile", Puppeteer.GetDefaultArgs(new LaunchOptions
{
UserDataDir = "foo"
}));
Assert.Contains("foo", Puppeteer.GetDefaultArgs(new LaunchOptions
{
UserDataDir = "foo"
}));
}
}

[Theory]
Expand Down
1 change: 1 addition & 0 deletions lib/PuppeteerSharp.Tests/TestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static class TestConstants
public static readonly string EmptyPage = $"{ServerUrl}/empty.html";
public static readonly string CrossProcessUrl = ServerIpUrl;
public static readonly string ExtensionPath = Path.Combine(Directory.GetCurrentDirectory(), "Assets", "simple-extension");
public static readonly bool IsChrome = Environment.GetEnvironmentVariable("PRODUCT") != "FIREFOX";

public static readonly DeviceDescriptor IPhone = Puppeteer.Devices[DeviceDescriptorName.IPhone6];
public static readonly DeviceDescriptor IPhone6Landscape = Puppeteer.Devices[DeviceDescriptorName.IPhone6Landscape];
Expand Down

0 comments on commit 2196ff4

Please sign in to comment.