Friday, December 5, 2025

thumbnail

Writing Automated UI Tests for Full Stack .NET Applications

 ๐Ÿš€ 1. Why Automated UI Tests?


Automated UI tests help you validate:


✔ End-to-end workflows


(e.g., user signs in → adds product → checks out)


✔ Cross-browser behavior


(e.g., Chrome, Edge, Firefox)


✔ Integration with backend APIs and databases

✔ Regression prevention


When UI or backend changes happen.


UI tests simulate real user actions, making them the highest confidence tests (but also the slowest)—so combining them with unit + API tests is part of a healthy testing strategy.


๐Ÿ›  2. Choosing a UI Testing Framework

Recommended: Playwright for .NET


Fast, modern, reliable.


Parallel execution.


Auto-waits for UI elements.


Works with .NET out of the box.


Supports Chromium, WebKit, Firefox.


Easy CI integration.


dotnet add package Microsoft.Playwright

playwright install


Other popular options:

Framework Pros Cons

Selenium Mature, flexible, supports many languages Slower, more brittle, no auto-wait

Cypress Developer-friendly, great for JS apps No official .NET API

Puppeteer Sharp Good for Chrome-based apps Single-browser focus

MAUI/Test Apps For desktop/mobile UI Not web-focused


For web-based full-stack .NET applications, Playwright is the clear winner.


๐Ÿงฑ 3. Test Architecture for Full-Stack .NET UI Tests

Typical layers:

+-----------------------------------------+

| UI Tests (Playwright / Selenium)        |

+-----------------------------------------+

| API Tests (xUnit + TestServer)          |

+-----------------------------------------+

| Unit Tests (xUnit/NUnit)                |

+-----------------------------------------+

| Application Code (ASP.NET, EF Core)     |

+-----------------------------------------+

| DB (SQL, PostgreSQL, etc.)              |

+-----------------------------------------+



UI tests should only verify critical user journeys, e.g.:


Login process


CRUD workflows


Checkout flows


Dashboards


Error states


✨ 4. Setting Up Playwright UI Tests in .NET

Create a test project

dotnet new xunit -n MyApp.UiTests

cd MyApp.UiTests

dotnet add package Microsoft.Playwright


Initialize Playwright

playwright install


๐Ÿงช 5. Example: UI Test with Playwright for a .NET App

Simple login UI automated test

using Microsoft.Playwright;

using Xunit;


public class LoginUiTests : IAsyncLifetime

{

    private IPlaywright _playwright;

    private IBrowser _browser;


    public async Task InitializeAsync()

    {

        _playwright = await Playwright.CreateAsync();

        _browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions

        {

            Headless = true

        });

    }


    public async Task DisposeAsync()

    {

        await _browser.CloseAsync();

        _playwright.Dispose();

    }


    [Fact]

    public async Task Login_Should_DisplayDashboard()

    {

        var page = await _browser.NewPageAsync();


        await page.GotoAsync("https://localhost:5001/login");


        await page.FillAsync("#email", "test@example.com");

        await page.FillAsync("#password", "Password123!");

        await page.ClickAsync("button[type='submit']");


        // Assertion

        await page.WaitForSelectorAsync("#dashboard");

        var header = await page.InnerTextAsync("h1");


        Assert.Equal("Dashboard", header);

    }

}


๐Ÿงช 6. Testing Full-Stack Functionality


UI tests can also verify:


✔ Database updates

✔ API responses

✔ Real-time UI (SignalR, Blazor)

✔ Background jobs (Hangfire, Quartz)


Example: verifying a record was created


await page.ClickAsync("#saveBtn");


// UI confirmation

await page.WaitForSelectorAsync(".success");


// Optional: database verification (integration test hybrid)

var user = await _db.Users.FirstAsync(u => u.Email == "test@example.com");

Assert.NotNull(user);


๐Ÿงฉ 7. Testing Blazor Apps


For Blazor WebAssembly:


Playwright or Selenium still works.


You interact with elements the same way as HTML.


For Blazor Server:


Test like a regular web UI.


Ensure SignalR connections are stable.


⚙️ 8. CI/CD Integration for Automated UI Tests


UI tests can run in:


✔ GitHub Actions

✔ Azure DevOps

✔ GitLab

✔ Jenkins


Example GitHub Actions snippet:


- name: Install Playwright

  run: npx playwright install --with-deps


- name: Run UI Tests

  run: dotnet test MyApp.UiTests


๐Ÿง  9. Best Practices for UI Testing in .NET

✔ Use selectors that are stable


Prefer test IDs:


<button data-test="submit-btn">Submit</button>


✔ Keep UI tests small and focused


One test = one workflow.


✔ Avoid relying on timing


Let Playwright auto-wait.


✔ Seed test data


Options:


EF Core seed scripts


Test containers


SQL scripts


In-memory databases for some tests


✔ Run tests in parallel if safe

✔ Keep environment consistent


Staging database + seeded users recommended.


๐Ÿ“Œ 10. When NOT to Use UI Tests


Don’t use UI tests for:


Heavy validation logic


API contract tests


Error-edge cases


Stress testing


Use API + unit tests instead.


๐ŸŽฏ Conclusion


Automated UI tests are essential for validating complete user flows in full-stack .NET applications. The modern tool of choice is Playwright, which integrates cleanly with .NET and provides fast, stable, cross-browser automation.


A solid testing strategy includes:


UI tests for full end-to-end coverage


API tests for backend correctness


Unit tests for business logic


Together, they make your .NET application reliable, maintainable, and production-ready

Learn Dot Net Course in Hyderabad

Read More

Testing Web APIs in .NET with Postman and xUnit

Testing Web APIs in .NET with Postman and xUnit

Debugging ASP.NET Core Applications Effectively

How to Perform Load Testing on .NET Applications

Visit Our Quality Thought Institute in Hyderabad

Get Directions 

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive