* Cookies tests

* Codefactor

* Remove methods from IPage
This commit is contained in:
Darío Kondratiuk 2020-02-01 13:23:13 -03:00 коммит произвёл GitHub
Родитель a7b7e075af
Коммит dfe06e68c4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 637 добавлений и 85 удалений

Просмотреть файл

@ -43,16 +43,6 @@ namespace PlaywrightSharp
/// </returns>
Task<IPage[]> GetPagesAsync();
/// <summary>
/// Sets the HTML markup to the browser context
/// </summary>
/// <param name="html">HTML markup to assign to the page.</param>
/// <param name="options">The navigations options</param>
/// <returns>A <see cref="Task"/> that completes when the javascript code executing injected the HTML finishes</returns>
/// <seealso cref="IFrame.SetContentAsync(string, NavigationOptions)"/>
/// <seealso cref="IPage.SetContentAsync(string, NavigationOptions)"/>
Task SetContentAsync(string html, NavigationOptions options = null);
/// <summary>
/// Returns the context's cookies
/// </summary>
@ -62,7 +52,6 @@ namespace PlaywrightSharp
/// If no URLs are specified, this method returns cookies for the current page URL.
/// If URLs are specified, only cookies for those URLs are returned.
/// </remarks>
/// <seealso cref="IPage.GetCookiesAsync(string[])"/>
Task<NetworkCookie[]> GetCookiesAsync(params string[] urls);
/// <summary>
@ -70,22 +59,12 @@ namespace PlaywrightSharp
/// </summary>
/// <param name="cookies">Cookies to set</param>
/// <returns>A <see cref="Task"/> that completes when the cookies are set</returns>
/// <seealso cref="IPage.SetCookiesAsync(SetNetworkCookieParam[])"/>
Task SetCookiesAsync(params SetNetworkCookieParam[] cookies);
/// <summary>
/// Deletes cookies from the context
/// </summary>
/// <param name="cookies">Cookies to delete</param>
/// <returns>A <see cref="Task"/> that completes when the cookies are deleted.</returns>
/// <seealso cref="IPage.DeleteCookiesAsync(SetNetworkCookieParam[])"/>
Task DeleteCookiesAsync(params SetNetworkCookieParam[] cookies);
/// <summary>
/// Clears the context's cookies
/// </summary>
/// <returns>A <see cref="Task"/> that completes when the cookies are cleared.</returns>
/// <seealso cref="IPage.ClearCookiesAsync"/>
Task ClearCookiesAsync();
}
}

Просмотреть файл

@ -68,7 +68,6 @@ namespace PlaywrightSharp
/// <param name="html">HTML markup to assign to the page.</param>
/// <param name="options">The navigations options</param>
/// <returns>A <see cref="Task"/> that completes when the javascript code executing injected the HTML finishes</returns>
/// <seealso cref="IBrowserContext.SetContentAsync(string, NavigationOptions)"/>
/// <seealso cref="IPage.SetContentAsync(string, NavigationOptions)"/>
Task SetContentAsync(string html, NavigationOptions options = null);

Просмотреть файл

@ -248,44 +248,8 @@ namespace PlaywrightSharp
/// <param name="options">The navigations options</param>
/// <returns>A <see cref="Task"/> that completes when the javascript code executing injected the HTML finishes</returns>
/// <seealso cref="IFrame.SetContentAsync(string, NavigationOptions)"/>
/// <seealso cref="IBrowserContext.SetContentAsync(string, NavigationOptions)"/>
Task SetContentAsync(string html, NavigationOptions options = null);
/// <summary>
/// Returns the page's cookies
/// </summary>
/// <param name="urls">Url's to return cookies for</param>
/// <returns>A <see cref="Task"/> that completes when the cookies are sent by the browser, yielding a <see cref="t:NetworkCookie[]"/></returns>
/// <remarks>
/// If no URLs are specified, this method returns cookies for the current page URL.
/// If URLs are specified, only cookies for those URLs are returned.
/// </remarks>
/// <seealso cref="IBrowserContext.GetCookiesAsync(string[])"/>
Task<NetworkCookie[]> GetCookiesAsync(params string[] urls);
/// <summary>
/// Clears all of the current cookies and then sets the cookies for the page
/// </summary>
/// <param name="cookies">Cookies to set</param>
/// <returns>A <see cref="Task"/> that completes when the cookies are set</returns>
/// <seealso cref="IBrowserContext.SetCookiesAsync(SetNetworkCookieParam[])"/>
Task SetCookiesAsync(params SetNetworkCookieParam[] cookies);
/// <summary>
/// Deletes cookies from the page
/// </summary>
/// <param name="cookies">Cookies to delete</param>
/// <returns>A <see cref="Task"/> that completes when the cookies are deleted.</returns>
/// <seealso cref="IBrowserContext.DeleteCookiesAsync(SetNetworkCookieParam[])"/>
Task DeleteCookiesAsync(params SetNetworkCookieParam[] cookies);
/// <summary>
/// Clears the page's cookies
/// </summary>
/// <returns>A <see cref="Task"/> that completes when the cookies are cleared.</returns>
/// <seealso cref="IBrowserContext.ClearCookiesAsync"/>
Task ClearCookiesAsync();
/// <summary>
/// Sets extra HTTP headers that will be sent with every request the page initiates
/// </summary>

Просмотреть файл

@ -4,7 +4,6 @@
/// Cookie data.
/// </summary>
/// <seealso cref="IBrowserContext.GetCookiesAsync(string[])"/>
/// <seealso cref="IPage.GetCookiesAsync(string[])"/>
public class NetworkCookie
{
/// <summary>

Просмотреть файл

@ -5,8 +5,6 @@
/// </summary>
/// <seealso cref="IBrowserContext.SetCookiesAsync(SetNetworkCookieParam[])"/>
/// <seealso cref="IBrowserContext.GetCookiesAsync(string[])"/>
/// <seealso cref="IPage.DeleteCookiesAsync(SetNetworkCookieParam[])"/>
/// <seealso cref="IPage.GetCookiesAsync(string[])"/>
public class SetNetworkCookieParam
{
/// <summary>

Просмотреть файл

@ -203,7 +203,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>Accessibility</playwright-describe>
///<playwright-it>should work with regular text</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true, skipLinux: true, skipWindows: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true, skipLinux: true, skipWindows: true)]
public async Task ShouldWorkWithRegularRext()
{
await Page.SetContentAsync("<div>Hello World</div>");
@ -273,7 +273,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>Accessibility</playwright-describe>
///<playwright-it>filtering children of leaf nodes</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true, skipWindows: true, skipLinux: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true, skipWindows: true, skipLinux: true)]
public async Task FilteringChildrenOfLeafNodes()
{
await Page.SetContentAsync(@"
@ -309,7 +309,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>Accessibility</playwright-describe>
///<playwright-it>rich text editable fields should have children</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true)]
public async Task RichTextEditableFieldsShouldHaveChildren()
{
await Page.SetContentAsync(@"
@ -369,7 +369,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>Accessibility</playwright-describe>
///<playwright-it>rich text editable fields with role should have children</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true)]
public async Task RichTextEditableFieldsWithRoleShouldHaveChildren()
{
await Page.SetContentAsync(@"

Просмотреть файл

@ -19,7 +19,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>plaintext contenteditable</playwright-describe>
///<playwright-it>plain text field with role should not have children</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true, skipFirefox: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true, skipFirefox: true)]
public async Task PlainTextFieldWithRoleShouldNotHaveChildren()
{
await Page.SetContentAsync("<div contenteditable='plaintext-only' role='textbox'>Edit this image:<img src='fakeimage.png' alt='my fake image'></div>");
@ -36,7 +36,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>plaintext contenteditable</playwright-describe>
///<playwright-it>plain text field without role should not have content</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true, skipFirefox: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true, skipFirefox: true)]
public async Task PlainTextFieldWithoutRoleShouldNotHaveContent()
{
await Page.SetContentAsync(
@ -49,7 +49,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>plaintext contenteditable</playwright-describe>
///<playwright-it>plain text field with tabindex and without role should not have content</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true, skipFirefox: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true, skipFirefox: true)]
public async Task PlainTextFieldWithTabindexAndWithoutRoleShouldNotHaveContent()
{
await Page.SetContentAsync("div contenteditable=\"plaintext-only\" tabIndex=0>Edit this image:<img src='fakeimage.png' alt='my fake image'></div>");
@ -66,7 +66,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>plaintext contenteditable</playwright-describe>
///<playwright-it>non editable textbox with role and tabIndex and label should not have children</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true, skipFirefox: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true, skipFirefox: true)]
public async Task NonEditableTextboxWithRoleAndTabIndexAndLabelShouldNotHaveChildren()
{
await Page.SetContentAsync(@"
@ -110,7 +110,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>plaintext contenteditable</playwright-describe>
///<playwright-it>checkbox with and tabIndex and label should not have children</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true, skipFirefox: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true, skipFirefox: true)]
public async Task CheckboxWithAndTabIndexAndLabelShouldNotHaveChildren()
{
await Page.SetContentAsync(@"
@ -131,7 +131,7 @@ namespace PlaywrightSharp.Tests.Accessibility
///<playwright-file>accessibility.spec.js</playwright-file>
///<playwright-describe>plaintext contenteditable</playwright-describe>
///<playwright-it>checkbox without label should not have children</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true, skipFirefox: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true, skipFirefox: true)]
public async Task CheckboxWithoutLabelShouldNotHaveChildren()
{
await Page.SetContentAsync(@"

Просмотреть файл

@ -7,10 +7,10 @@ namespace PlaywrightSharp.Tests.Attributes
/// <summary>
/// Skip browsers and/or platforms
/// </summary>
public class SkipBrowserAndPlayformFact : FactAttribute
public class SkipBrowserAndPlatformFact : FactAttribute
{
/// <summary>
/// Creates a new <seealso cref="SkipBrowserAndPlayformFact"/>
/// Creates a new <seealso cref="SkipBrowserAndPlatformFact"/>
/// </summary>
/// <param name="skipFirefox">Skip firefox</param>
/// <param name="skipChromium">Skip Chromium</param>
@ -18,7 +18,7 @@ namespace PlaywrightSharp.Tests.Attributes
/// <param name="skipOSX">Skip OSX</param>
/// <param name="skipWindows">Skip Windows</param>
/// <param name="skipLinux">Skip Linux</param>
public SkipBrowserAndPlayformFact(
public SkipBrowserAndPlatformFact(
bool skipFirefox = false,
bool skipChromium = false,
bool skipWebkit = false,
@ -26,16 +26,36 @@ namespace PlaywrightSharp.Tests.Attributes
bool skipWindows = false,
bool skipLinux = false)
{
if (
(skipFirefox && TestConstants.IsFirefox) ||
(skipWebkit && TestConstants.IsWebKit) ||
(skipChromium && TestConstants.IsChromium) ||
(skipOSX && RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) ||
(skipWindows && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) ||
(skipLinux && RuntimeInformation.IsOSPlatform(OSPlatform.Linux)))
if (SkipBrowser(skipFirefox, skipChromium, skipWebkit) && SkipPlatform(skipOSX, skipWindows, skipLinux))
{
Skip = "Skipped by browser/platform";
}
}
}
private static bool SkipPlatform(bool skipOSX, bool skipWindows, bool skipLinux)
=>
(
(skipOSX && RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) ||
(skipWindows && RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) ||
(skipLinux && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
) ||
(
!skipOSX &&
!skipLinux &&
!skipWindows
);
private static bool SkipBrowser(bool skipFirefox, bool skipChromium, bool skipWebkit)
=>
(
(skipFirefox && TestConstants.IsFirefox) ||
(skipWebkit && TestConstants.IsWebKit) ||
(skipChromium && TestConstants.IsChromium)
) ||
(
!skipFirefox &&
!skipWebkit &&
!skipChromium
);
}
}

Просмотреть файл

@ -0,0 +1,69 @@
using System.Linq;
using System.Threading.Tasks;
using PlaywrightSharp.Tests.Attributes;
using PlaywrightSharp.Tests.BaseTests;
using Xunit;
using Xunit.Abstractions;
namespace PlaywrightSharp.Tests.BrowserContext
{
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.clearCookies</playwright-describe>
public class ClearCookiesTests : PlaywrightSharpPageBaseTest
{
internal ClearCookiesTests(ITestOutputHelper output) : base(output)
{
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.clearCookies</playwright-describe>
/// <playwright-it>should clear cookies</playwright-it>
[Fact]
public async Task ShouldClearCookes()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.SetCookiesAsync(new SetNetworkCookieParam
{
Url = TestConstants.EmptyPage,
Name = "cookie1",
Value = "1"
});
Assert.Equal("cookie1=1", await Page.EvaluateAsync<string>("document.cookie"));
await Context.ClearCookiesAsync();
Assert.Empty(await Page.EvaluateAsync<string>("document.cookie"));
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.clearCookies</playwright-describe>
/// <playwright-it>should isolate cookies when clearing</playwright-it>
[Fact]
public async Task ShouldIsolateWhenClearing()
{
var anotherContext = await NewContextAsync();
await Context.SetCookiesAsync(new SetNetworkCookieParam
{
Name = "page1cookie",
Value = "page1value",
Url = TestConstants.EmptyPage
});
await anotherContext.SetCookiesAsync(new SetNetworkCookieParam
{
Name = "page2cookie",
Value = "page2value",
Url = TestConstants.EmptyPage
});
Assert.Single(await Context.GetCookiesAsync());
Assert.Single(await anotherContext.GetCookiesAsync());
await Context.ClearCookiesAsync();
Assert.Empty((await Context.GetCookiesAsync()));
Assert.Single((await anotherContext.GetCookiesAsync()));
await anotherContext.ClearCookiesAsync();
Assert.Empty(await Context.GetCookiesAsync());
Assert.Empty(await anotherContext.GetCookiesAsync());
}
}
}

Просмотреть файл

@ -0,0 +1,185 @@
using System.Linq;
using System.Threading.Tasks;
using PlaywrightSharp.Tests.Attributes;
using PlaywrightSharp.Tests.BaseTests;
using Xunit;
using Xunit.Abstractions;
namespace PlaywrightSharp.Tests.BrowserContext
{
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.cookies</playwright-describe>
public class CookiesTests : PlaywrightSharpPageBaseTest
{
internal CookiesTests(ITestOutputHelper output) : base(output)
{
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.cookies</playwright-describe>
/// <playwright-it>should return no cookies in pristine browser context</playwright-it>
[Fact]
public async Task ShouldReturnNoCookiesInPristineBrowserContext()
=> Assert.Empty(await Context.GetCookiesAsync());
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.cookies</playwright-describe>
/// <playwright-it>should get a cookie</playwright-it>
[Fact]
public async Task ShouldGetACookie()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Page.EvaluateAsync("() => document.cookie = 'username=John Doe'");
var cookie = (await Page.BrowserContext.GetCookiesAsync()).FirstOrDefault();
Assert.Equal("username", cookie.Name);
Assert.Equal("John Doe", cookie.Value);
Assert.Equal("localhost", cookie.Domain);
Assert.Equal("/", cookie.Path);
Assert.Equal(-1, cookie.Expires);
Assert.False(cookie.HttpOnly);
Assert.False(cookie.Secure);
Assert.True(cookie.Session);
Assert.Equal(SameSite.None, cookie.SameSite);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.cookies</playwright-describe>
/// <playwright-it>should properly report httpOnly cookie</playwright-it>
[Fact]
public async Task ShouldProperlyReportHttpOnlyCookie()
{
Server.SetRoute("/empty.html", context =>
{
context.Response.Headers["Set-Cookie"] = ";HttpOnly; Path=/";
return Task.CompletedTask;
});
await Page.GoToAsync(TestConstants.EmptyPage);
var cookies = await Context.GetCookiesAsync();
Assert.Single(cookies);
Assert.True(cookies[0].HttpOnly);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.cookies</playwright-describe>
/// <playwright-it>should properly report "Strict" sameSite cookie</playwright-it>
[SkipBrowserAndPlatformFact(skipWebkit: true, skipLinux: true, skipWindows: true)]
public async Task ShouldProperlyReportSStrictSameSiteCookie()
{
Server.SetRoute("/empty.html", context =>
{
context.Response.Headers["Set-Cookie"] = ";SameSite=Strict";
return Task.CompletedTask;
});
await Page.GoToAsync(TestConstants.EmptyPage);
var cookies = await Context.GetCookiesAsync();
Assert.Single(cookies);
Assert.Equal(SameSite.Strict, cookies[0].SameSite);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.cookies</playwright-describe>
/// <playwright-it>should properly report "Lax" sameSite cookie</playwright-it>
[SkipBrowserAndPlatformFact(skipWebkit: true, skipLinux: true, skipWindows: true)]
public async Task ShouldProperlyReportLaxSameSiteCookie()
{
Server.SetRoute("/empty.html", context =>
{
context.Response.Headers["Set-Cookie"] = ";SameSite=Lax";
return Task.CompletedTask;
});
await Page.GoToAsync(TestConstants.EmptyPage);
var cookies = await Context.GetCookiesAsync();
Assert.Single(cookies);
Assert.Equal(SameSite.Lax, cookies[0].SameSite);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.cookies</playwright-describe>
/// <playwright-it>should get multiple cookies</playwright-it>
[Fact]
public async Task ShouldGetMultipleCookies()
{
await Page.GoToAsync(TestConstants.EmptyPage);
Assert.Empty(await Context.GetCookiesAsync());
await Page.EvaluateAsync(@"() => {
document.cookie = 'username=John Doe';
document.cookie = 'password=1234';
}");
var cookies = (await Context.GetCookiesAsync()).OrderBy(c => c.Name).ToList();
var cookie = cookies[0];
Assert.Equal("password", cookie.Name);
Assert.Equal("1234", cookie.Value);
Assert.Equal("localhost", cookie.Domain);
Assert.Equal("/", cookie.Path);
Assert.Equal(cookie.Expires, -1);
Assert.False(cookie.HttpOnly);
Assert.False(cookie.Secure);
Assert.True(cookie.Session);
cookie = cookies[1];
Assert.Equal("username", cookie.Name);
Assert.Equal("John Doe", cookie.Value);
Assert.Equal("localhost", cookie.Domain);
Assert.Equal("/", cookie.Path);
Assert.Equal(cookie.Expires, -1);
Assert.False(cookie.HttpOnly);
Assert.False(cookie.Secure);
Assert.True(cookie.Session);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.cookies</playwright-describe>
/// <playwright-it>should get cookies from multiple urls</playwright-it>
[Fact]
public async Task ShouldGetCookiesFromMultipleUrls()
{
await Context.SetCookiesAsync(
new SetNetworkCookieParam
{
Url = "https://foo.com",
Name = "doggo",
Value = "woofs"
},
new SetNetworkCookieParam
{
Url = "https://bar.com",
Name = "catto",
Value = "purrs"
},
new SetNetworkCookieParam
{
Url = "https://baz.com",
Name = "birdo",
Value = "tweets"
}
);
var cookies = (await Context.GetCookiesAsync("https://foo.com", "https://baz.com")).OrderBy(c => c.Name).ToList();
Assert.Equal(2, cookies.Count);
var cookie = cookies[0];
Assert.Equal("birdo", cookie.Name);
Assert.Equal("tweets", cookie.Value);
Assert.Equal("baz.com", cookie.Domain);
Assert.Equal("/", cookie.Path);
Assert.Equal(cookie.Expires, -1);
Assert.False(cookie.HttpOnly);
Assert.True(cookie.Secure);
Assert.True(cookie.Session);
cookie = cookies[1];
Assert.Equal("doggo", cookie.Name);
Assert.Equal("woofs", cookie.Value);
Assert.Equal("foo.com", cookie.Domain);
Assert.Equal("/", cookie.Path);
Assert.Equal(cookie.Expires, -1);
Assert.False(cookie.HttpOnly);
Assert.True(cookie.Secure);
Assert.True(cookie.Session);
}
}
}

Просмотреть файл

@ -4,9 +4,10 @@ using PlaywrightSharp.Tests.BaseTests;
using Xunit;
using Xunit.Abstractions;
namespace PlaywrightSharp.Tests.DefaultBrowserContext
namespace PlaywrightSharp.Tests.BrowserContext
{
/// <playwright-file>defaultbrowsercontext.spec.js</playwright-file>
/// <playwright-describe>defaultContext()</playwright-describe>
public class DefaultBrowserContextTests : PlaywrightSharpPageBaseTest
{
internal DefaultBrowserContextTests(ITestOutputHelper output) : base(output)

Просмотреть файл

@ -0,0 +1,338 @@
using System.Linq;
using System.Threading.Tasks;
using PlaywrightSharp.Tests.Attributes;
using PlaywrightSharp.Tests.BaseTests;
using Xunit;
using Xunit.Abstractions;
namespace PlaywrightSharp.Tests.BrowserContext
{
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
public class SetCookiesTests : PlaywrightSharpPageBaseTest
{
internal SetCookiesTests(ITestOutputHelper output) : base(output)
{
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should work</playwright-it>
[Fact]
public async Task ShouldWork()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.SetCookiesAsync(new SetNetworkCookieParam
{
Url = TestConstants.EmptyPage,
Name = "password",
Value = "123456"
});
Assert.Equal("password=123456", await Page.EvaluateAsync<string>("document.cookie"));
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should isolate cookies in browser contexts</playwright-it>
[Fact]
public async Task ShouldIsolateCookiesInBrowserContexts()
{
var anotherContext = await NewContextAsync();
await Context.SetCookiesAsync(new SetNetworkCookieParam
{
Name = "page1cookie",
Value = "page1value",
Url = TestConstants.EmptyPage
});
await anotherContext.SetCookiesAsync(new SetNetworkCookieParam
{
Name = "page2cookie",
Value = "page2value",
Url = TestConstants.EmptyPage
});
var cookies1 = await Context.GetCookiesAsync();
var cookies2 = await anotherContext.GetCookiesAsync();
Assert.Single(cookies1);
Assert.Single(cookies2);
Assert.Equal("page1cookie", cookies1[0].Name);
Assert.Equal("page1value", cookies1[0].Value);
Assert.Equal("page2cookie", cookies2[0].Name);
Assert.Equal("page2value", cookies2[0].Value);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should set multiple cookies</playwright-it>
[Fact]
public async Task ShouldSetMultipleCookies()
{
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.SetCookiesAsync(
new SetNetworkCookieParam
{
Url = TestConstants.EmptyPage,
Name = "password",
Value = "123456"
},
new SetNetworkCookieParam
{
Url = TestConstants.EmptyPage,
Name = "foo",
Value = "bar"
}
);
Assert.Equal(
new[]
{
"foo=bar",
"password=123456"
},
await Page.EvaluateAsync<string[]>(@"() => {
const cookies = document.cookie.split(';');
return cookies.map(cookie => cookie.trim()).sort();
}")
);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should have |expires| set to |-1| for session cookies</playwright-it>
[Fact]
public async Task ShouldHaveExpiresSetToMinus1ForSessionCookies()
{
await Context.SetCookiesAsync(new SetNetworkCookieParam
{
Url = TestConstants.EmptyPage,
Name = "password",
Value = "123456"
});
var cookies = await Context.GetCookiesAsync();
Assert.True(cookies[0].Session);
Assert.Equal(-1, cookies[0].Expires);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should set cookie with reasonable defaults</playwright-it>
[Fact]
public async Task ShouldSetCookieWithReasonableDefaults()
{
await Context.SetCookiesAsync(new SetNetworkCookieParam
{
Url = TestConstants.EmptyPage,
Name = "password",
Value = "123456"
});
var cookie = Assert.Single(await Context.GetCookiesAsync());
Assert.Equal("password", cookie.Name);
Assert.Equal("123456", cookie.Value);
Assert.Equal("localhost", cookie.Domain);
Assert.Equal("/", cookie.Path);
Assert.Equal(-1, cookie.Expires);
Assert.False(cookie.HttpOnly);
Assert.False(cookie.Secure);
Assert.True(cookie.Session);
Assert.Equal(SameSite.None, cookie.SameSite);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should set a cookie with a path</playwright-it>
[Fact]
public async Task ShouldSetACookieWithAPath()
{
await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
await Context.SetCookiesAsync(new SetNetworkCookieParam
{
Domain = "localhost",
Path = "/grid.html",
Name = "gridcookie",
Value = "GRID"
});
var cookie = Assert.Single(await Context.GetCookiesAsync());
Assert.Equal("gridcookie", cookie.Name);
Assert.Equal("GRID", cookie.Value);
Assert.Equal("localhost", cookie.Domain);
Assert.Equal("/grid.html", cookie.Path);
Assert.Equal(cookie.Expires, -1);
Assert.False(cookie.HttpOnly);
Assert.False(cookie.Secure);
Assert.True(cookie.Session);
Assert.Equal(SameSite.None, cookie.SameSite);
Assert.Equal("gridcookie=GRID", await Page.EvaluateAsync<string>("document.cookie"));
await Page.GoToAsync(TestConstants.EmptyPage);
Assert.Empty(await Page.EvaluateAsync<string>("document.cookie"));
await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
Assert.Equal("gridcookie=GRID", await Page.EvaluateAsync<string>("document.cookie"));
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should not set a cookie with blank page URL</playwright-it>
[Fact]
public async Task ShouldNotSetACookieWithBlankPageURL()
{
await Page.GoToAsync(TestConstants.AboutBlank);
var exception = await Assert.ThrowsAsync<PlaywrightSharpException>(async ()
=> await Context.SetCookiesAsync(
new SetNetworkCookieParam
{
Url = TestConstants.EmptyPage,
Name = "example-cookie",
Value = "best"
},
new SetNetworkCookieParam
{
Url = "about:blank",
Name = "example-cookie",
Value = "best"
}));
Assert.Equal("Blank page can not have cookie \"example-cookie-blank\"", exception.Message);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should not set a cookie on a data URL page</playwright-it>
[Fact]
public async Task ShouldNotSetACookieOnADataURLPage()
{
await Page.GoToAsync("data:,Hello%2C%20World!");
var exception = await Assert.ThrowsAnyAsync<PlaywrightSharpException>(async ()
=> await Context.SetCookiesAsync(
new SetNetworkCookieParam
{
Url = "data:,Hello%2C%20World!",
Name = "example-cookie",
Value = "best"
}));
Assert.Equal("Data URL page can not have cookie \"example-cookie\"", exception.Message);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should default to setting secure cookie for HTTPS websites</playwright-it>
[Fact]
public async Task ShouldDefaultToSettingSecureCookieForHttpsWebsites()
{
await Page.GoToAsync(TestConstants.EmptyPage);
string SecureUrl = "https://example.com";
await Context.SetCookiesAsync(new SetNetworkCookieParam
{
Url = SecureUrl,
Name = "foo",
Value = "bar"
});
var cookie = Assert.Single(await Context.GetCookiesAsync(SecureUrl));
Assert.True(cookie.Secure);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should be able to set unsecure cookie for HTTP website</playwright-it>
[Fact]
public async Task ShouldBeAbleToSetUnsecureCookieForHttpWebSite()
{
await Page.GoToAsync(TestConstants.EmptyPage);
string SecureUrl = "http://example.com";
await Context.SetCookiesAsync(new SetNetworkCookieParam
{
Url = SecureUrl,
Name = "foo",
Value = "bar"
});
var cookie = Assert.Single(await Context.GetCookiesAsync(SecureUrl));
Assert.False(cookie.Secure);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>should set a cookie on a different domain</playwright-it>
[Fact]
public async Task ShouldSetACookieOnADifferentDomain()
{
await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
await Context.SetCookiesAsync(new SetNetworkCookieParam { Name = "example-cookie", Value = "best", Url = "https://www.example.com" });
Assert.Equal(string.Empty, await Page.EvaluateAsync<string>("document.cookie"));
Assert.Empty(await Context.GetCookiesAsync());
var cookie = Assert.Single(await Context.GetCookiesAsync("https://www.example.com"));
Assert.Equal("example-cookie", cookie.Name);
Assert.Equal("best", cookie.Value);
Assert.Equal("www.example.com", cookie.Domain);
Assert.Equal("/", cookie.Path);
Assert.Equal(cookie.Expires, -1);
Assert.False(cookie.HttpOnly);
Assert.True(cookie.Secure);
Assert.True(cookie.Session);
}
/// <playwright-file>cookies.spec.js</playwright-file>
/// <playwright-describe>BrowserContext.setCookies</playwright-describe>
/// <playwright-it>sshould set cookies from a frame</playwright-it>
[Fact]
public async Task ShouldSetCookiesFromAFrame()
{
await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");
await Context.SetCookiesAsync(
new SetNetworkCookieParam
{
Url = TestConstants.ServerUrl,
Name = "localhost-cookie",
Value = "best"
},
new SetNetworkCookieParam
{
Url = TestConstants.CrossProcessUrl,
Name = "127-cookie",
Value = "worst"
});
await Page.EvaluateAsync(@"src => {
let fulfill;
const promise = new Promise(x => fulfill = x);
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
iframe.onload = fulfill;
iframe.src = src;
return promise;
}", TestConstants.CrossProcessHttpPrefix);
Assert.Equal("localhost-cookie=best", await Page.EvaluateAsync<string>("document.cookie"));
Assert.Equal("127-cookie=worst", await Page.FirstChildFrame().EvaluateAsync<string>("document.cookie"));
var cookie = Assert.Single(await Context.GetCookiesAsync());
Assert.Equal("localhost-cookie", cookie.Name);
Assert.Equal("best", cookie.Value);
Assert.Equal("localhost", cookie.Domain);
Assert.Equal("/", cookie.Path);
Assert.Equal(cookie.Expires, -1);
Assert.False(cookie.HttpOnly);
Assert.False(cookie.Secure);
Assert.True(cookie.Session);
Assert.Equal(SameSite.None, cookie.SameSite);
cookie = Assert.Single(await Context.GetCookiesAsync(TestConstants.CrossProcessHttpPrefix));
Assert.Equal("127-cookie", cookie.Name);
Assert.Equal("worst", cookie.Value);
Assert.Equal("127.0.0.1", cookie.Domain);
Assert.Equal("/", cookie.Path);
Assert.Equal(cookie.Expires, -1);
Assert.False(cookie.HttpOnly);
Assert.False(cookie.Secure);
Assert.True(cookie.Session);
Assert.Equal(SameSite.None, cookie.SameSite);
}
}
}

Просмотреть файл

@ -557,7 +557,7 @@ button.style.position = 'absolute';
///<playwright-file>click.spec.js</playwright-file>
///<playwright-describe>Page.click</playwright-describe>
///<playwright-it>should update modifiers correctly</playwright-it>
[SkipBrowserAndPlayformFact(skipWebkit: true)]
[SkipBrowserAndPlatformFact(skipWebkit: true)]
public async Task ShouldUpdateModifiersCorrectly()
{
await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
@ -584,7 +584,7 @@ button.style.position = 'absolute';
///<playwright-file>click.spec.js</playwright-file>
///<playwright-describe>Page.click</playwright-describe>
///<playwright-it>should click an offscreen element when scroll-behavior is smooth</playwright-it>
[SkipBrowserAndPlayformFact(skipChromium: true)]
[SkipBrowserAndPlatformFact(skipChromium: true)]
public async Task ShouldClickAnOffscreenElementWhenScrollBehaviorIsSmooth()
{
await Page.SetContentAsync(@$"