From e4d5353bb545aed4fd2722fab525961b65055474 Mon Sep 17 00:00:00 2001 From: Joe DeFilippo Date: Thu, 18 Apr 2024 21:17:50 -0400 Subject: [PATCH] updates --- .gitignore | 2 ++ playwright-csharp.csproj | 1 + tests/Examples/APIExample.cs | 56 +++++++++++++++++++++++++++++ tests/Examples/PlaywrightExample.cs | 8 ++--- 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/Examples/APIExample.cs diff --git a/.gitignore b/.gitignore index 5ecdc02..0a5e170 100644 --- a/.gitignore +++ b/.gitignore @@ -414,3 +414,5 @@ obj/Debug/net8.0/playwright-csharp.pdb obj/Debug/net8.0/playwright-csharp.sourcelink.json obj/Debug/net8.0/ref/playwright-csharp.dll obj/Debug/net8.0/refint/playwright-csharp.dll +bin/Debug/net8.0/Faker.Net.6.0.dll +bin/Debug/net8.0/de-DE/Faker.Net.6.0.resources.dll diff --git a/playwright-csharp.csproj b/playwright-csharp.csproj index 563593c..aa101a2 100644 --- a/playwright-csharp.csproj +++ b/playwright-csharp.csproj @@ -12,6 +12,7 @@ + diff --git a/tests/Examples/APIExample.cs b/tests/Examples/APIExample.cs new file mode 100644 index 0000000..208fee4 --- /dev/null +++ b/tests/Examples/APIExample.cs @@ -0,0 +1,56 @@ +using System.Net; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Microsoft.Playwright; +using Microsoft.Playwright.NUnit; +using NUnit.Framework; + +namespace PlaywrightTests; + +[TestFixture] +public class APIExample : PageTest +{ + private IAPIRequestContext Request = null; + + [Test] + public async Task APITestGetReturns200() + { + var headers = new Dictionary(); + headers.Add("Authorization", "Bearer "); + + Request = await this.Playwright.APIRequest.NewContextAsync(new() + { + // All requests we send go to this API endpoint. + BaseURL = "http://127.0.0.1:7174", + ExtraHTTPHeaders = headers, + }); + + var Response = await Request.GetAsync("/api/employee/1"); + Assert.AreEqual(200, Response.Status); + } + + [Test] + public async Task AddEmployeeReturns201() + { + var headers = new Dictionary(); + headers.Add("Authorization", "Bearer "); + + Request = await this.Playwright.APIRequest.NewContextAsync(new() + { + BaseURL = "http://127.0.0.1:7174", + ExtraHTTPHeaders = headers, + }); + + var data = new Dictionary() { + { "EmployeeId", 6 }, + { "FirstName", Faker.Name.First() }, + { "LastName", Faker.Name.Last()}, + { "Age", 41 }, + { "State", Faker.Address.UsStateAbbr() } + }; + + var Response = await Request.PostAsync("/api/employee", new() { DataObject = data }); + Assert.AreEqual(201, Response.Status); + + } +} \ No newline at end of file diff --git a/tests/Examples/PlaywrightExample.cs b/tests/Examples/PlaywrightExample.cs index 45a2145..592d140 100644 --- a/tests/Examples/PlaywrightExample.cs +++ b/tests/Examples/PlaywrightExample.cs @@ -1,12 +1,9 @@ using System.Text.RegularExpressions; -using System.Threading.Tasks; using Microsoft.Playwright; using Microsoft.Playwright.NUnit; -using NUnit.Framework; namespace PlaywrightTests; - [TestFixture] public class PlaywrightExample : PageTest { @@ -20,14 +17,17 @@ public async Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntro // create a locator var getStarted = Page.GetByRole(AriaRole.Link, new() { Name = "Get started" }); + // Expect an attribute "to be strictly equal" to the value. + await Expect(getStarted).ToBeAttachedAsync(); await Expect(getStarted).ToHaveAttributeAsync("href", "/docs/intro"); + // Click the get started link. await getStarted.ClickAsync(); // Expects the URL to contain intro. - await Expect(Page).ToHaveURLAsync(new Regex(".*intro")); + await Expect(Page).ToHaveURLAsync("https://playwright.dev/docs/intro"); } } \ No newline at end of file