Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 849b542

Browse files
authored
Merge pull request #73 from YauheniPo/feature/webdrivermanager
added webdrivermanager of io.github.bonigarcia library interaction
2 parents eef989b + 38b2a5d commit 849b542

File tree

11 files changed

+111
-60
lines changed

11 files changed

+111
-60
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@
134134
<version>2.18.3</version>
135135
<scope>test</scope>
136136
</dependency>
137+
<dependency>
138+
<groupId>io.github.bonigarcia</groupId>
139+
<artifactId>webdrivermanager</artifactId>
140+
<version>3.4.0</version>
141+
</dependency>
137142
</dependencies>
138143

139144
<build>

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/BrowserManagement.java

Lines changed: 87 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.markusbernhardt.seleniumlibrary.keywords;
22

33
import io.appium.java_client.ios.IOSDriver;
4+
import io.github.bonigarcia.wdm.DriverManagerType;
5+
import io.github.bonigarcia.wdm.WebDriverManager;
46
import io.selendroid.client.SelendroidDriver;
57

68
import java.io.File;
@@ -10,7 +12,6 @@
1012
import java.net.URL;
1113
import java.net.UnknownHostException;
1214
import java.util.ArrayList;
13-
import java.util.Iterator;
1415
import java.util.List;
1516
import java.util.Map.Entry;
1617
import java.util.concurrent.TimeUnit;
@@ -169,13 +170,15 @@ public void closeBrowser() {
169170
"| Iphone | iphone |\r\n" +
170171
"| JBrowser | jbrowser |\r\n" +
171172
"\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" +
173174
"\r\n" +
174175
"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" +
175176
"\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" +
177178
"\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" +
179182
"\r\n" +
180183
"Examples:\r\n" +
181184
"| `Open Browser` | http://example.com | Chrome |\r\n" +
@@ -184,13 +187,18 @@ public void closeBrowser() {
184187
"\r\n" +
185188
"If the provided configuration options are not enough, it is possible to use `Create Webdriver` to customize browser initialization even more.")
186189
@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) {
189192
String browserName = robot.getParamsValue(args, 0, "firefox");
190193
String alias = robot.getParamsValue(args, 1, "None");
191194
String remoteUrl = robot.getParamsValue(args, 2, "None");
192195
String desiredCapabilities = robot.getParamsValue(args, 3, "None");
193196
String browserOptions = robot.getParamsValue(args, 4, "None");
197+
boolean isWebDriverManager = robot.getParamsValue(args, 5, false);
198+
199+
if (isWebDriverManager) {
200+
webDriverManagerSetup(browserName);
201+
}
194202

195203
try {
196204
logging.info("browserName: " + browserName);
@@ -665,7 +673,7 @@ protected WebDriver createLocalWebDriver(String browserName, Capabilities desire
665673
case "ipad":
666674
case "iphone":
667675
try {
668-
return new IOSDriver<WebElement>(new URL(""), desiredCapabilities);
676+
return new IOSDriver<>(new URL(""), desiredCapabilities);
669677
} catch (Exception e) {
670678
throw new SeleniumLibraryFatalException("Creating " + browserName + " instance failed.", e);
671679
}
@@ -674,6 +682,41 @@ protected WebDriver createLocalWebDriver(String browserName, Capabilities desire
674682
}
675683
}
676684

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+
677720
protected WebDriver createRemoteWebDriver(Capabilities desiredCapabilities, URL remoteUrl) {
678721
HttpCommandExecutor httpCommandExecutor = new HttpCommandExecutor(remoteUrl);
679722
setRemoteWebDriverProxy(httpCommandExecutor);
@@ -682,7 +725,7 @@ protected WebDriver createRemoteWebDriver(Capabilities desiredCapabilities, URL
682725

683726
protected Capabilities createCapabilities(String browserName, String desiredCapabilitiesString,
684727
String browserOptions) {
685-
Capabilities desiredCapabilities;
728+
MutableCapabilities desiredCapabilities;
686729
switch (browserName.toLowerCase()) {
687730
case "ff":
688731
case "firefox":
@@ -734,17 +777,16 @@ protected Capabilities createCapabilities(String browserName, String desiredCapa
734777
JSONObject jsonObject = (JSONObject) JSONValue.parse(desiredCapabilitiesString);
735778
if (jsonObject != null) {
736779
// 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());
741783
}
742784
} else {
743785
// Invalid JSON. Old style key-value pairs
744786
for (String capability : desiredCapabilitiesString.split(",")) {
745787
String[] keyValue = capability.split(":");
746788
if (keyValue.length == 2) {
747-
((MutableCapabilities) desiredCapabilities).setCapability(keyValue[0], keyValue[1]);
789+
desiredCapabilities.setCapability(keyValue[0], keyValue[1]);
748790
} else {
749791
logging.warn("Invalid desiredCapabilities: " + desiredCapabilitiesString);
750792
}
@@ -759,34 +801,34 @@ protected void parseBrowserOptionsChrome(String browserOptions, Capabilities des
759801
JSONObject jsonObject = (JSONObject) JSONValue.parse(browserOptions);
760802
if (jsonObject != null) {
761803
// 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;
764806
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;
770815
}
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;
778823
}
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;
790832
}
791833
}
792834
} else {
@@ -800,16 +842,14 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
800842
JSONObject jsonObject = (JSONObject) JSONValue.parse(browserOptions);
801843
if (jsonObject != null) {
802844
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;
806847
String key = entry.getKey().toString();
807848
if (key.equals("preferences")) {
808849
// Preferences
809850
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;
813853
Object valuePreferences = entryPreferences.getValue();
814854
logging.debug(String.format("Adding property: %s with value: %s",
815855
entryPreferences.getKey().toString(), valuePreferences));
@@ -818,7 +858,7 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
818858
((Number) valuePreferences).intValue());
819859
} else if (valuePreferences instanceof Boolean) {
820860
firefoxProfile.setPreference(entryPreferences.getKey().toString(),
821-
((Boolean) valuePreferences).booleanValue());
861+
(Boolean) valuePreferences);
822862
} else {
823863
firefoxProfile.setPreference(entryPreferences.getKey().toString(),
824864
valuePreferences.toString());
@@ -827,9 +867,8 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
827867
} else if (key.equals("extensions")) {
828868
// Extensions
829869
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));
833872
firefoxProfile.addExtension(file);
834873
}
835874
} else {

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Cookie.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.github.markusbernhardt.seleniumlibrary.keywords;
22

3-
import java.text.DateFormat;
43
import java.util.ArrayList;
54
import java.util.Date;
65

76
import org.robotframework.javalib.annotation.ArgumentNames;
87
import org.robotframework.javalib.annotation.Autowired;
98
import org.robotframework.javalib.annotation.RobotKeyword;
10-
import org.robotframework.javalib.annotation.RobotKeywordOverload;
119
import org.robotframework.javalib.annotation.RobotKeywords;
1210

1311
import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.robotframework.javalib.annotation.ArgumentNames;
1616
import org.robotframework.javalib.annotation.Autowired;
1717
import org.robotframework.javalib.annotation.RobotKeyword;
18-
import org.robotframework.javalib.annotation.RobotKeywordOverload;
1918
import org.robotframework.javalib.annotation.RobotKeywords;
2019

2120
import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;
@@ -881,7 +880,7 @@ public void xpathShouldMatchXTimes(String xpath, int expectedXpathCount, String.
881880
int actualXpathCount = elements.size();
882881

883882
if (actualXpathCount != expectedXpathCount) {
884-
if (message == null || message.equals("")) {
883+
if (message.isEmpty()) {
885884
message = String.format("Xpath %s should have matched %s times but matched %s times.", xpath,
886885
expectedXpathCount, actualXpathCount);
887886
}
@@ -892,6 +891,22 @@ public void xpathShouldMatchXTimes(String xpath, int expectedXpathCount, String.
892891
logLevel);
893892
}
894893

894+
@RobotKeyword("Click to element from list elements by locator ``xpath``.")
895+
@ArgumentNames({"xpath", "index=0", "message=NONE"})
896+
public void clickElementByIndex(String xpath, String... params) {
897+
String message = robot.getParamsValue(params, 0, "");
898+
int index = robot.getParamsValue(params, 1, 0);
899+
List<WebElement> elements = elementFind(xpath, false, false);
900+
if (elements.isEmpty()) {
901+
if (message.isEmpty()) {
902+
message = String.format("The Element was not found by locator '%s' with index '%d'", xpath, index);
903+
}
904+
throw new SeleniumLibraryNonFatalException(message);
905+
}
906+
WebElement element = elements.get(index);
907+
element.click();
908+
}
909+
895910
// ##############################
896911
// Internal Methods
897912
// ##############################

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Logging.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.robotframework.javalib.annotation.ArgumentNames;
1313
import org.robotframework.javalib.annotation.Autowired;
1414
import org.robotframework.javalib.annotation.RobotKeyword;
15-
import org.robotframework.javalib.annotation.RobotKeywordOverload;
1615
import org.robotframework.javalib.annotation.RobotKeywords;
1716

1817
import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Robot.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Map;
66

77
import org.python.util.PythonInterpreter;
8-
import org.robotframework.javalib.annotation.Autowired;
98
import org.robotframework.javalib.annotation.RobotKeywords;
109

1110
import com.google.gson.Gson;
@@ -31,6 +30,8 @@ public <T> T getParamsValue(String[] params, int index, T defaultValue) {
3130
value = (T) givenValue;
3231
} else if (defaultValue instanceof List) {
3332
value = (T) parseRobotList(givenValue);
33+
} else if (Boolean.valueOf(givenValue)) {
34+
value = (T) Boolean.valueOf(givenValue);
3435
}
3536
}
3637
return value;

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Screenshot.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.robotframework.javalib.annotation.ArgumentNames;
1212
import org.robotframework.javalib.annotation.Autowired;
1313
import org.robotframework.javalib.annotation.RobotKeyword;
14-
import org.robotframework.javalib.annotation.RobotKeywordOverload;
1514
import org.robotframework.javalib.annotation.RobotKeywords;
1615

1716
import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/SelectElement.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.robotframework.javalib.annotation.ArgumentNames;
1010
import org.robotframework.javalib.annotation.Autowired;
1111
import org.robotframework.javalib.annotation.RobotKeyword;
12-
import org.robotframework.javalib.annotation.RobotKeywordOverload;
1312
import org.robotframework.javalib.annotation.RobotKeywords;
1413

1514
import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/TableElement.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.robotframework.javalib.annotation.ArgumentNames;
88
import org.robotframework.javalib.annotation.Autowired;
99
import org.robotframework.javalib.annotation.RobotKeyword;
10-
import org.robotframework.javalib.annotation.RobotKeywordOverload;
1110
import org.robotframework.javalib.annotation.RobotKeywords;
1211

1312
import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;

0 commit comments

Comments
 (0)