Home > Blockchain >  PlayWright NUnit test: Page object does not get initialized
PlayWright NUnit test: Page object does not get initialized

Time:07-26

I'm trying to set up a NUnit Playwright project but I'm having trouble getting the sample test to work. Either my test does not run or the Page object doesn't get initialized.

enter image description here

 HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage
   Source: UnitTest1.cs line 15
   Duration: 196 ms

  Message: 
Test method TelerikWebDesignToolTests.Tests.HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage threw exception: 
System.NullReferenceException: Object reference not set to an instance of an object.

  Stack Trace: 
<HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage>d__0.MoveNext() line 17
--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ThreadOperations.ExecuteWithAbortSafety(Action action)

I've tried a few tutorials but for this attempt, I'm following https://playwright.dev/dotnet/docs/intro

If I try to run the code verbatim only changing the namespace and adding curly braces to the end of the namespace my test in my "Test Explorer just doesn't run.

using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using NUnit.Framework;

namespace TelerikWebDesignToolTests
{


    [Parallelizable(ParallelScope.Self)]
    public class Tests : PageTest
    {
        [Test]
        async public Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
        {
            await Page.GotoAsync("https://playwright.dev");

            // Expect a title "to contain" a substring.
            await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));

            // create a locator
            var getStarted = Page.Locator("text=Get Started");

            // Expect an attribute "to be strictly equal" to the value.
            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"));
        }
    }
}

I can see in the out from "Tests" the reason is:

No test matches the given testcase filter `FullyQualifiedName=TelerikWebDesignToolTests.Tests.HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage`

The only way I could get VS to run the test was by modifying the code to what is seen in the included image. For reference, I've pasted the code below.

using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Playwright.NUnit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;

namespace TelerikWebDesignToolTests
{


    [Parallelizable(ParallelScope.Self)]
    [TestClass]
    public class Tests : PageTest
    {
        [TestMethod]
        async public Task HomepageHasPlaywrightInTitleAndGetStartedLinkLinkingtoTheIntroPage()
        {
            await Page.GotoAsync("https://playwright.dev");

            // Expect a title "to contain" a substring.
            await Expect(Page).ToHaveTitleAsync(new Regex("Playwright"));

            // create a locator
            var getStarted = Page.Locator("text=Get Started");

            // Expect an attribute "to be strictly equal" to the value.
            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"));
        }
    }
}

CodePudding user response:

Seems you are mixing up MSTest and NUnit. You need to either use Microsoft.Playwright.MSTest or use Test instead of TestMethod.

  • Related