Skip to content

Commit

Permalink
Merge pull request #413 from Lombiq/issue/OFFI-135
Browse files Browse the repository at this point in the history
OFFI-135: DRYing and using system json
  • Loading branch information
sarahelsaig authored Oct 8, 2024
2 parents a533c0b + 7e5e38f commit 014340e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Task VerifyBlogImage() =>
// is the different rendering of text on each platform, but it can occur between different Linux distributions too.
// Here: https://pandasauce.org/post/linux-fonts/ you can find a good summary about this from 2019, but still valid
// in 2022.
[Theory, Chrome, Edge]
[Theory, Chrome, Edge(Skip = "Until not resolved: https://github.com/atata-framework/atata-webdriversetup/issues/16")]
public Task VerifyNavbar(Browser browser) =>
ExecuteTestAfterSetupAsync(
context =>
Expand Down
6 changes: 4 additions & 2 deletions Lombiq.Tests.UI.Samples/Tests/MultiBrowserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ public MultiBrowserTests(ITestOutputHelper testOutputHelper)

// First, let's see a test using Edge. While the default browser is Chrome if you don't set anything, all
// ExecuteTest* methods can also accept a browser, if you want to use a different one.
[Fact]
#pragma warning disable xUnit1004
[Fact(Skip = "Until not resolved: https://github.com/atata-framework/atata-webdriversetup/issues/16")]
#pragma warning restore xUnit1004
public Task AnonymousHomePageShouldExistWithEdge() =>
ExecuteTestAfterSetupAsync(NavbarIsCorrect, Browser.Edge);

// This test is now marked not with the [Fact] attribute but [Theory]. With it, you can create so-called data-driven
// tests. [Chrome] and [Edge] are input parameters of the test, and thus in effect, you have now two tests:
// AnonymousHomePageShouldExistMultiBrowser once with Chrome, and once with Edge. See here for more info:
// https://andrewlock.net/creating-parameterised-tests-in-xunit-with-inlinedata-classdata-and-memberdata/.
[Theory, Chrome, Edge]
[Theory, Chrome, Edge(Skip = "Until not resolved: https://github.com/atata-framework/atata-webdriversetup/issues/16")]
public Task AnonymousHomePageShouldExistMultiBrowser(Browser browser) =>
ExecuteTestAfterSetupAsync(NavbarIsCorrect, browser);

Expand Down
76 changes: 51 additions & 25 deletions Lombiq.Tests.UI/Extensions/HttpClientUITestContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Lombiq.Tests.UI.Services;
using Newtonsoft.Json.Linq;
using Shouldly;
using System;
using System.Collections.Generic;
Expand All @@ -25,6 +24,8 @@ namespace Lombiq.Tests.UI.Extensions;
Justification = "Disposed by the HttpClient.")]
public static class HttpClientUITestContextExtensions
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web);

public static HttpClient CreateClient(this UITestContext context)
{
var handler = new HttpClientHandler
Expand Down Expand Up @@ -102,8 +103,7 @@ public static async Task<TObject> GetAndReadResponseContentAsync<TObject>(
where TObject : class
{
var content = await GetAndReadResponseContentAsync(context, client, requestUri);
var parsed = JToken.Parse(content);
return parsed.ToObject<TObject>();
return Deserialize<TObject>(content);
}

/// <summary>
Expand All @@ -117,7 +117,7 @@ public static async Task<string> PostAndReadResponseContentAsync(
string requestUri,
string json)
{
using var response = await PostAndGetResponseAsync(client, requestUri, json);
using var response = await PostAndGetResponseAsync(client, json, requestUri);
return await response.Content.ReadAsStringAsync();
}

Expand All @@ -129,16 +129,29 @@ public static async Task<string> PostAndReadResponseContentAsync(
public static async Task<TObject> PostAndReadResponseContentAsync<TObject>(
this UITestContext context,
HttpClient client,
string requestUri,
string json)
where TObject : class
{
using var response = await PostAndGetResponseAsync(client, requestUri, json);
var content = await response.Content.ReadAsStringAsync();
var parsed = JToken.Parse(content);
string json,
string requestUri)
where TObject : class =>
Deserialize<TObject>(await context.PostAndReadResponseContentAsync(
client,
requestUri,
json));

return parsed.ToObject<TObject>();
}
/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided
/// <paramref name="objectToSerialize"/>, that will be serialized as JSON, then the response content is deserialized
/// to the given <typeparamref name="TObject"/> and returned.
/// </summary>
public static async Task<TObject> PostAndReadResponseContentAsync<TObject>(
this UITestContext context,
HttpClient client,
object objectToSerialize,
string requestUri)
where TObject : class =>
Deserialize<TObject>(await context.PostAndReadResponseContentAsync(
client,
requestUri,
Serialize(objectToSerialize)));

/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided
Expand All @@ -149,11 +162,8 @@ public static Task<string> PostAndReadResponseContentAsync(
this UITestContext context,
HttpClient client,
object objectToSerialize,
string requestUri)
{
var json = JsonSerializer.Serialize(objectToSerialize);
return PostAndReadResponseContentAsync(context, client, requestUri, json);
}
string requestUri) =>
PostAndReadResponseContentAsync(context, client, requestUri, Serialize(objectToSerialize));

/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided
Expand All @@ -164,27 +174,29 @@ public static Task<HttpResponseMessage> PostAndGetResponseAsync(
this UITestContext context,
HttpClient client,
object objectToSerialize,
string requestUri)
{
var json = JsonSerializer.Serialize(objectToSerialize);
return PostAndGetResponseAsync(client, requestUri, json);
}
string requestUri) =>
PostAndGetResponseAsync(client, Serialize(objectToSerialize), requestUri);

/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided <paramref name="json"/>.
/// </summary>
/// <returns>The <see cref="HttpResponseMessage"/>.</returns>
public static async Task<HttpResponseMessage> PostAndGetResponseAsync(
HttpClient client,
string requestUri,
string json)
string json,
string requestUri)
{
var stringContent = new StringContent(json, Encoding.UTF8, MediaTypeNames.Application.Json);
var response = await client.PostAsync(requestUri, stringContent);

return response;
}

/// <summary>
/// Issues a POST request to the given <paramref name="requestUri"/> using the provided
/// <paramref name="objectToSerialize"/>, that will be serialized as JSON, then the response is checked if it's the
/// <paramref name="expected"/> status code.
/// </summary>
public static async Task PostAndResponseStatusCodeShouldBeAsync(
this UITestContext context,
HttpClient client,
Expand All @@ -195,4 +207,18 @@ public static async Task PostAndResponseStatusCodeShouldBeAsync(
using var response = await context.PostAndGetResponseAsync(client, objectToSerialize, requestUri);
response.StatusCode.ShouldBe(expected);
}

/// <summary>
/// Returns the serialized object as JSON using the default <see cref="JOptions"/> settings.
/// </summary>
public static string Serialize(object objectToSerialize) =>
JsonSerializer.Serialize(objectToSerialize, JsonSerializerOptions);

/// <summary>
/// Deserializes the provided <paramref name="content"/> to the given <typeparamref name="TObject"/> using the
/// default <see cref="JOptions"/> settings.
/// </summary>
public static TObject Deserialize<TObject>(string content)
where TObject : class =>
JsonSerializer.Deserialize<TObject>(content, JsonSerializerOptions);
}

0 comments on commit 014340e

Please sign in to comment.