Files
recordingtest/tests/Recordingtest.Recorder.Tests/RecorderTests.cs
2026-04-07 14:27:46 +09:00

142 lines
4.0 KiB
C#

using System;
using System.IO;
using Xunit;
namespace Recordingtest.Recorder.Tests;
public class RecorderTests
{
[Fact]
public void ElementPathBuilder_WithNestedElements_ReturnsFullPath()
{
var window = new FakeElement
{
ClassName = "Window",
Name = "Main",
};
var panel = new FakeElement
{
ClassName = "StackPanel",
AutomationId = "ToolStrip",
Parent = window,
};
var button = new FakeElement
{
ClassName = "Button",
AutomationId = "BoxCommand",
Parent = panel,
};
var path = ElementPathBuilder.Build(button);
Assert.Equal(
"Window[@Name='Main']/StackPanel[@AutomationId='ToolStrip']/Button[@AutomationId='BoxCommand']",
path);
}
[Fact]
public void OffsetNormalizer_ClicksInsideElement_ReturnsZeroToOne()
{
var bounds = (Left: 100.0, Top: 200.0, Width: 400.0, Height: 200.0);
var (dx, dy) = OffsetNormalizer.Normalize(bounds, 300, 300);
Assert.Equal(0.5, dx, 6);
Assert.Equal(0.5, dy, 6);
var (dx2, dy2) = OffsetNormalizer.Normalize(bounds, 100, 200);
Assert.Equal(0.0, dx2, 6);
Assert.Equal(0.0, dy2, 6);
// Out-of-bounds clamps into [0,1]
var (dx3, dy3) = OffsetNormalizer.Normalize(bounds, 9999, -9999);
Assert.InRange(dx3, 0.0, 1.0);
Assert.InRange(dy3, 0.0, 1.0);
}
[Fact]
public void FocusedElementIsPassword_ReturnsMasked()
{
var pwd = new FakeElement
{
ClassName = "PasswordBox",
IsPassword = true,
};
var value = MaskPolicy.Apply(pwd, "supersecret");
Assert.Equal("<MASKED>", value);
Assert.True(MaskPolicy.IsMasked(pwd));
var plain = new FakeElement { ClassName = "TextBox" };
Assert.Equal("hello", MaskPolicy.Apply(plain, "hello"));
Assert.False(MaskPolicy.IsMasked(plain));
}
[Fact]
public void YamlSerializer_RoundtripsScenario()
{
var s = new Scenario
{
Name = "smoke",
Description = "round trip",
Sut = new ScenarioSut
{
Exe = "EG-BIM Modeler/EG-BIM Modeler.exe",
StartupTimeoutMs = 15000,
},
Steps =
{
new ScenarioStep
{
Kind = "click",
Target = new ScenarioTarget
{
UiaPath = "Window[@Name='Main']/Button[@AutomationId='BoxCommand']",
Offset = new[] { 0.5, 0.5 },
},
Value = null,
WaitFor = null,
},
new ScenarioStep
{
Kind = "type",
Target = new ScenarioTarget
{
UiaPath = "Window[@Name='Main']/Edit[@AutomationId='Pwd']",
Offset = new[] { 0.5, 0.5 },
},
Value = "<MASKED>",
},
},
};
var yaml = ScenarioWriter.Serialize(s);
var parsed = ScenarioWriter.Deserialize(yaml);
Assert.Equal(s.Name, parsed.Name);
Assert.Equal(s.Description, parsed.Description);
Assert.Equal(s.Sut.Exe, parsed.Sut.Exe);
Assert.Equal(s.Sut.StartupTimeoutMs, parsed.Sut.StartupTimeoutMs);
Assert.Equal(s.Steps.Count, parsed.Steps.Count);
Assert.Equal(s.Steps[0].Target!.UiaPath, parsed.Steps[0].Target!.UiaPath);
Assert.Equal(s.Steps[1].Value, parsed.Steps[1].Value);
}
[Fact]
public void Cli_MissingAttach_ExitTwo()
{
var stderr = Console.Error;
try
{
Console.SetError(new StringWriter());
var rc = Program.Main(new[] { "--output", "scenarios/x.yaml" });
Assert.Equal(2, rc);
}
finally
{
Console.SetError(stderr);
}
}
}