1
1
package com .github .markusbernhardt .seleniumlibrary .keywords ;
2
2
3
3
import io .appium .java_client .ios .IOSDriver ;
4
+ import io .github .bonigarcia .wdm .DriverManagerType ;
5
+ import io .github .bonigarcia .wdm .WebDriverManager ;
4
6
import io .selendroid .client .SelendroidDriver ;
5
7
6
8
import java .io .File ;
10
12
import java .net .URL ;
11
13
import java .net .UnknownHostException ;
12
14
import java .util .ArrayList ;
13
- import java .util .Iterator ;
14
15
import java .util .List ;
15
16
import java .util .Map .Entry ;
16
17
import java .util .concurrent .TimeUnit ;
@@ -166,13 +167,15 @@ public void closeBrowser() {
166
167
"| Iphone | iphone |\r \n " +
167
168
"| JBrowser | jbrowser |\r \n " +
168
169
"\r \n " +
169
- "To be able to actually use one of these browsers, you need to have a matching Selenium browser driver available. See the [https://github.com/Hi-Fi/robotframework-seleniumlibrary-java#browser-drivers|project documentation] for more details.\r \n " +
170
+ "To be able to actually use one of these browsers, you need to have a matching Selenium browser driver available. See the [https://github.com/Hi-Fi/robotframework-seleniumlibrary-java#browser-drivers|project documentation] for more details.\r \n " +
170
171
"\r \n " +
171
172
"Optional ``alias`` is an alias given for this browser instance and it can be used for switching between browsers. An alternative approach for switching is using an index returned by this keyword. These indices start from 1, are incremented when new browsers are opened, and reset back to 1 when `Close All Browsers` is called. See `Switch Browser` for more information and examples.\r \n " +
172
173
"\r \n " +
173
- "Optional ``remote_url`` is the URL for a remote Selenium server. If you specify a value for a remote, you can also specify ``desired_capabilities`` to configure, for example, a proxy server for Internet Explorer or a browser and operating system when using [http://saucelabs.com|Sauce Labs]. Desired capabilities can be given as a dictionary. [https://github.com/SeleniumHQ/selenium/wiki/Capabilities| Selenium documentation] lists possible capabilities that can be enabled.\r \n " +
174
+ "Optional ``remote_url`` is the URL for a remote Selenium server. If you specify a value for a remote, you can also specify ``desired_capabilities`` to configure, for example, a proxy server for Internet Explorer or a browser and operating system when using [http://saucelabs.com|Sauce Labs]. Desired capabilities can be given as a dictionary. [https://github.com/SeleniumHQ/selenium/wiki/Capabilities| Selenium documentation] lists possible capabilities that can be enabled.\r \n " +
174
175
"\r \n " +
175
- "Optional ``ff_profile_dir`` is the path to the Firefox profile directory if you wish to overwrite the default profile Selenium uses. Notice that prior to SeleniumLibrary 3.0, the library contained its own profile that was used by default.\r \n " +
176
+ "Optional ``ff_profile_dir`` is the path to the Firefox profile directory if you wish to overwrite the default profile Selenium uses. Notice that prior to SeleniumLibrary 3.0, the library contained its own profile that was used by default.\r \n " +
177
+ "\r \n " +
178
+ "Optional ``isWebDriverManager`` is a flag of using automation download driver of browser and setting system variable for driver path.\r \n " +
176
179
"\r \n " +
177
180
"Examples:\r \n " +
178
181
"| `Open Browser` | http://example.com | Chrome |\r \n " +
@@ -181,13 +184,18 @@ public void closeBrowser() {
181
184
"\r \n " +
182
185
"If the provided configuration options are not enough, it is possible to use `Create Webdriver` to customize browser initialization even more." )
183
186
@ ArgumentNames ({ "url" , "browserName=firefox" , "alias=None" , "remoteUrl=None" , "desiredCapabilities=None" ,
184
- "browserOptions=None" })
185
- public String openBrowser (String url , String ... args ) throws Throwable {
187
+ "browserOptions=None" , "isWebDriverManager=false" })
188
+ public String openBrowser (String url , String ... args ) {
186
189
String browserName = robot .getParamsValue (args , 0 , "firefox" );
187
190
String alias = robot .getParamsValue (args , 1 , "None" );
188
191
String remoteUrl = robot .getParamsValue (args , 2 , "None" );
189
192
String desiredCapabilities = robot .getParamsValue (args , 3 , "None" );
190
193
String browserOptions = robot .getParamsValue (args , 4 , "None" );
194
+ boolean isWebDriverManager = robot .getParamsValue (args , 5 , false );
195
+
196
+ if (isWebDriverManager ) {
197
+ webDriverManagerSetup (browserName );
198
+ }
191
199
192
200
try {
193
201
logging .info ("browserName: " + browserName );
@@ -660,7 +668,7 @@ protected WebDriver createLocalWebDriver(String browserName, Capabilities desire
660
668
case "ipad" :
661
669
case "iphone" :
662
670
try {
663
- return new IOSDriver <WebElement >(new URL ("" ), desiredCapabilities );
671
+ return new IOSDriver <>(new URL ("" ), desiredCapabilities );
664
672
} catch (Exception e ) {
665
673
throw new SeleniumLibraryFatalException ("Creating " + browserName + " instance failed." , e );
666
674
}
@@ -669,6 +677,41 @@ protected WebDriver createLocalWebDriver(String browserName, Capabilities desire
669
677
}
670
678
}
671
679
680
+ @ RobotKeyword ("WebDriver Manager Setup" )
681
+ @ ArgumentNames ({"browserName=firefox" })
682
+ public void webDriverManagerSetup (String browserName ) {
683
+ initWebDriver (browserName );
684
+ logging .info (String .format ("Init WebDriver Manager for '%s' browser" , browserName ));
685
+ }
686
+
687
+ private void initWebDriver (String browserName ) {
688
+ switch (browserName .toLowerCase ()) {
689
+ case "ff" :
690
+ case "firefox" :
691
+ case "ffheadless" :
692
+ case "firefoxheadless" :
693
+ WebDriverManager .getInstance (DriverManagerType .FIREFOX ).setup ();
694
+ break ;
695
+ case "ie" :
696
+ case "internetexplorer" :
697
+ WebDriverManager .getInstance (DriverManagerType .IEXPLORER ).setup ();
698
+ break ;
699
+ case "edge" :
700
+ WebDriverManager .getInstance (DriverManagerType .EDGE ).setup ();
701
+ break ;
702
+ case "gc" :
703
+ case "chrome" :
704
+ case "googlechrome" :
705
+ case "gcheadless" :
706
+ case "chromeheadless" :
707
+ case "googlechromeheadless" :
708
+ WebDriverManager .getInstance (DriverManagerType .CHROME ).setup ();
709
+ break ;
710
+ default :
711
+ throw new SeleniumLibraryFatalException (browserName + " is not a supported browser for WebDriver Manager." );
712
+ }
713
+ }
714
+
672
715
protected WebDriver createRemoteWebDriver (Capabilities desiredCapabilities , URL remoteUrl ) {
673
716
HttpCommandExecutor httpCommandExecutor = new HttpCommandExecutor (remoteUrl );
674
717
setRemoteWebDriverProxy (httpCommandExecutor );
@@ -677,7 +720,7 @@ protected WebDriver createRemoteWebDriver(Capabilities desiredCapabilities, URL
677
720
678
721
protected Capabilities createCapabilities (String browserName , String desiredCapabilitiesString ,
679
722
String browserOptions ) {
680
- Capabilities desiredCapabilities ;
723
+ MutableCapabilities desiredCapabilities ;
681
724
switch (browserName .toLowerCase ()) {
682
725
case "ff" :
683
726
case "firefox" :
@@ -726,17 +769,16 @@ protected Capabilities createCapabilities(String browserName, String desiredCapa
726
769
JSONObject jsonObject = (JSONObject ) JSONValue .parse (desiredCapabilitiesString );
727
770
if (jsonObject != null ) {
728
771
// Valid JSON
729
- Iterator <?> iterator = jsonObject .entrySet ().iterator ();
730
- while (iterator .hasNext ()) {
731
- Entry <?, ?> entry = (Entry <?, ?>) iterator .next ();
732
- ((MutableCapabilities ) desiredCapabilities ).setCapability (entry .getKey ().toString (), entry .getValue ());
772
+ for (Object o : jsonObject .entrySet ()) {
773
+ Entry <?, ?> entry = (Entry <?, ?>) o ;
774
+ desiredCapabilities .setCapability (entry .getKey ().toString (), entry .getValue ());
733
775
}
734
776
} else {
735
777
// Invalid JSON. Old style key-value pairs
736
778
for (String capability : desiredCapabilitiesString .split ("," )) {
737
779
String [] keyValue = capability .split (":" );
738
780
if (keyValue .length == 2 ) {
739
- (( MutableCapabilities ) desiredCapabilities ) .setCapability (keyValue [0 ], keyValue [1 ]);
781
+ desiredCapabilities .setCapability (keyValue [0 ], keyValue [1 ]);
740
782
} else {
741
783
logging .warn ("Invalid desiredCapabilities: " + desiredCapabilitiesString );
742
784
}
@@ -751,34 +793,34 @@ protected void parseBrowserOptionsChrome(String browserOptions, Capabilities des
751
793
JSONObject jsonObject = (JSONObject ) JSONValue .parse (browserOptions );
752
794
if (jsonObject != null ) {
753
795
// Check all properties for translation to ChromeOptions
754
- for (Iterator <?> iterator = jsonObject .keySet (). iterator (); iterator . hasNext (); ) {
755
- String key = (String )iterator . next () ;
796
+ for (Object o : jsonObject .keySet ()) {
797
+ String key = (String ) o ;
756
798
switch (key ) {
757
- case "args" : {
758
- // args is a list of strings
759
- List <String > args = new ArrayList <>();
760
- for (Object arg : (JSONArray )jsonObject .get (key )) {
761
- args .add ("--" +arg .toString ().replace ("--" , "" ));
799
+ case "args" : {
800
+ // args is a list of strings
801
+ List <String > args = new ArrayList <>();
802
+ for (Object arg : (JSONArray ) jsonObject .get (key )) {
803
+ args .add ("--" + arg .toString ().replace ("--" , "" ));
804
+ }
805
+ ((ChromeOptions ) desiredCapabilities ).addArguments (args );
806
+ break ;
762
807
}
763
- (( ChromeOptions ) desiredCapabilities ). addArguments ( args );
764
- break ;
765
- }
766
- case " extensions" : {
767
- List < File > extensions = new ArrayList <>();
768
- for ( Object extension : ( JSONArray ) jsonObject . get ( key )) {
769
- extensions . add ( new File ( extension . toString (). toString (). replace ( '/' , File . separatorChar ))) ;
808
+ case "extensions" : {
809
+ List < File > extensions = new ArrayList <>() ;
810
+ for ( Object extension : ( JSONArray ) jsonObject . get ( key )) {
811
+ extensions . add ( new File ( extension . toString (). replace ( '/' , File . separatorChar )));
812
+ }
813
+ (( ChromeOptions ) desiredCapabilities ). addExtensions ( extensions );
814
+ break ;
770
815
}
771
- ((ChromeOptions ) desiredCapabilities ).addExtensions (extensions );
772
- break ;
773
- }
774
- case "disable-extensions" :
775
- // change casing
776
- ((ChromeOptions ) desiredCapabilities ).setExperimentalOption ("useAutomationExtension" , false );
777
- break ;
778
- default :
779
- // all unknonw properties are passed as is
780
- ((ChromeOptions ) desiredCapabilities ).setExperimentalOption (key , jsonObject .get (key ));
781
- break ;
816
+ case "disable-extensions" :
817
+ // change casing
818
+ ((ChromeOptions ) desiredCapabilities ).setExperimentalOption ("useAutomationExtension" , false );
819
+ break ;
820
+ default :
821
+ // all unknonw properties are passed as is
822
+ ((ChromeOptions ) desiredCapabilities ).setExperimentalOption (key , jsonObject .get (key ));
823
+ break ;
782
824
}
783
825
}
784
826
} else {
@@ -792,16 +834,14 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
792
834
JSONObject jsonObject = (JSONObject ) JSONValue .parse (browserOptions );
793
835
if (jsonObject != null ) {
794
836
FirefoxProfile firefoxProfile = new FirefoxProfile ();
795
- Iterator <?> iterator = jsonObject .entrySet ().iterator ();
796
- while (iterator .hasNext ()) {
797
- Entry <?, ?> entry = (Entry <?, ?>) iterator .next ();
837
+ for (Object o1 : jsonObject .entrySet ()) {
838
+ Entry <?, ?> entry = (Entry <?, ?>) o1 ;
798
839
String key = entry .getKey ().toString ();
799
840
if (key .equals ("preferences" )) {
800
841
// Preferences
801
842
JSONObject preferences = (JSONObject ) entry .getValue ();
802
- Iterator <?> iteratorPreferences = preferences .entrySet ().iterator ();
803
- while (iteratorPreferences .hasNext ()) {
804
- Entry <?, ?> entryPreferences = (Entry <?, ?>) iteratorPreferences .next ();
843
+ for (Object o : preferences .entrySet ()) {
844
+ Entry <?, ?> entryPreferences = (Entry <?, ?>) o ;
805
845
Object valuePreferences = entryPreferences .getValue ();
806
846
logging .debug (String .format ("Adding property: %s with value: %s" ,
807
847
entryPreferences .getKey ().toString (), valuePreferences ));
@@ -810,7 +850,7 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
810
850
((Number ) valuePreferences ).intValue ());
811
851
} else if (valuePreferences instanceof Boolean ) {
812
852
firefoxProfile .setPreference (entryPreferences .getKey ().toString (),
813
- (( Boolean ) valuePreferences ). booleanValue () );
853
+ (Boolean ) valuePreferences );
814
854
} else {
815
855
firefoxProfile .setPreference (entryPreferences .getKey ().toString (),
816
856
valuePreferences .toString ());
@@ -819,9 +859,8 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
819
859
} else if (key .equals ("extensions" )) {
820
860
// Extensions
821
861
JSONArray extensions = (JSONArray ) entry .getValue ();
822
- Iterator <?> iteratorExtensions = extensions .iterator ();
823
- while (iteratorExtensions .hasNext ()) {
824
- File file = new File (iteratorExtensions .next ().toString ().replace ('/' , File .separatorChar ));
862
+ for (Object extension : extensions ) {
863
+ File file = new File (extension .toString ().replace ('/' , File .separatorChar ));
825
864
firefoxProfile .addExtension (file );
826
865
}
827
866
} else {
0 commit comments