Skip to content

Commit b8862f5

Browse files
net0wellclaude
andcommitted
test(painel): critérios de aceite da spec 008 com Playwright e axe-core
- DashboardHost real no Kestrel com 4 usuários de login fixo, worker em processo, PostgreSQL e RabbitMQ (D-156). - AC-1 a 13, AC-15 a 19, AC-23 e AC-25; trace do Playwright guardado quando um teste falha. - Microsoft.Playwright 1.63.0 (D-160) e Deque.AxeCore.Playwright 4.13.0 (D-161). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e9196c9 commit b8862f5

15 files changed

Lines changed: 1190 additions & 0 deletions

‎Directory.Packages.props‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
</PropertyGroup>
55
<ItemGroup>
66
<PackageVersion Include="Cronos" Version="0.13.0" />
7+
<PackageVersion Include="Deque.AxeCore.Playwright" Version="4.13.0" />
78
<PackageVersion Include="JsonSchema.Net" Version="9.4.1" />
89
<PackageVersion Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="10.0.13" />
910
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.13" />
@@ -25,6 +26,7 @@
2526
<PackageVersion Include="Microsoft.Extensions.Logging" Version="10.0.13" />
2627
<PackageVersion Include="Microsoft.Extensions.Options" Version="10.0.13" />
2728
<PackageVersion Include="Microsoft.Extensions.TimeProvider.Testing" Version="10.11.0" />
29+
<PackageVersion Include="Microsoft.Playwright" Version="1.63.0" />
2830
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
2931
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.20.1" />
3032
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.20.1" />

‎PythonScriptRunner.slnx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Project Path="tests/PythonScriptRunner.Core.Tests/PythonScriptRunner.Core.Tests.csproj" />
1818
<Project Path="tests/PythonScriptRunner.Dashboard.Tests/PythonScriptRunner.Dashboard.Tests.csproj" />
1919
<Project Path="tests/PythonScriptRunner.EntityFrameworkCore.Tests/PythonScriptRunner.EntityFrameworkCore.Tests.csproj" />
20+
<Project Path="tests/PythonScriptRunner.Panel.E2E.Tests/PythonScriptRunner.Panel.E2E.Tests.csproj" />
2021
<Project Path="tests/PythonScriptRunner.RabbitMQ.Tests/PythonScriptRunner.RabbitMQ.Tests.csproj" />
2122
<Project Path="tests/PythonScriptRunner.Worker.Tests/PythonScriptRunner.Worker.Tests.csproj" />
2223
</Folder>

‎src/PythonScriptRunner.Core/PythonScriptRunner.Core.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<InternalsVisibleTo Include="PythonScriptRunner.RabbitMQ.Tests" />
2222
<InternalsVisibleTo Include="PythonScriptRunner.Worker.Tests" />
2323
<InternalsVisibleTo Include="PythonScriptRunner.Dashboard.Tests" />
24+
<InternalsVisibleTo Include="PythonScriptRunner.Panel.E2E.Tests" />
2425
</ItemGroup>
2526

2627
<ItemGroup>

‎src/PythonScriptRunner.RabbitMQ/PythonScriptRunner.RabbitMQ.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<InternalsVisibleTo Include="PythonScriptRunner.RabbitMQ.Tests" />
1414
<InternalsVisibleTo Include="PythonScriptRunner.Worker.Tests" />
1515
<InternalsVisibleTo Include="PythonScriptRunner.Dashboard.Tests" />
16+
<InternalsVisibleTo Include="PythonScriptRunner.Panel.E2E.Tests" />
1617
</ItemGroup>
1718

