How to make it easier for UI test engineers to take a dependency on UI elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[HtmlTargetElement(Attributes = "test-id")]
public class TestIdHelper : TagHelper
{
private readonly bool _displayTestId;
public TestIdHelper(IConfiguration configuration)
{
bool.TryParse(configuration["IsTesting"], out bool isTesting);
_displayTestId = isTesting;
}
// PascalCase gets translated into kebab-case.
public string TestId { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.Attributes.RemoveAll("test-id");
if(_displayTestId)
{
output.Attributes.SetAttribute("tId", TestId);
}
}
}
Example of use
1
<li><asp-controller="Manage" asp-action="Index" test-id="mnuManage">@Localizer["Dashboard"]</a></li>