1616
1717package com .microsoft .playwright ;
1818
19- import com .microsoft .playwright .BrowserType .LaunchOptions ;
20- import com .microsoft .playwright .impl .driver .Driver ;
2119import com .microsoft .playwright .options .AriaRole ;
2220import com .microsoft .playwright .options .Location ;
2321import com .microsoft .playwright .options .MouseButton ;
2725import org .junit .jupiter .api .Test ;
2826import org .junit .jupiter .api .io .TempDir ;
2927
30- import java .io .File ;
3128import java .io .IOException ;
3229import java .nio .file .Files ;
3330import java .nio .file .Path ;
@@ -63,7 +60,7 @@ void shouldCollectTrace1(@TempDir Path tempDir) throws Exception {
6360 context .tracing ().stop (new Tracing .StopOptions ().setPath (traceFile ));
6461
6562 assertTrue (Files .exists (traceFile ));
66- showTraceViewer (traceFile , traceViewer -> {
63+ TraceViewerPage . showTraceViewer (this . browserType , traceFile , traceViewer -> {
6764 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
6865 Pattern .compile ("Navigate to \" /empty.html\" " ),
6966 Pattern .compile ("Set content" ),
@@ -93,15 +90,15 @@ void shouldCollectTwoTraces(@TempDir Path tempDir) throws Exception {
9390 assertTrue (Files .exists (traceFile1 ));
9491 assertTrue (Files .exists (traceFile2 ));
9592
96- showTraceViewer (traceFile1 , traceViewer -> {
93+ TraceViewerPage . showTraceViewer (this . browserType , traceFile1 , traceViewer -> {
9794 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
9895 Pattern .compile ("Navigate to \" /empty.html\" " ),
9996 Pattern .compile ("Set content" ),
10097 Pattern .compile ("Click" )
10198 });
10299 });
103100
104- showTraceViewer (traceFile2 , traceViewer -> {
101+ TraceViewerPage . showTraceViewer (this . browserType , traceFile2 , traceViewer -> {
105102 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
106103 Pattern .compile ("Double click" ),
107104 Pattern .compile ("Close" )
@@ -128,7 +125,7 @@ void shouldWorkWithMultipleChunks(@TempDir Path tempDir) throws Exception {
128125 assertTrue (Files .exists (traceFile1 ));
129126 assertTrue (Files .exists (traceFile2 ));
130127
131- showTraceViewer (traceFile1 , traceViewer -> {
128+ TraceViewerPage . showTraceViewer (this . browserType , traceFile1 , traceViewer -> {
132129 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
133130 Pattern .compile ("Set content" ),
134131 Pattern .compile ("Click" )
@@ -138,7 +135,7 @@ void shouldWorkWithMultipleChunks(@TempDir Path tempDir) throws Exception {
138135 assertThat (frame .locator ("button" )).hasText ("Click" );
139136 });
140137
141- showTraceViewer (traceFile2 , traceViewer -> {
138+ TraceViewerPage . showTraceViewer (this . browserType , traceFile2 , traceViewer -> {
142139 assertThat (traceViewer .actionTitles ()).containsText (new String [] {"Hover" });
143140 FrameLocator frame = traceViewer .snapshotFrame ("Hover" , 0 , false );
144141 assertThat (frame .locator ("button" )).hasText ("Click" );
@@ -155,7 +152,7 @@ void shouldCollectSources(@TempDir Path tmpDir) throws Exception {
155152 Path trace = tmpDir .resolve ("trace1.zip" );
156153 context .tracing ().stop (new Tracing .StopOptions ().setPath (trace ));
157154
158- showTraceViewer (trace , traceViewer -> {
155+ TraceViewerPage . showTraceViewer (this . browserType , trace , traceViewer -> {
159156 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
160157 Pattern .compile ("Navigate to \" /empty.html\" " ),
161158 Pattern .compile ("Set content" ),
@@ -210,7 +207,7 @@ void shouldRespectTracesDirAndName(@TempDir Path tempDir) throws Exception {
210207 assertTrue (Files .exists (tracesDir .resolve ("name2.trace" )));
211208 assertTrue (Files .exists (tracesDir .resolve ("name2.network" )));
212209
213- showTraceViewer (tempDir .resolve ("trace1.zip" ), traceViewer -> {
210+ TraceViewerPage . showTraceViewer (this . browserType , tempDir .resolve ("trace1.zip" ), traceViewer -> {
214211 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
215212 Pattern .compile ("Navigate to \" /one-style.html\" " )
216213 });
@@ -219,7 +216,7 @@ void shouldRespectTracesDirAndName(@TempDir Path tempDir) throws Exception {
219216 assertThat (frame .locator ("body" )).hasText ("hello, world!" );
220217 });
221218
222- showTraceViewer (tempDir .resolve ("trace2.zip" ), traceViewer -> {
219+ TraceViewerPage . showTraceViewer (this . browserType , tempDir .resolve ("trace2.zip" ), traceViewer -> {
223220 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
224221 Pattern .compile ("Navigate to \" /har.html\" " )
225222 });
@@ -247,7 +244,7 @@ void canCallTracingGroupGroupEndAtAnyTimeAndAutoClose(@TempDir Path tempDir) thr
247244 context .tracing ().groupEnd ();
248245 context .tracing ().groupEnd ();
249246
250- showTraceViewer (traceFile1 , traceViewer -> {
247+ TraceViewerPage . showTraceViewer (this . browserType , traceFile1 , traceViewer -> {
251248 assertThat (traceViewer .actionTitles ()).containsText (new String [] {"actual" , "Navigate to \" /empty.html\" " });
252249 });
253250 }
@@ -268,7 +265,7 @@ void traceGroupGroupEnd(@TempDir Path tempDir) throws Exception {
268265 Path traceFile1 = tempDir .resolve ("trace1.zip" );
269266 context .tracing ().stop (new Tracing .StopOptions ().setPath (traceFile1 ));
270267
271- showTraceViewer (traceFile1 , traceViewer -> {
268+ TraceViewerPage . showTraceViewer (this . browserType , traceFile1 , traceViewer -> {
272269 traceViewer .expandAction ("inner group 1" );
273270 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
274271 Pattern .compile ("outer group" ),
@@ -313,7 +310,7 @@ void shouldTraceVariousAPIs(@TempDir Path tempDir) throws Exception {
313310 Path traceFile1 = tempDir .resolve ("trace1.zip" );
314311 context .tracing ().stop (new Tracing .StopOptions ().setPath (traceFile1 ));
315312
316- showTraceViewer (traceFile1 , traceViewer -> {
313+ TraceViewerPage . showTraceViewer (this . browserType , traceFile1 , traceViewer -> {
317314 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
318315 Pattern .compile ("Install clock" ),
319316 Pattern .compile ("Set content" ),
@@ -356,105 +353,10 @@ public void shouldNotRecordNetworkActions(@TempDir Path tempDir) throws Exceptio
356353 Path traceFile1 = tempDir .resolve ("trace1.zip" );
357354 context .tracing ().stop (new Tracing .StopOptions ().setPath (traceFile1 ));
358355
359- showTraceViewer (traceFile1 , traceViewer -> {
356+ TraceViewerPage . showTraceViewer (this . browserType , traceFile1 , traceViewer -> {
360357 assertThat (traceViewer .actionTitles ()).hasText (new Pattern [] {
361358 Pattern .compile ("Navigate to \" /empty.html\" " )
362359 });
363360 });
364361 }
365-
366- private void showTraceViewer (Path tracePath , TraceViewerConsumer callback ) throws Exception {
367- Path driverDir = Driver .ensureDriverInstalled (java .util .Collections .emptyMap (), true ).driverDir ();
368- Path traceViewerPath = driverDir .resolve ("package" ).resolve ("lib" ).resolve ("vite" ).resolve ("traceViewer" );
369- Server traceServer = Server .createHttp (Utils .nextFreePort ());
370- traceServer .setResourceProvider (path -> {
371- Path filePath = traceViewerPath .resolve (path .substring (1 ));
372- if (Files .exists (filePath ) && !Files .isDirectory (filePath )) {
373- try {
374- return Files .newInputStream (filePath );
375- } catch (IOException e ) {
376- return null ;
377- }
378- }
379- return null ;
380- });
381- traceServer .setRoute ("/trace.zip" , exchange -> {
382- exchange .getResponseHeaders ().add ("Content-Type" , "application/zip" );
383- exchange .sendResponseHeaders (200 , Files .size (tracePath ));
384- Files .copy (tracePath , exchange .getResponseBody ());
385- exchange .getResponseBody ().close ();
386- });
387-
388- try (Browser browser = browserType .launch (createLaunchOptions ());
389- BrowserContext context = browser .newContext ()) {
390- Page page = context .newPage ();
391- page .navigate (traceServer .PREFIX + "/index.html?trace=" + traceServer .PREFIX + "/trace.zip" );
392-
393- TraceViewerPage traceViewer = new TraceViewerPage (page );
394- callback .accept (traceViewer );
395- } finally {
396- traceServer .stop ();
397- }
398- }
399-
400- @ FunctionalInterface
401- interface TraceViewerConsumer {
402- void accept (TraceViewerPage traceViewer ) throws Exception ;
403- }
404- }
405-
406- class TraceViewerPage {
407- private final Page page ;
408-
409- public TraceViewerPage (Page page ) {
410- this .page = page ;
411- }
412-
413- public Page page () {
414- return page ;
415- }
416-
417- public Locator actionsTree () {
418- return page .getByTestId ("actions-tree" );
419- }
420-
421- public Locator actionTitles () {
422- return page .locator (".action-title" );
423- }
424-
425- public Locator stackFrames () {
426- return this .page .getByRole (AriaRole .LIST , new Page .GetByRoleOptions ().setName ("stack trace" )).getByRole (AriaRole .LISTITEM );
427- }
428-
429- public void selectAction (String title , int ordinal ) {
430- this .actionsTree ().getByTitle (title ).nth (ordinal ).click ();
431- }
432-
433- public void selectAction (String title ) {
434- selectAction (title , 0 );
435- }
436-
437- public void selectSnapshot (String name ) {
438- this .page .getByRole (AriaRole .TAB , new Page .GetByRoleOptions ().setName (name )).click ();
439- }
440-
441- public FrameLocator snapshotFrame (String actionName , int ordinal , boolean hasSubframe ) {
442- selectAction (actionName , ordinal );
443- while (page .frames ().size () < (hasSubframe ? 4 : 3 )) {
444- page .waitForTimeout (200 );
445- }
446- return page .frameLocator ("iframe.snapshot-visible[name=snapshot]" );
447- }
448-
449- public FrameLocator snapshotFrame (String actionName , int ordinal ) {
450- return snapshotFrame (actionName , ordinal , false );
451- }
452-
453- public void showSourceTab () {
454- page .getByRole (AriaRole .TAB , new Page .GetByRoleOptions ().setName ("Source" )).click ();
455- }
456-
457- public void expandAction (String title ) {
458- this .actionsTree ().getByRole (AriaRole .TREEITEM , new Locator .GetByRoleOptions ().setName (title )).locator (".codicon-chevron-right" ).click ();
459- }
460362}
0 commit comments