Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve variable initialization #2223

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions lib/PuppeteerSharp.Tests/InjectedTests/InjectedTests.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
using System.Reflection.Metadata;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.InjectedTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class InjectedTests : PuppeteerPageBaseTest
{
public InjectedTests(ITestOutputHelper output) : base(output)
{
}

[PuppeteerTest("injected.spec.ts", "PuppeteerUtil tests", "should work")]
[PuppeteerFact]
public async Task ShouldWork()
{
var world = (Page.MainFrame as Frame).PuppeteerWorld;
var result = await world.EvaluateFunctionAsync<bool>(@"
PuppeteerUtil => {
return typeof PuppeteerUtil === 'object';
}",
await world.GetPuppeteerUtilAsync());
Assert.True(result);
}

[PuppeteerTest("injected.spec.ts", "createFunction tests", "should work")]
[PuppeteerFact]
public async Task CreateFunctionShouldWork()
{
var world = (Page.MainFrame as Frame).PuppeteerWorld;
var result = await (Page.MainFrame as Frame)
.PuppeteerWorld.EvaluateFunctionAsync<int>(@"({createFunction}, fnString) => {
return createFunction(fnString)(4);
}",
await world.GetPuppeteerUtilAsync(),
"() => 4");
Assert.Equal(4, result);
}
}
}
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace PuppeteerSharp.Tests.InjectedTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class InjectedTests : PuppeteerPageBaseTest
{
public InjectedTests(ITestOutputHelper output) : base(output)
{
}
[PuppeteerTest("injected.spec.ts", "PuppeteerUtil tests", "should work")]
[PuppeteerFact]
public async Task ShouldWork()
{
var world = (Page.MainFrame as Frame).PuppeteerWorld;
var result = await world.EvaluateFunctionAsync<bool>(@"
PuppeteerUtil => {
return typeof PuppeteerUtil === 'object';
}",
await world.GetPuppeteerUtilAsync());
Assert.True(result);
}
[PuppeteerTest("injected.spec.ts", "createFunction tests", "should work")]
[PuppeteerFact]
public async Task CreateFunctionShouldWork()
{
var world = (Page.MainFrame as Frame).PuppeteerWorld;
var result = await (Page.MainFrame as Frame)
.PuppeteerWorld.EvaluateFunctionAsync<int>(@"({createFunction}, fnString) => {
return createFunction(fnString)(4);
}",
await world.GetPuppeteerUtilAsync(),
"() => 4");
Assert.Equal(4, result);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,131 +1,131 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.PageTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class BrowserContextOverridePermissionsTests : PuppeteerPageBaseTest
{
public BrowserContextOverridePermissionsTests(ITestOutputHelper output) : base(output)
{
}

private Task<string> GetPermissionAsync(IPage page, string name)
=> page.EvaluateFunctionAsync<string>(
"name => navigator.permissions.query({ name }).then(result => result.state)",
name);

[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should be prompt by default")]
[PuppeteerFact]
public async Task ShouldBePromptByDefault()
{
await Page.GoToAsync(TestConstants.EmptyPage);
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
}

[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should deny permission when not listed")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldDenyPermissionWhenNotListed()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { });
Assert.Equal("denied", await GetPermissionAsync(Page, "geolocation"));
}

[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should grant permission when listed")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldGrantPermissionWhenListed()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[]
{
OverridePermission.Geolocation
});
Assert.Equal("granted", await GetPermissionAsync(Page, "geolocation"));
}

[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should reset permissions")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldResetPermissions()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[]
{
OverridePermission.Geolocation
});
Assert.Equal("granted", await GetPermissionAsync(Page, "geolocation"));
await Context.ClearPermissionOverridesAsync();
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
}

[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should trigger permission onchange")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldTriggerPermissionOnchange()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Page.EvaluateFunctionAsync(@"() => {
window.events = [];
return navigator.permissions.query({ name: 'geolocation'}).then(function(result) {
window.events.push(result.state);
result.onchange = function() {
window.events.push(result.state);
};
});
}");
Assert.Equal(new string[] { "prompt" }, await Page.EvaluateExpressionAsync<string[]>("window.events"));
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { });
Assert.Equal(new string[] { "prompt", "denied" }, await Page.EvaluateExpressionAsync<string[]>("window.events"));
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[]
{
OverridePermission.Geolocation
});
Assert.Equal(
new string[] { "prompt", "denied", "granted" },
await Page.EvaluateExpressionAsync<string[]>("window.events"));
await Context.ClearPermissionOverridesAsync();
Assert.Equal(
new string[] { "prompt", "denied", "granted", "prompt" },
await Page.EvaluateExpressionAsync<string[]>("window.events"));
}

