21
21
import static java .util .concurrent .TimeUnit .SECONDS ;
22
22
import static org .assertj .core .api .Assertions .assertThat ;
23
23
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
24
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
24
25
import static org .assertj .core .api .InstanceOfAssertFactories .MAP ;
25
26
import static org .openqa .selenium .json .Json .MAP_TYPE ;
26
27
import static org .openqa .selenium .remote .http .Contents .string ;
31
32
import com .google .common .collect .ImmutableSet ;
32
33
33
34
import java .nio .file .Path ;
35
+ import java .util .List ;
36
+ import java .util .Optional ;
34
37
import org .junit .jupiter .api .BeforeEach ;
38
+ import org .junit .jupiter .api .DisplayName ;
35
39
import org .junit .jupiter .api .Test ;
40
+ import org .junit .jupiter .api .TestInfo ;
36
41
import org .openqa .selenium .Capabilities ;
37
42
import org .openqa .selenium .ImmutableCapabilities ;
38
43
import org .openqa .selenium .NoSuchSessionException ;
49
54
import org .openqa .selenium .grid .data .Session ;
50
55
import org .openqa .selenium .grid .data .SessionClosedEvent ;
51
56
import org .openqa .selenium .grid .node .local .LocalNode ;
57
+ import org .openqa .selenium .grid .node .local .LocalNode .Builder ;
52
58
import org .openqa .selenium .grid .node .remote .RemoteNode ;
53
59
import org .openqa .selenium .grid .security .Secret ;
54
60
import org .openqa .selenium .grid .testing .EitherAssert ;
@@ -106,7 +112,7 @@ private static <A, B> EitherAssert<A, B> assertThatEither(Either<A, B> either) {
106
112
}
107
113
108
114
@ BeforeEach
109
- public void setUp () throws URISyntaxException {
115
+ public void setUp (TestInfo testInfo ) throws URISyntaxException {
110
116
tracer = DefaultTestTracer .createTracer ();
111
117
bus = new GuavaEventBus ();
112
118
@@ -129,13 +135,15 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
129
135
}
130
136
}
131
137
132
- local = LocalNode .builder (tracer , bus , uri , uri , registrationSecret )
133
- .add (caps , new TestSessionFactory ((id , c ) -> new Handler (c )))
134
- .add (caps , new TestSessionFactory ((id , c ) -> new Handler (c )))
135
- .add (caps , new TestSessionFactory ((id , c ) -> new Handler (c )))
136
- .downloadsPath (downloadsPath .getAbsolutePath ())
137
- .maximumConcurrentSessions (2 )
138
- .build ();
138
+ Builder builder = LocalNode .builder (tracer , bus , uri , uri , registrationSecret )
139
+ .add (caps , new TestSessionFactory ((id , c ) -> new Handler (c )))
140
+ .add (caps , new TestSessionFactory ((id , c ) -> new Handler (c )))
141
+ .add (caps , new TestSessionFactory ((id , c ) -> new Handler (c )))
142
+ .maximumConcurrentSessions (2 );
143
+ if (!testInfo .getDisplayName ().equalsIgnoreCase ("BootWithoutDownloadsDir" )) {
144
+ builder = builder .downloadsPath (downloadsPath .getAbsolutePath ());
145
+ }
146
+ local = builder .build ();
139
147
140
148
node = new RemoteNode (
141
149
tracer ,
@@ -495,20 +503,111 @@ void canDownloadAFile() throws IOException {
495
503
Session session = response .right ().getSession ();
496
504
String hello = "Hello, world!" ;
497
505
498
- HttpRequest req = new HttpRequest (GET , String .format ("/session/%s/se/file " , session .getId ()));
506
+ HttpRequest req = new HttpRequest (POST , String .format ("/session/%s/se/files " , session .getId ()));
499
507
File zip = createTmpFile (hello );
500
- req .addQueryParameter ("filename" , zip .getName ());
508
+ String payload = new Json ().toJson (Collections .singletonMap ("filename" , zip .getName ()));
509
+ req .setContent (() -> new ByteArrayInputStream (payload .getBytes ()));
501
510
HttpResponse rsp = node .execute (req );
511
+ Map <String , Object > raw = new Json ().toType (string (rsp ), Json .MAP_TYPE );
502
512
node .stop (session .getId ());
503
- Map < String , Object > map = new Json (). toType ( string ( rsp ), Json . MAP_TYPE );
513
+ assertThat ( raw ). isNotNull ( );
504
514
File baseDir = getTemporaryFilesystemBaseDir (TemporaryFilesystem .getDefaultTmpFS ());
515
+ Map <String , Object > map = Optional .ofNullable (
516
+ raw .get ("value" )
517
+ ).map (data -> (Map <String , Object >) data )
518
+ .orElseThrow (() -> new IllegalStateException ("Could not find value attribute" ));
505
519
String encodedContents = map .get ("contents" ).toString ();
506
520
Zip .unzip (encodedContents , baseDir );
507
521
Path path = new File (baseDir .getAbsolutePath () + "/" + map .get ("filename" )).toPath ();
508
522
String decodedContents = String .join ("" , Files .readAllLines (path ));
509
523
assertThat (decodedContents ).isEqualTo (hello );
510
524
}
511
525
526
+ @ Test
527
+ void canListFilesToDownload () {
528
+ Either <WebDriverException , CreateSessionResponse > response =
529
+ node .newSession (createSessionRequest (caps ));
530
+ assertThatEither (response ).isRight ();
531
+ Session session = response .right ().getSession ();
532
+ String hello = "Hello, world!" ;
533
+ File zip = createTmpFile (hello );
534
+ HttpRequest req = new HttpRequest (GET , String .format ("/session/%s/se/files" , session .getId ()));
535
+ HttpResponse rsp = node .execute (req );
536
+ Map <String , Object > raw = new Json ().toType (string (rsp ), Json .MAP_TYPE );
537
+ node .stop (session .getId ());
538
+ assertThat (raw ).isNotNull ();
539
+ Map <String , Object > map = Optional .ofNullable (
540
+ raw .get ("value" )
541
+ ).map (data -> (Map <String , Object >) data )
542
+ .orElseThrow (() -> new IllegalStateException ("Could not find value attribute" ));
543
+ List <String > files = (List <String >) map .get ("filenames" );
544
+ assertThat (files ).contains (zip .getName ());
545
+ }
546
+
547
+ @ Test
548
+ @ DisplayName ("BootWithoutDownloadsDir" )
549
+ void canDownloadFileThrowsErrorMsgWhenDownloadsDirNotSpecified () {
550
+ Either <WebDriverException , CreateSessionResponse > response =
551
+ node .newSession (createSessionRequest (caps ));
552
+ assertThatEither (response ).isRight ();
553
+ Session session = response .right ().getSession ();
554
+ createTmpFile ("Hello, world!" );
555
+
556
+ HttpRequest req = new HttpRequest (POST , String .format ("/session/%s/se/files" , session .getId ()));
557
+ String msg = "Please specify the path wherein the files downloaded using the browser "
558
+ + "would be available via the command line arg [--downloads-path] and restart the node" ;
559
+ assertThatThrownBy (() -> node .execute (req ))
560
+ .hasMessageContaining (msg );
561
+ }
562
+
563
+ @ Test
564
+ void canDownloadFileThrowsErrorMsgWhenPayloadIsMissing () {
565
+ Either <WebDriverException , CreateSessionResponse > response =
566
+ node .newSession (createSessionRequest (caps ));
567
+ assertThatEither (response ).isRight ();
568
+ Session session = response .right ().getSession ();
569
+ createTmpFile ("Hello, world!" );
570
+
571
+ HttpRequest req = new HttpRequest (POST , String .format ("/session/%s/se/files" , session .getId ()));
572
+ String msg = "Please specify file to download in payload as {\" filename\" : \" fileToDownload\" }" ;
573
+ assertThatThrownBy (() -> node .execute (req ))
574
+ .hasMessageContaining (msg );
575
+ }
576
+
577
+ @ Test
578
+ void canDownloadFileThrowsErrorMsgWhenWrongPayloadIsGiven () {
579
+ Either <WebDriverException , CreateSessionResponse > response =
580
+ node .newSession (createSessionRequest (caps ));
581
+ assertThatEither (response ).isRight ();
582
+ Session session = response .right ().getSession ();
583
+ createTmpFile ("Hello, world!" );
584
+
585
+ HttpRequest req = new HttpRequest (POST , String .format ("/session/%s/se/files" , session .getId ()));
586
+ String payload = new Json ().toJson (Collections .singletonMap ("my-file" , "README.md" ));
587
+ req .setContent (() -> new ByteArrayInputStream (payload .getBytes ()));
588
+
589
+ String msg = "Please specify file to download in payload as {\" filename\" : \" fileToDownload\" }" ;
590
+ assertThatThrownBy (() -> node .execute (req ))
591
+ .hasMessageContaining (msg );
592
+ }
593
+
594
+ @ Test
595
+ void canDownloadFileThrowsErrorMsgWhenFileNotFound () {
596
+ Either <WebDriverException , CreateSessionResponse > response =
597
+ node .newSession (createSessionRequest (caps ));
598
+ assertThatEither (response ).isRight ();
599
+ Session session = response .right ().getSession ();
600
+ createTmpFile ("Hello, world!" );
601
+
602
+ HttpRequest req = new HttpRequest (POST , String .format ("/session/%s/se/files" , session .getId ()));
603
+ String payload = new Json ().toJson (Collections .singletonMap ("filename" , "README.md" ));
604
+ req .setContent (() -> new ByteArrayInputStream (payload .getBytes ()));
605
+
606
+ String msg = "Cannot find file [README.md] in directory" ;
607
+ assertThatThrownBy (() -> node .execute (req ))
608
+ .hasMessageContaining (msg );
609
+ }
610
+
512
611
@ Test
513
612
void shouldNotCreateSessionIfDraining () {
514
613
node .drain ();
0 commit comments