1819
<ItemGroup>
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System.Globalization;
2+
using System.Text.RegularExpressions;
3+
using PythonScriptRunner.Panel.E2E.Tests.Infrastructure;
4+
5+
namespace PythonScriptRunner.Panel.E2E.Tests;
6+
7+
/// <summary>Spec 008: fixed login, expired session, roles and theme in the real panel.</summary>
8+
public sealed partial class AccessTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python, BrowserFixture browser)
9+
: PanelTest(postgres, broker, python, browser)
10+
{
11+
[Fact]
12+
public async Task Spec008_AC17_FixedLogin_GoesToEntrar_RejectsAWrongPassword_ReturnsToTheRoute_AndLogsOut()
13+
{
14+
await StartAsync();
15+
var page = await OpenAsync();
16+
17+
await GoToAsync(page, "execucoes");
18+
await Expect(page).ToHaveURLAsync(App.UrlOf("entrar?volta=/execucoes"));
19+
var form = page.Locator("form");
20+
await form.FillLoginAsync(PanelUsers.Executor, "senha-errada");
21+
await Expect(page.GetByText("Usuário ou senha inválidos.")).ToBeVisibleAsync();
22+
await form.FillLoginAsync(null, PanelUsers.Password);
23+
24+
await Expect(page).ToHaveURLAsync(App.UrlOf("execucoes"));
25+
var menu = page.Locator(".v-navigation-drawer");
26+
await Expect(MenuItem(menu, "Execuções")).ToBeVisibleAsync();
27+
await Expect(MenuItem(menu, "Agendamentos")).ToBeVisibleAsync();
28+
await Expect(MenuItem(menu, "Auditoria")).ToHaveCountAsync(0);
29+
await Expect(MenuItem(menu, "Administração")).ToHaveCountAsync(0);
30+
31+
await page.GetByTestId("user-menu").ClickAsync();
32+
await page.GetByTestId("logout").ClickAsync();
33+
await Expect(page).ToHaveURLAsync(new Regex("/psr/entrar"));
34+
(await page.APIRequest.GetAsync(App.UrlOf("api/me"))).Status.ShouldBe(401);
35+
}
36+
37+
[Fact]
38+
public async Task Spec008_AC18_TheEleventhAttemptInAMinute_ShowsTheCountdown_AndTheLoginWorksWhenItEnds()
39+
{
40+
await StartAsync();
41+
var page = await OpenAsync();
42+
await GoToAsync(page, "entrar");
43+
var form = page.Locator("form");
44+
var submit = form.GetByRole(AriaRole.Button, new LocatorGetByRoleOptions { Name = "Entrar", Exact = true });
45+
46+
for (var i = 1; i <= 10; i++)
47+
{
48+
// Unknown users: the lockout of a user (5 failures) is not what this test is about.
49+
await form.FillLoginAsync($"ninguem{i}", "senha-errada");
50+
await Expect(form.GetByLabel("Senha")).ToHaveValueAsync(string.Empty);
51+
}
52+
53+
await form.FillLoginAsync(PanelUsers.Author, PanelUsers.Password);
54+
var countdown = page.GetByTestId("login-throttled");
55+
await Expect(countdown).ToContainTextAsync(CountdownText());
56+
await Expect(submit).ToBeDisabledAsync();
57+
var seconds = int.Parse(CountdownText().Match(await countdown.InnerTextAsync()).Groups[1].Value, CultureInfo.InvariantCulture);
58+
seconds.ShouldBeInRange(1, 60);
59+
await Expect(countdown).Not.ToContainTextAsync($"em {seconds} s");
60+
61+
await Expect(countdown).ToBeHiddenAsync(new LocatorAssertionsToBeHiddenOptions { Timeout = 75_000 });
62+
await Expect(submit).ToBeEnabledAsync();
63+
await submit.ClickAsync();
64+
await Expect(page).ToHaveURLAsync(App.Url.ToString());
65+
}
66+
67+
[Fact]
68+
public async Task Spec008_AC19_ExpiredSession_AsksTheSameUserInADialog_AndFinishesTheSave()
69+
{
70+
await StartAsync();
71+
await App.SaveStageDraftAsync("eco", PanelScripts.Echo, [Scripts.Text("texto")], [Scripts.Text("texto")]);
72+
var page = await OpenAsync(PanelUsers.Author);
73+
await GoToAsync(page, "etapas/eco/editar");
74+
await page.Locator("[data-testid=field-builder][data-kind=inputs]").GetByTestId("add-field").ClickAsync();
75+
await Expect(page.GetByTestId("dirty")).ToBeVisibleAsync();
76+
77+
// The session ends (the cookie is gone, as when it expires): the save answers 401.
78+
await page.Context.ClearCookiesAsync();
79+
await page.GetByTestId("save").ClickAsync();
80+
81+
var dialog = page.Locator(".v-dialog").Filter(new LocatorFilterOptions { HasText = "Sua sessão expirou" });
82+
await Expect(dialog).ToBeVisibleAsync();
83+
await Expect(dialog.GetByLabel("Usuário")).ToHaveValueAsync(PanelUsers.Author);
84+
await Expect(dialog.GetByLabel("Usuário")).Not.ToBeEditableAsync();
85+
await dialog.FillLoginAsync(null, PanelUsers.Password);
86+
87+
await Expect(dialog).ToBeHiddenAsync();
88+
await Expect(page.GetByTestId("dirty")).ToBeHiddenAsync();
89+
var saved = await App.Host.Admin.GetStageDraftAsync("eco", Ct);
90+
saved!.Data.Inputs.Select(f => f.Name).ShouldBe(["texto", "campo_2"]);
91+
}
92+
93+
[Fact]
94+
public async Task Spec008_AC10_Reader_SeesNoActionButtons_AndEditRoutesShowNoPermission()
95+
{
96+
await StartAsync(s => s.Worker = false);
97+
await App.ImportFixtureAsync("calculo-progressao");
98+
var run = await App.Host.Queue.EnqueuePipelineAsync("calculo-progressao", PanelScripts.Calculo, cancellationToken: Ct);
99+
var executor = await OpenAsync(PanelUsers.Executor);
100+
var reader = await OpenAsync(PanelUsers.Reader);
101+
102+
// What the reader must not see is there for the executor (no worker: the run stays queued and cancellable).
103+
await GoToAsync(executor, $"execucoes/{run.RunId}");
104+
await Expect(executor.GetByTestId("cancel-run")).ToBeVisibleAsync();
105+
await GoToAsync(executor, "trilhas/calculo-progressao");
106+
await Expect(executor.GetByTestId("run-pipeline")).ToBeVisibleAsync();
107+
108+
await GoToAsync(reader, $"execucoes/{run.RunId}");
109+
await Expect(reader.GetByTestId("run-status")).ToBeVisibleAsync();
110+
await Expect(reader.GetByTestId("cancel-run")).ToHaveCountAsync(0);
111+
await GoToAsync(reader, "trilhas/calculo-progressao");
112+
await Expect(reader.GetByTestId("versions-table")).ToBeVisibleAsync();
113+
await ExpectNoneAsync(reader, "run-pipeline", "edit-pipeline", "delete-pipeline");
114+
await GoToAsync(reader, "etapas");
115+
await Expect(reader.Locator("[data-key=progressao-termos]")).ToBeVisibleAsync();
116+
await ExpectNoneAsync(reader, "create", "import-zip");
117+
await GoToAsync(reader, "etapas/progressao-termos");
118+
await Expect(reader.GetByTestId("versions-table")).ToBeVisibleAsync();
119+
await ExpectNoneAsync(reader, "edit-stage", "delete-stage");
120+
await GoToAsync(reader, "agendamentos");
121+
await Expect(reader.GetByTestId("schedules-table")).ToBeVisibleAsync();
122+
await ExpectNoneAsync(reader, "create-schedule");
123+
124+
foreach (var path in new[] { "etapas/progressao-termos/editar", "trilhas/calculo-progressao/editar", "auditoria", "administracao" })
125+
{
126+
await GoToAsync(reader, path);
127+
await Expect(reader.GetByRole(AriaRole.Heading, new PageGetByRoleOptions { Name = "Sem permissão" })).ToBeVisibleAsync();
128+
}
129+
}
130+
131+
[Fact]
132+
public async Task Spec008_AC12_FollowsTheDarkSystemTheme_AndKeepsTheManualChoiceAfterReload()
133+
{
134+
await StartAsync();
135+
var page = await OpenAsync(PanelUsers.Reader, o => o.ColorScheme = ColorScheme.Dark);
136+
await GoToAsync(page, string.Empty);
137+
var app = page.Locator(".v-application");
138+
await Expect(app).ToHaveClassAsync(new Regex(@"\bv-theme--dark\b"));
139+
140+
await page.GetByTestId("theme-menu").ClickAsync();
141+
await page.GetByTestId("theme-light").ClickAsync();
142+
await Expect(app).ToHaveClassAsync(new Regex(@"\bv-theme--light\b"));
143+
await page.ReloadAsync();
144+
145+
await Expect(app).ToHaveClassAsync(new Regex(@"\bv-theme--light\b"));
146+
}
147+
148+
private static ILocator MenuItem(ILocator menu, string text) =>
149+
menu.GetByText(text, new LocatorGetByTextOptions { Exact = true });
150+
151+
private static async Task ExpectNoneAsync(IPage page, params string[] testIds)
152+
{
153+
foreach (var id in testIds)
154+
{
155+
await Expect(page.GetByTestId(id)).ToHaveCountAsync(0);
156+
}
157+
}
158+
159+
[GeneratedRegex(@"Tente de novo em (\d+) s")]
160+
private static partial Regex CountdownText();
161+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Deque.AxeCore.Playwright;
2+
using PythonScriptRunner.Panel.E2E.Tests.Infrastructure;
3+
4+
namespace PythonScriptRunner.Panel.E2E.Tests;
5+
6+
/// <summary>Spec 008, AC-15: axe-core finds no serious or critical violation on the login, a list, a form and a run.</summary>
7+
public sealed class AccessibilityTests(PostgresFixture postgres, BrokerFixture broker, PythonFixture python, BrowserFixture browser)
8+
: PanelTest(postgres, broker, python, browser)
9+
{
10+
[Fact]
11+
public async Task Spec008_AC15_LoginListFormAndRunDetail_HaveNoSeriousOrCriticalViolations()
12+
{
13+
await StartAsync();
14+
await App.ImportFixtureAsync("calculo-progressao");
15+
var run = await App.Host.Queue.EnqueuePipelineAsync("calculo-progressao", PanelScripts.Calculo, cancellationToken: Ct);
16+
await App.Host.WaitForRunAsync(run.RunId, timeout: TimeSpan.FromMinutes(3));
17+
var page = await OpenAsync();
18+
19+
await GoToAsync(page, "entrar");
20+
await Expect(page.GetByLabel("Senha")).ToBeVisibleAsync();
21+
await ExpectAccessibleAsync(page, "login");
22+
await page.Locator("form").FillLoginAsync(PanelUsers.Executor, PanelUsers.Password);
23+
await Expect(page).ToHaveURLAsync(App.Url.ToString());
24+
25+
await GoToAsync(page, "execucoes");
26+
await Expect(page.GetByTestId("runs-table").Locator("tbody tr").First).ToBeVisibleAsync();
27+
await ExpectAccessibleAsync(page, "lista de execuções");
28+
29+
await GoToAsync(page, "trilhas/calculo-progressao");
30+
await page.GetByTestId("run-pipeline").ClickAsync();
31+
await Expect(page.GetByTestId("run-dialog").Locator("[data-field=a1]").First).ToBeVisibleAsync();
32+
await ExpectAccessibleAsync(page, "formulário de disparo");
33+
34+
await GoToAsync(page, $"execucoes/{run.RunId}");
35+
await Expect(page.Locator("[data-testid=run-flow] [data-step]")).ToHaveCountAsync(4);
36+
await ExpectAccessibleAsync(page, "detalhe da execução");
37+
}
38+
39+
private static async Task ExpectAccessibleAsync(IPage page, string screen)
40+
{
41+
var result = await page.RunAxe();
42+
var violations = result.Violations
43+
.Where(v => v.Impact is "serious" or "critical")
44+
.Select(v => $"{v.Id} ({v.Impact}): {v.Help} — {string.Join(" | ", v.Nodes.Select(n => $"{n.Target} {n.Html} {string.Join(" ", n.Any.Concat(n.All).Concat(n.None).Select(c => c.Message))}"))}")
45+
.ToList();
46+
violations.ShouldBeEmpty($"{screen}:\n{string.Join("\n", violations)}");
47+
}
48+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[assembly: AssemblyFixture(typeof(PythonScriptRunner.Panel.E2E.Tests.Infrastructure.BrowserFixture))]
2+
3+
namespace PythonScriptRunner.Panel.E2E.Tests.Infrastructure;
4+
5+
/// <summary>
6+
/// One Chromium for the whole run (each test opens its own contexts). Installs the browser when missing; CI installs it
7+
/// beforehand with <c>playwright.ps1 install --with-deps chromium</c>. <c>PSR_E2E_HEADED=1</c> shows the browser.
8+
/// </summary>
9+
public sealed class BrowserFixture : IAsyncLifetime
10+
{
11+
private IPlaywright? _playwright;
12+
13+
public IBrowser Browser { get; private set; } = null!;
14+
15+
public async ValueTask InitializeAsync()
16+
{
17+
var code = Microsoft.Playwright.Program.Main(["install", "chromium"]);
18+
if (code != 0)
19+
{
20+
throw new InvalidOperationException($"Installing Chromium for Playwright failed (exit code {code}).");
21+
}
22+
23+
// Live updates and Python runs take longer than the 5 s default of the web assertions.
24+
SetDefaultExpectTimeout(20_000);
25+
_playwright = await Playwright.CreateAsync();
26+
Browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
27+
{
28+
Headless = Environment.GetEnvironmentVariable("PSR_E2E_HEADED") != "1",
29+
});
30+
}
31+
32+
public async ValueTask DisposeAsync()
33+
{
34+
if (Browser is not null)
35+
{
36+
await Browser.DisposeAsync();
37+
}
38+
39+
_playwright?.Dispose();
40+
}
41+
}

0 commit comments

Comments
 (0)