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 ;
@@ -169,13 +170,15 @@ public void closeBrowser() {
169
170
"| Iphone | iphone |\r \n " +
170
171
"| JBrowser | jbrowser |\r \n " +
171
172
"\r \n " +
172
- "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 " +
173
+ "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 " +
173
174
"\r \n " +
174
175
"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 " +
175
176
"\r \n " +
176
- "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 " +
177
+ "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 " +
177
178
"\r \n " +
178
- "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 " +
179
+ "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 " +
180
+ "\r \n " +
181
+ "Optional ``isWebDriverManager`` is a flag of using automation download driver of browser and setting system variable for driver path.\r \n " +
179
182
"\r \n " +
180
183
"Examples:\r \n " +
181
184
"| `Open Browser` | http://example.com | Chrome |\r \n " +
@@ -184,13 +187,18 @@ public void closeBrowser() {
184
187
"\r \n " +
185
188
"If the provided configuration options are not enough, it is possible to use `Create Webdriver` to customize browser initialization even more." )
186
189
@ ArgumentNames ({ "url" , "browserName=firefox" , "alias=None" , "remoteUrl=None" , "desiredCapabilities=None" ,
187
- "browserOptions=None" })
188
- public String openBrowser (String url , String ... args ) throws Throwable {
190
+ "browserOptions=None" , "isWebDriverManager=false" })
191
+ public String openBrowser (String url , String ... args ) {
189
192
String browserName = robot .getParamsValue (args , 0 , "firefox" );
190
193
String alias = robot .getParamsValue (args , 1 , "None" );
191
194
String remoteUrl = robot .getParamsValue (args , 2 , "None" );
192
195
String desiredCapabilities = robot .getParamsValue (args , 3 , "None" );
193
196
String browserOptions = robot .getParamsValue (args , 4 , "None" );
197
+ boolean isWebDriverManager = robot .getParamsValue (args , 5 , false );
198
+
199
+ if (isWebDriverManager ) {
200
+ webDriverManagerSetup (browserName );
201
+ }
194
202
195
203
try {
196
204
logging .info ("browserName: " + browserName );
@@ -665,7 +673,7 @@ protected WebDriver createLocalWebDriver(String browserName, Capabilities desire
665
673
case "ipad" :
666
674
case "iphone" :
667
675
try {
668
- return new IOSDriver <WebElement >(new URL ("" ), desiredCapabilities );
676
+ return new IOSDriver <>(new URL ("" ), desiredCapabilities );
669
677
} catch (Exception e ) {
670
678
throw new SeleniumLibraryFatalException ("Creating " + browserName + " instance failed." , e );
671
679
}
@@ -674,6 +682,41 @@ protected WebDriver createLocalWebDriver(String browserName, Capabilities desire
674
682
}
675
683
}
676
684
685
+ @ RobotKeyword ("WebDriver Manager Setup" )
686
+ @ ArgumentNames ({"browserName=firefox" })
687
+ public void webDriverManagerSetup (String browserName ) {
688
+ initWebDriver (browserName );
689
+ logging .info (String .format ("Init WebDriver Manager for '%s' browser" , browserName ));
690
+ }
691
+
692
+ private void initWebDriver (String browserName ) {
693
+ switch (browserName .toLowerCase ()) {
694
+ case "ff" :
695
+ case "firefox" :
696
+ case "ffheadless" :
697
+ case "firefoxheadless" :
698
+ WebDriverManager .getInstance (DriverManagerType .FIREFOX ).setup ();
699
+ break ;
700
+ case "ie" :
701
+ case "internetexplorer" :
702
+ WebDriverManager .getInstance (DriverManagerType .IEXPLORER ).setup ();
703
+ break ;
704
+ case "edge" :
705
+ WebDriverManager .getInstance (DriverManagerType .EDGE ).setup ();
706
+ break ;
707
+ case "gc" :
708
+ case "chrome" :
709
+ case "googlechrome" :
710
+ case "gcheadless" :
711
+ case "chromeheadless" :
712
+ case "googlechromeheadless" :
713
+ WebDriverManager .getInstance (DriverManagerType .CHROME ).setup ();
714
+ break ;
715
+ default :
716
+ throw new SeleniumLibraryFatalException (browserName + " is not a supported browser for WebDriver Manager." );
717
+ }
718
+ }
719
+
677
720
protected WebDriver createRemoteWebDriver (Capabilities desiredCapabilities , URL remoteUrl ) {
678
721
HttpCommandExecutor httpCommandExecutor = new HttpCommandExecutor (remoteUrl );
679
722
setRemoteWebDriverProxy (httpCommandExecutor );
@@ -682,7 +725,7 @@ protected WebDriver createRemoteWebDriver(Capabilities desiredCapabilities, URL
682
725
683
726
protected Capabilities createCapabilities (String browserName , String desiredCapabilitiesString ,
684
727
String browserOptions ) {
685
- Capabilities desiredCapabilities ;
728
+ MutableCapabilities desiredCapabilities ;
686
729
switch (browserName .toLowerCase ()) {
687
730
case "ff" :
688
731
case "firefox" :
@@ -734,17 +777,16 @@ protected Capabilities createCapabilities(String browserName, String desiredCapa
734
777
JSONObject jsonObject = (JSONObject ) JSONValue .parse (desiredCapabilitiesString );
735
778
if (jsonObject != null ) {
736
779
// Valid JSON
737
- Iterator <?> iterator = jsonObject .entrySet ().iterator ();
738
- while (iterator .hasNext ()) {
739
- Entry <?, ?> entry = (Entry <?, ?>) iterator .next ();
740
- ((MutableCapabilities ) desiredCapabilities ).setCapability (entry .getKey ().toString (), entry .getValue ());
780
+ for (Object o : jsonObject .entrySet ()) {
781
+ Entry <?, ?> entry = (Entry <?, ?>) o ;
782
+ desiredCapabilities .setCapability (entry .getKey ().toString (), entry .getValue ());
741
783
}
742
784
} else {
743
785
// Invalid JSON. Old style key-value pairs
744
786
for (String capability : desiredCapabilitiesString .split ("," )) {
745
787
String [] keyValue = capability .split (":" );
746
788
if (keyValue .length == 2 ) {
747
- (( MutableCapabilities ) desiredCapabilities ) .setCapability (keyValue [0 ], keyValue [1 ]);
789
+ desiredCapabilities .setCapability (keyValue [0 ], keyValue [1 ]);
748
790
} else {
749
791
logging .warn ("Invalid desiredCapabilities: " + desiredCapabilitiesString );
750
792
}
@@ -759,34 +801,34 @@ protected void parseBrowserOptionsChrome(String browserOptions, Capabilities des
759
801
JSONObject jsonObject = (JSONObject ) JSONValue .parse (browserOptions );
760
802
if (jsonObject != null ) {
761
803
// Check all properties for translation to ChromeOptions
762
- for (Iterator <?> iterator = jsonObject .keySet (). iterator (); iterator . hasNext (); ) {
763
- String key = (String )iterator . next () ;
804
+ for (Object o : jsonObject .keySet ()) {
805
+ String key = (String ) o ;
764
806
switch (key ) {
765
- case "args" : {
766
- // args is a list of strings
767
- List <String > args = new ArrayList <>();
768
- for (Object arg : (JSONArray )jsonObject .get (key )) {
769
- args .add ("--" +arg .toString ().replace ("--" , "" ));
807
+ case "args" : {
808
+ // args is a list of strings
809
+ List <String > args = new ArrayList <>();
810
+ for (Object arg : (JSONArray ) jsonObject .get (key )) {
811
+ args .add ("--" + arg .toString ().replace ("--" , "" ));
812
+ }
813
+ ((ChromeOptions ) desiredCapabilities ).addArguments (args );
814
+ break ;
770
815
}
771
- (( ChromeOptions ) desiredCapabilities ). addArguments ( args );
772
- break ;
773
- }
774
- case " extensions" : {
775
- List < File > extensions = new ArrayList <>();
776
- for ( Object extension : ( JSONArray ) jsonObject . get ( key )) {
777
- extensions . add ( new File ( extension . toString (). toString (). replace ( '/' , File . separatorChar ))) ;
816
+ case "extensions" : {
817
+ List < File > extensions = new ArrayList <>() ;
818
+ for ( Object extension : ( JSONArray ) jsonObject . get ( key )) {
819
+ extensions . add ( new File ( extension . toString (). replace ( '/' , File . separatorChar )));
820
+ }
821
+ (( ChromeOptions ) desiredCapabilities ). addExtensions ( extensions );
822
+ break ;
778
823
}
779
- ((ChromeOptions ) desiredCapabilities ).addExtensions (extensions );
780
- break ;
781
- }
782
- case "disable-extensions" :
783
- // change casing
784
- ((ChromeOptions ) desiredCapabilities ).setExperimentalOption ("useAutomationExtension" , false );
785
- break ;
786
- default :
787
- // all unknonw properties are passed as is
788
- ((ChromeOptions ) desiredCapabilities ).setExperimentalOption (key , jsonObject .get (key ));
789
- break ;
824
+ case "disable-extensions" :
825
+ // change casing
826
+ ((ChromeOptions ) desiredCapabilities ).setExperimentalOption ("useAutomationExtension" , false );
827
+ break ;
828
+ default :
829
+ // all unknonw properties are passed as is
830
+ ((ChromeOptions ) desiredCapabilities ).setExperimentalOption (key , jsonObject .get (key ));
831
+ break ;
790
832
}
791
833
}
792
834
} else {
@@ -800,16 +842,14 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
800
842
JSONObject jsonObject = (JSONObject ) JSONValue .parse (browserOptions );
801
843
if (jsonObject != null ) {
802
844
FirefoxProfile firefoxProfile = new FirefoxProfile ();
803
- Iterator <?> iterator = jsonObject .entrySet ().iterator ();
804
- while (iterator .hasNext ()) {
805
- Entry <?, ?> entry = (Entry <?, ?>) iterator .next ();
845
+ for (Object o1 : jsonObject .entrySet ()) {
846
+ Entry <?, ?> entry = (Entry <?, ?>) o1 ;
806
847
String key = entry .getKey ().toString ();
807
848
if (key .equals ("preferences" )) {
808
849
// Preferences
809
850
JSONObject preferences = (JSONObject ) entry .getValue ();
810
- Iterator <?> iteratorPreferences = preferences .entrySet ().iterator ();
811
- while (iteratorPreferences .hasNext ()) {
812
- Entry <?, ?> entryPreferences = (Entry <?, ?>) iteratorPreferences .next ();
851
+ for (Object o : preferences .entrySet ()) {
852
+ Entry <?, ?> entryPreferences = (Entry <?, ?>) o ;
813
853
Object valuePreferences = entryPreferences .getValue ();
814
854
logging .debug (String .format ("Adding property: %s with value: %s" ,
815
855
entryPreferences .getKey ().toString (), valuePreferences ));
@@ -818,7 +858,7 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
818
858
((Number ) valuePreferences ).intValue ());
819
859
} else if (valuePreferences instanceof Boolean ) {
820
860
firefoxProfile .setPreference (entryPreferences .getKey ().toString (),
821
- (( Boolean ) valuePreferences ). booleanValue () );
861
+ (Boolean ) valuePreferences );
822
862
} else {
823
863
firefoxProfile .setPreference (entryPreferences .getKey ().toString (),
824
864
valuePreferences .toString ());
@@ -827,9 +867,8 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
827
867
} else if (key .equals ("extensions" )) {
828
868
// Extensions
829
869
JSONArray extensions = (JSONArray ) entry .getValue ();
830
- Iterator <?> iteratorExtensions = extensions .iterator ();
831
- while (iteratorExtensions .hasNext ()) {
832
- File file = new File (iteratorExtensions .next ().toString ().replace ('/' , File .separatorChar ));
870
+ for (Object extension : extensions ) {
871
+ File file = new File (extension .toString ().replace ('/' , File .separatorChar ));
833
872
firefoxProfile .addExtension (file );
834
873
}
835
874
} else {
0 commit comments