[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should isolate permissions between browser contexts")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldIsolatePermissionsBetweenBrowserContexts()
{
await Page.GoToAsync(TestConstants.EmptyPage);
var otherContext = await Browser.CreateIncognitoBrowserContextAsync();
var otherPage = await otherContext.NewPageAsync();
await otherPage.GoToAsync(TestConstants.EmptyPage);
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
Assert.Equal("prompt", await GetPermissionAsync(otherPage, "geolocation"));

await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { });
await otherContext.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { OverridePermission.Geolocation });
Assert.Equal("denied", await GetPermissionAsync(Page, "geolocation"));
Assert.Equal("granted", await GetPermissionAsync(otherPage, "geolocation"));

await Context.ClearPermissionOverridesAsync();
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
Assert.Equal("granted", await GetPermissionAsync(otherPage, "geolocation"));

await otherContext.CloseAsync();
}

[SkipBrowserFact(skipFirefox: true)]
public async Task AllEnumsdAreValid()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.OverridePermissionsAsync(
TestConstants.EmptyPage,
Enum.GetValues(typeof(OverridePermission)).Cast<OverridePermission>().ToArray());
Assert.Equal("granted", await GetPermissionAsync(Page, "geolocation"));
await Context.ClearPermissionOverridesAsync();
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
}
}
}
using System.Linq;
using System.Threading.Tasks;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;
namespace PuppeteerSharp.Tests.PageTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class BrowserContextOverridePermissionsTests : PuppeteerPageBaseTest
{
public BrowserContextOverridePermissionsTests(ITestOutputHelper output) : base(output)
{
}
private Task<string> GetPermissionAsync(IPage page, string name)
=> page.EvaluateFunctionAsync<string>(
"name => navigator.permissions.query({ name }).then(result => result.state)",
name);
[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should be prompt by default")]
[PuppeteerFact]
public async Task ShouldBePromptByDefault()
{
await Page.GoToAsync(TestConstants.EmptyPage);
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
}
[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should deny permission when not listed")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldDenyPermissionWhenNotListed()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { });
Assert.Equal("denied", await GetPermissionAsync(Page, "geolocation"));
}
[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should grant permission when listed")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldGrantPermissionWhenListed()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[]
{
OverridePermission.Geolocation
});
Assert.Equal("granted", await GetPermissionAsync(Page, "geolocation"));
}
[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should reset permissions")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldResetPermissions()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[]
{
OverridePermission.Geolocation
});
Assert.Equal("granted", await GetPermissionAsync(Page, "geolocation"));
await Context.ClearPermissionOverridesAsync();
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
}
[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should trigger permission onchange")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldTriggerPermissionOnchange()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Page.EvaluateFunctionAsync(@"() => {
window.events = [];
return navigator.permissions.query({ name: 'geolocation'}).then(function(result) {
window.events.push(result.state);
result.onchange = function() {
window.events.push(result.state);
};
});
}");
Assert.Equal(new string[] { "prompt" }, await Page.EvaluateExpressionAsync<string[]>("window.events"));
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { });
Assert.Equal(new string[] { "prompt", "denied" }, await Page.EvaluateExpressionAsync<string[]>("window.events"));
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[]
{
OverridePermission.Geolocation
});
Assert.Equal(
new string[] { "prompt", "denied", "granted" },
await Page.EvaluateExpressionAsync<string[]>("window.events"));
await Context.ClearPermissionOverridesAsync();
Assert.Equal(
new string[] { "prompt", "denied", "granted", "prompt" },
await Page.EvaluateExpressionAsync<string[]>("window.events"));
}
[PuppeteerTest("page.spec.ts", "BrowserContext.overridePermissions", "should isolate permissions between browser contexts")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldIsolatePermissionsBetweenBrowserContexts()
{
await Page.GoToAsync(TestConstants.EmptyPage);
var otherContext = await Browser.CreateIncognitoBrowserContextAsync();
var otherPage = await otherContext.NewPageAsync();
await otherPage.GoToAsync(TestConstants.EmptyPage);
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
Assert.Equal("prompt", await GetPermissionAsync(otherPage, "geolocation"));
await Context.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { });
await otherContext.OverridePermissionsAsync(TestConstants.EmptyPage, new OverridePermission[] { OverridePermission.Geolocation });
Assert.Equal("denied", await GetPermissionAsync(Page, "geolocation"));
Assert.Equal("granted", await GetPermissionAsync(otherPage, "geolocation"));
await Context.ClearPermissionOverridesAsync();
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
Assert.Equal("granted", await GetPermissionAsync(otherPage, "geolocation"));
await otherContext.CloseAsync();
}
[SkipBrowserFact(skipFirefox: true)]
public async Task AllEnumsdAreValid()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.OverridePermissionsAsync(
TestConstants.EmptyPage,
Enum.GetValues(typeof(OverridePermission)).Cast<OverridePermission>().ToArray());
Assert.Equal("granted", await GetPermissionAsync(Page, "geolocation"));
await Context.ClearPermissionOverridesAsync();
Assert.Equal("prompt", await GetPermissionAsync(Page, "geolocation"));
}
}
}
Loading