44import com .microsoft .playwright .junit .UsePlaywright ;
55import org .junit .jupiter .api .Test ;
66
7+ import java .util .Arrays ;
8+ import java .util .List ;
9+ import java .util .regex .Matcher ;
10+ import java .util .regex .Pattern ;
11+ import java .util .stream .Collectors ;
12+
713import static com .microsoft .playwright .assertions .PlaywrightAssertions .assertThat ;
814import static org .junit .jupiter .api .Assertions .assertEquals ;
915
1016@ FixtureTest
1117@ UsePlaywright
1218public class TestPageAriaSnapshot {
19+ public static String unshift (String snapshot ) {
20+ List <String > lines = Arrays .asList (snapshot .split ("\n " ));
21+ int whitespacePrefixLength = 100 ;
22+ Pattern pattern = Pattern .compile ("^(\\ s*).*" );
23+ for (String line : lines ) {
24+ if (line .trim ().isEmpty ())
25+ continue ;
26+ Matcher matcher = pattern .matcher (line );
27+ if (!matcher .matches ()) {
28+ continue ;
29+ }
30+ String match = matcher .group (1 );
31+ if (match .length () < whitespacePrefixLength ) {
32+ whitespacePrefixLength = match .length ();
33+ }
34+ }
35+ final int prefixLength = whitespacePrefixLength ;
36+ return lines .stream ()
37+ .filter (line -> !line .trim ().isEmpty ())
38+ .map (line -> line .substring (prefixLength ))
39+ .collect (Collectors .joining ("\n " ));
40+ }
41+
1342 private static void checkAndMatchSnapshot (Locator locator , String snapshot ) {
14- assertEquals (snapshot , locator .ariaSnapshot ());
43+ assertEquals (unshift ( snapshot ) , locator .ariaSnapshot ());
1544 assertThat (locator ).matchesAriaSnapshot (snapshot );
1645 }
1746
@@ -24,7 +53,9 @@ void shouldSnapshot(Page page) {
2453 @ Test
2554 void shouldSnapshotList (Page page ) {
2655 page .setContent ("<h1>title</h1><h1>title 2</h1>" );
27- checkAndMatchSnapshot (page .locator ("body" ), "- heading \" title\" [level=1]\n - heading \" title 2\" [level=1]" );
56+ checkAndMatchSnapshot (page .locator ("body" ), "" +
57+ " - heading \" title\" [level=1]\n " +
58+ " - heading \" title 2\" [level=1]" );
2859 }
2960
3061 @ Test
@@ -42,7 +73,8 @@ void shouldSnapshotComplex(Page page) {
4273 @ Test
4374 void shouldAllowTextNodes (Page page ) {
4475 page .setContent ("<h1>Microsoft</h1><div>Open source projects and samples from Microsoft</div>" );
45- checkAndMatchSnapshot (page .locator ("body" ), "- heading \" Microsoft\" [level=1]\n - text: Open source projects and samples from Microsoft" );
76+ checkAndMatchSnapshot (page .locator ("body" ), "- heading \" Microsoft\" [level=1]\n " +
77+ "- text: Open source projects and samples from Microsoft" );
4678 }
4779
4880 @ Test
0 commit comments