20
20
import com .google .auto .service .AutoService ;
21
21
import com .google .common .collect .ImmutableMap ;
22
22
import org .openqa .selenium .Capabilities ;
23
+ import org .openqa .selenium .WebDriver ;
24
+ import org .openqa .selenium .WebDriverException ;
25
+ import org .openqa .selenium .WrapsDriver ;
23
26
import org .openqa .selenium .internal .Require ;
27
+ import org .openqa .selenium .io .Zip ;
24
28
import org .openqa .selenium .remote .AdditionalHttpCommands ;
25
29
import org .openqa .selenium .remote .AugmenterProvider ;
26
30
import org .openqa .selenium .remote .CommandInfo ;
27
31
import org .openqa .selenium .remote .ExecuteMethod ;
32
+ import org .openqa .selenium .remote .RemoteWebDriver ;
28
33
import org .openqa .selenium .remote .http .HttpMethod ;
29
34
35
+ import java .io .File ;
36
+ import java .io .IOException ;
30
37
import java .nio .file .Path ;
31
38
import java .util .Map ;
32
39
import java .util .function .Predicate ;
33
40
34
41
import static org .openqa .selenium .remote .Browser .FIREFOX ;
42
+ import static org .openqa .selenium .remote .DriverCommand .UPLOAD_FILE ;
35
43
36
44
@ AutoService ({AdditionalHttpCommands .class , AugmenterProvider .class })
37
45
public class AddHasExtensions implements AugmenterProvider <HasExtensions >, AdditionalHttpCommands {
@@ -65,9 +73,23 @@ public HasExtensions getImplementation(Capabilities capabilities, ExecuteMethod
65
73
public String installExtension (Path path ) {
66
74
Require .nonNull ("Extension Path" , path );
67
75
68
- return (String ) executeMethod .execute (
69
- INSTALL_EXTENSION ,
70
- ImmutableMap .of ("path" , path .toAbsolutePath ().toString (), "temporary" , false ));
76
+ if (executeMethod instanceof WrapsDriver ) {
77
+ WebDriver wrapped = ((WrapsDriver ) executeMethod ).getWrappedDriver ();
78
+ if (wrapped instanceof RemoteWebDriver ) {
79
+ File localFile = ((RemoteWebDriver ) wrapped ).getFileDetector ().getLocalFile (path .toString ());
80
+ if (localFile != null ) {
81
+ try {
82
+ String zip = Zip .zip (localFile );
83
+ String newPath = (String ) executeMethod .execute (UPLOAD_FILE , ImmutableMap .of ("file" , zip ));
84
+ return installExtensionAtPath (newPath );
85
+ } catch (IOException e ) {
86
+ throw new WebDriverException ("Cannot upload " + localFile , e );
87
+ }
88
+ }
89
+ }
90
+ }
91
+
92
+ return installExtensionAtPath (path .toString ());
71
93
}
72
94
73
95
@ Override
@@ -76,6 +98,12 @@ public void uninstallExtension(String extensionId) {
76
98
77
99
executeMethod .execute (UNINSTALL_EXTENSION , ImmutableMap .of ("id" , extensionId ));
78
100
}
101
+
102
+ private String installExtensionAtPath (String path ) {
103
+ return (String ) executeMethod .execute (
104
+ INSTALL_EXTENSION ,
105
+ ImmutableMap .of ("path" , path , "temporary" , false ));
106
+ }
79
107
};
80
108
}
81
109
}
0 commit comments