|
| 1 | +using Microsoft.Playwright; |
| 2 | + |
| 3 | +namespace AfterBlazorServerSide.Tests.Migration; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Integration tests for the PostBack demo page at /postback-demo. |
| 7 | +/// Verifies GetPostBackEventReference, GetPostBackClientHyperlink, |
| 8 | +/// and ScriptManager.GetCurrent startup script registration. |
| 9 | +/// </summary> |
| 10 | +[Collection(nameof(PlaywrightCollection))] |
| 11 | +public class PostBackTests |
| 12 | +{ |
| 13 | + private readonly PlaywrightFixture _fixture; |
| 14 | + |
| 15 | + public PostBackTests(PlaywrightFixture fixture) |
| 16 | + { |
| 17 | + _fixture = fixture; |
| 18 | + } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Verifies that clicking the postback button triggers a __doPostBack call |
| 22 | + /// and the result area displays the event target and argument. |
| 23 | + /// </summary> |
| 24 | + [Fact] |
| 25 | + public async Task PostBack_Button_TriggersPostBackEvent() |
| 26 | + { |
| 27 | + var page = await _fixture.NewPageAsync(); |
| 28 | + |
| 29 | + try |
| 30 | + { |
| 31 | + await page.GotoAsync($"{_fixture.BaseUrl}/postback-demo", new PageGotoOptions |
| 32 | + { |
| 33 | + WaitUntil = WaitUntilState.NetworkIdle, |
| 34 | + Timeout = 30000 |
| 35 | + }); |
| 36 | + |
| 37 | + // Wait for Blazor interactive circuit to be ready |
| 38 | + var button = page.Locator("#postback-button"); |
| 39 | + await button.WaitForAsync(new LocatorWaitForOptions |
| 40 | + { |
| 41 | + State = WaitForSelectorState.Attached, |
| 42 | + Timeout = 10000 |
| 43 | + }); |
| 44 | + |
| 45 | + await button.ClickAsync(); |
| 46 | + |
| 47 | + // Wait for the result to appear after the server round-trip |
| 48 | + var result = page.Locator("#postback-result"); |
| 49 | + await result.WaitForAsync(new LocatorWaitForOptions |
| 50 | + { |
| 51 | + State = WaitForSelectorState.Attached, |
| 52 | + Timeout = 10000 |
| 53 | + }); |
| 54 | + |
| 55 | + // Allow time for Blazor to process the postback and re-render |
| 56 | + await page.WaitForTimeoutAsync(2000); |
| 57 | + |
| 58 | + var resultText = await result.TextContentAsync(); |
| 59 | + Assert.NotNull(resultText); |
| 60 | + Assert.Contains("PostBack received!", resultText!); |
| 61 | + } |
| 62 | + finally |
| 63 | + { |
| 64 | + await page.CloseAsync(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Verifies that clicking the postback hyperlink (javascript:__doPostBack) |
| 70 | + /// triggers a postback and the hyperlink result area is updated. |
| 71 | + /// </summary> |
| 72 | + [Fact] |
| 73 | + public async Task PostBackHyperlink_Click_TriggersPostBackEvent() |
| 74 | + { |
| 75 | + var page = await _fixture.NewPageAsync(); |
| 76 | + |
| 77 | + try |
| 78 | + { |
| 79 | + await page.GotoAsync($"{_fixture.BaseUrl}/postback-demo", new PageGotoOptions |
| 80 | + { |
| 81 | + WaitUntil = WaitUntilState.NetworkIdle, |
| 82 | + Timeout = 30000 |
| 83 | + }); |
| 84 | + |
| 85 | + // Wait for Blazor interactive circuit to be ready |
| 86 | + var link = page.Locator("#postback-link"); |
| 87 | + await link.WaitForAsync(new LocatorWaitForOptions |
| 88 | + { |
| 89 | + State = WaitForSelectorState.Attached, |
| 90 | + Timeout = 10000 |
| 91 | + }); |
| 92 | + |
| 93 | + await link.ClickAsync(); |
| 94 | + |
| 95 | + // Wait for the result to appear after the server round-trip |
| 96 | + var result = page.Locator("#hyperlink-result"); |
| 97 | + await result.WaitForAsync(new LocatorWaitForOptions |
| 98 | + { |
| 99 | + State = WaitForSelectorState.Attached, |
| 100 | + Timeout = 10000 |
| 101 | + }); |
| 102 | + |
| 103 | + // Allow time for Blazor to process the postback and re-render |
| 104 | + await page.WaitForTimeoutAsync(2000); |
| 105 | + |
| 106 | + var resultText = await result.TextContentAsync(); |
| 107 | + Assert.NotNull(resultText); |
| 108 | + Assert.Contains("PostBack received!", resultText!); |
| 109 | + } |
| 110 | + finally |
| 111 | + { |
| 112 | + await page.CloseAsync(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Verifies that ScriptManager.GetCurrent registers a startup script |
| 118 | + /// that populates the target div on page load. |
| 119 | + /// </summary> |
| 120 | + [Fact] |
| 121 | + public async Task ScriptManager_GetCurrent_RegistersStartupScript() |
| 122 | + { |
| 123 | + var page = await _fixture.NewPageAsync(); |
| 124 | + |
| 125 | + try |
| 126 | + { |
| 127 | + await page.GotoAsync($"{_fixture.BaseUrl}/postback-demo", new PageGotoOptions |
| 128 | + { |
| 129 | + WaitUntil = WaitUntilState.NetworkIdle, |
| 130 | + Timeout = 30000 |
| 131 | + }); |
| 132 | + |
| 133 | + // Wait for the startup script to execute and populate the target |
| 134 | + var target = page.Locator("#scriptmanager-target"); |
| 135 | + await target.WaitForAsync(new LocatorWaitForOptions |
| 136 | + { |
| 137 | + State = WaitForSelectorState.Attached, |
| 138 | + Timeout = 10000 |
| 139 | + }); |
| 140 | + |
| 141 | + // Allow time for OnAfterRenderAsync to fire and JS to execute |
| 142 | + await page.WaitForTimeoutAsync(2000); |
| 143 | + |
| 144 | + var targetText = await target.TextContentAsync(); |
| 145 | + Assert.NotNull(targetText); |
| 146 | + Assert.False(string.IsNullOrWhiteSpace(targetText), |
| 147 | + "ScriptManager target should have text content after startup script runs"); |
| 148 | + } |
| 149 | + finally |
| 150 | + { |
| 151 | + await page.CloseAsync(); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments