Last active
July 25, 2021 09:22
-
-
Save Chandankkrr/aab4573cb529e6f519e2d5466094d1db to your computer and use it in GitHub Desktop.
Mocking API response using Playwright
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
using System; | |
using System.Collections.Generic; | |
using System.Text.Json; | |
using System.Threading.Tasks; | |
using Microsoft.Playwright; | |
namespace ConsoleApp | |
{ | |
class Program | |
{ | |
static async Task Main(string[] args) | |
{ | |
const string email = "test.user@blazor.com"; | |
const string password = "Test1234#"; | |
using var playwright = await Playwright.CreateAsync(); | |
await using var browser = await playwright.Chromium.LaunchAsync(); | |
var page = await browser.NewPageAsync(); | |
await page.GotoAsync("https://victorious-tree-06261d500.azurestaticapps.net/register"); | |
// Fill input[type="text"] | |
await page.FillAsync("input[type=\"text\"]", email); | |
// Fill input[type="password"] | |
await page.FillAsync("input[type=\"password\"]", password); | |
// Fill text=PasswordRepeat the password >> input[type="password"] | |
await page.FillAsync("text=PasswordRepeat the password >> input[type=\"password\"]", password); | |
await page.RouteAsync("**/api/account/create", async route => | |
{ | |
await route.FulfillAsync(new RouteFulfillOptions | |
{ | |
Status = 400, | |
Body = JsonSerializer.Serialize(new | |
{ | |
Success = false, | |
Errors = new[] {"User is already registered"} | |
}), | |
Headers = new KeyValuePair<string, string>[] | |
{ | |
new("Access-Control-Allow-Origin", "https://victorious-tree-06261d500.azurestaticapps.net") | |
} | |
}); | |
}); | |
// Use a predicate taking a Response object | |
var waitForAccountCreateTask = page.WaitForResponseAsync("**/api/account/create"); | |
// Click button:has-text("Register") | |
await page.ClickAsync("button:has-text(\"Register\")"); | |
await waitForAccountCreateTask; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment