-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
OSOE-647: Context extensions and By helpers.
- Loading branch information
Showing
4 changed files
with
112 additions
and
8 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
Lombiq.Tests.UI/Extensions/BrowserUITestContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Lombiq.Tests.UI.Extensions; | ||
using Lombiq.Tests.UI.Services; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Net; | ||
|
||
public static class BrowserUITestContextExtensions | ||
{ | ||
/// <summary> | ||
/// Gets all cookies from the browser and converts them into .NET <see cref="Cookie"/> instances in a <see | ||
/// cref="CookieContainer"/>. This can be useful if you want to make web requests using <see cref="HttpClient"/> | ||
/// while using the login and other cookies from the browser. | ||
/// </summary> | ||
public static CookieContainer GetCookieContainer(this UITestContext context) | ||
{ | ||
var cookieContainer = new CookieContainer(); | ||
foreach (var seleniumCookie in context.Driver.Manage().Cookies.AllCookies) | ||
{ | ||
var netCookie = new Cookie | ||
{ | ||
Domain = seleniumCookie.Domain, | ||
HttpOnly = seleniumCookie.IsHttpOnly, | ||
Name = seleniumCookie.Name, | ||
Path = seleniumCookie.Path, | ||
Secure = seleniumCookie.Secure, | ||
Value = seleniumCookie.Value, | ||
}; | ||
|
||
if (seleniumCookie.Expiry.HasValue) netCookie.Expires = seleniumCookie.Expiry.Value; | ||
|
||
cookieContainer.Add(netCookie); | ||
} | ||
|
||
return cookieContainer; | ||
} | ||
|
||
[SuppressMessage( | ||
"Security", | ||
"SCS0004: Certificate Validation has been disabled.", | ||
Justification = "Necessary for local testing.")] | ||
[SuppressMessage( | ||
"Security", | ||
"CA5399: HttpClient is created without enabling CheckCertificateRevocationList.", | ||
Justification = "Necessary for local testing.")] | ||
public static async Task<T> FetchWithBrowserContextAsync<T>( | ||
this UITestContext context, | ||
HttpMethod method, | ||
string address, | ||
Func<HttpResponseMessage, Task<T>> processResponseAsync) | ||
{ | ||
using var handler = new HttpClientHandler | ||
{ | ||
CookieContainer = context.GetCookieContainer(), | ||
ServerCertificateCustomValidationCallback = (_, _, _, _) => true, | ||
}; | ||
|
||
using var client = new HttpClient(handler); | ||
using var request = new HttpRequestMessage(method, new Uri(context.GetCurrentUri(), address)); | ||
using var response = await client.SendAsync(request); | ||
|
||
return await processResponseAsync(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters