Skip to content

Commit 54dcc32

Browse files
authored
feat: improve error handling in setup methods for Browser, Context, and Page for TUnit.Playwright (#2746)
1 parent eb3b8cc commit 54dcc32

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

.github/workflows/dotnet.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ jobs:
5757
if: matrix.os == 'ubuntu-latest'
5858
uses: docker/setup-docker-action@v4.3.0
5959

60-
- name: Install PlaywrightDependencies
60+
- name: Install Playwright Dependencies
6161
run: npx playwright install-deps
6262

63+
- name: Install Playwright Browsers
64+
run: npx playwright install
65+
6366
- name: Build
6467
run: dotnet build -c Release
6568

TUnit.Playwright/BrowserTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public async Task<IBrowserContext> NewContext(BrowserNewContextOptions options)
2929
[Before(HookType.Test, "", 0)]
3030
public async Task BrowserSetup()
3131
{
32+
if (BrowserType == null)
33+
{
34+
throw new InvalidOperationException($"BrowserType is not initialized. This may indicate that {nameof(PlaywrightTest)}.{nameof(Playwright)} is not initialized or {nameof(PlaywrightTest)}.{nameof(PlaywrightSetup)} did not execute properly.");
35+
}
36+
3237
var service = await BrowserService.Register(this, BrowserType, _options).ConfigureAwait(false);
3338
Browser = service.Browser;
3439
}

TUnit.Playwright/ContextTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public virtual BrowserNewContextOptions ContextOptions(TestContext testContext)
1919
[Before(HookType.Test, "", 0)]
2020
public async Task ContextSetup(TestContext testContext)
2121
{
22+
if (Browser == null)
23+
{
24+
throw new InvalidOperationException($"Browser is not initialized. This may indicate that {nameof(BrowserTest)}.{nameof(BrowserSetup)} did not execute properly.");
25+
}
26+
2227
Context = await NewContext(ContextOptions(testContext)).ConfigureAwait(false);
2328
}
2429
}

TUnit.Playwright/PageTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public class PageTest : ContextTest
1010
[Before(HookType.Test, "", 0)]
1111
public async Task PageSetup()
1212
{
13+
if (Context == null)
14+
{
15+
throw new InvalidOperationException($"Browser context is not initialized. This may indicate that {nameof(ContextTest)}.{nameof(ContextSetup)} did not execute properly.");
16+
}
17+
1318
Page = await Context.NewPageAsync().ConfigureAwait(false);
1419
}
1520
}

0 commit comments

Comments
 (0)