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

added webdrivermanager of io.github.bonigarcia library interaction #73

Merged
merged 5 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
<version>2.18.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.markusbernhardt.seleniumlibrary.keywords;

import io.appium.java_client.ios.IOSDriver;
import io.github.bonigarcia.wdm.DriverManagerType;
import io.github.bonigarcia.wdm.WebDriverManager;
import io.selendroid.client.SelendroidDriver;

import java.io.File;
Expand All @@ -10,7 +12,6 @@
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -169,13 +170,15 @@ public void closeBrowser() {
"| Iphone | iphone |\r\n" +
"| JBrowser | jbrowser |\r\n" +
"\r\n" +
"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" +
"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" +
"\r\n" +
"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" +
"\r\n" +
"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" +
"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" +
"\r\n" +
"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" +
"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" +
"\r\n" +
"Optional ``isWebDriverManager`` is a flag of using automation download driver of browser and setting system variable for driver path.\r\n" +
"\r\n" +
"Examples:\r\n" +
"| `Open Browser` | http://example.com | Chrome |\r\n" +
Expand All @@ -184,13 +187,18 @@ public void closeBrowser() {
"\r\n" +
"If the provided configuration options are not enough, it is possible to use `Create Webdriver` to customize browser initialization even more.")
@ArgumentNames({ "url", "browserName=firefox", "alias=None", "remoteUrl=None", "desiredCapabilities=None",
"browserOptions=None" })
public String openBrowser(String url, String... args) throws Throwable {
"browserOptions=None", "isWebDriverManager=false" })
public String openBrowser(String url, String... args) {
String browserName = robot.getParamsValue(args, 0, "firefox");
String alias = robot.getParamsValue(args, 1, "None");
String remoteUrl = robot.getParamsValue(args, 2, "None");
String desiredCapabilities = robot.getParamsValue(args, 3, "None");
String browserOptions = robot.getParamsValue(args, 4, "None");
boolean isWebDriverManager = robot.getParamsValue(args, 5, false);

if (isWebDriverManager) {
webDriverManagerSetup(browserName);
}

try {
logging.info("browserName: " + browserName);
Expand Down Expand Up @@ -665,7 +673,7 @@ protected WebDriver createLocalWebDriver(String browserName, Capabilities desire
case "ipad":
case "iphone":
try {
return new IOSDriver<WebElement>(new URL(""), desiredCapabilities);
return new IOSDriver<>(new URL(""), desiredCapabilities);
} catch (Exception e) {
throw new SeleniumLibraryFatalException("Creating " + browserName + " instance failed.", e);
}
Expand All @@ -674,6 +682,41 @@ protected WebDriver createLocalWebDriver(String browserName, Capabilities desire
}
}

@RobotKeyword("WebDriver Manager Setup")
@ArgumentNames({"browserName=firefox"})
public void webDriverManagerSetup(String browserName) {
initWebDriver(browserName);
logging.info(String.format("Init WebDriver Manager for '%s' browser", browserName));
}

private void initWebDriver(String browserName) {
switch (browserName.toLowerCase()) {
case "ff":
case "firefox":
case "ffheadless":
case "firefoxheadless":
WebDriverManager.getInstance(DriverManagerType.FIREFOX).setup();
break;
case "ie":
case "internetexplorer":
WebDriverManager.getInstance(DriverManagerType.IEXPLORER).setup();
break;
case "edge":
WebDriverManager.getInstance(DriverManagerType.EDGE).setup();
break;
case "gc":
case "chrome":
case "googlechrome":
case "gcheadless":
case "chromeheadless":
case "googlechromeheadless":
WebDriverManager.getInstance(DriverManagerType.CHROME).setup();
break;
default:
throw new SeleniumLibraryFatalException(browserName + " is not a supported browser for WebDriver Manager.");
}
}

protected WebDriver createRemoteWebDriver(Capabilities desiredCapabilities, URL remoteUrl) {
HttpCommandExecutor httpCommandExecutor = new HttpCommandExecutor(remoteUrl);
setRemoteWebDriverProxy(httpCommandExecutor);
Expand All @@ -682,7 +725,7 @@ protected WebDriver createRemoteWebDriver(Capabilities desiredCapabilities, URL

protected Capabilities createCapabilities(String browserName, String desiredCapabilitiesString,
String browserOptions) {
Capabilities desiredCapabilities;
MutableCapabilities desiredCapabilities;
switch (browserName.toLowerCase()) {
case "ff":
case "firefox":
Expand Down Expand Up @@ -734,17 +777,16 @@ protected Capabilities createCapabilities(String browserName, String desiredCapa
JSONObject jsonObject = (JSONObject) JSONValue.parse(desiredCapabilitiesString);
if (jsonObject != null) {
// Valid JSON
Iterator<?> iterator = jsonObject.entrySet().iterator();
while (iterator.hasNext()) {
Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
((MutableCapabilities) desiredCapabilities).setCapability(entry.getKey().toString(), entry.getValue());
for (Object o : jsonObject.entrySet()) {
Entry<?, ?> entry = (Entry<?, ?>) o;
desiredCapabilities.setCapability(entry.getKey().toString(), entry.getValue());
}
} else {
// Invalid JSON. Old style key-value pairs
for (String capability : desiredCapabilitiesString.split(",")) {
String[] keyValue = capability.split(":");
if (keyValue.length == 2) {
((MutableCapabilities) desiredCapabilities).setCapability(keyValue[0], keyValue[1]);
desiredCapabilities.setCapability(keyValue[0], keyValue[1]);
} else {
logging.warn("Invalid desiredCapabilities: " + desiredCapabilitiesString);
}
Expand All @@ -759,34 +801,34 @@ protected void parseBrowserOptionsChrome(String browserOptions, Capabilities des
JSONObject jsonObject = (JSONObject) JSONValue.parse(browserOptions);
if (jsonObject != null) {
// Check all properties for translation to ChromeOptions
for (Iterator<?> iterator = jsonObject.keySet().iterator(); iterator.hasNext(); ) {
String key = (String)iterator.next();
for (Object o : jsonObject.keySet()) {
String key = (String) o;
switch (key) {
case "args": {
// args is a list of strings
List<String> args = new ArrayList<>();
for (Object arg : (JSONArray)jsonObject.get(key)) {
args.add("--"+arg.toString().replace("--", ""));
case "args": {
// args is a list of strings
List<String> args = new ArrayList<>();
for (Object arg : (JSONArray) jsonObject.get(key)) {
args.add("--" + arg.toString().replace("--", ""));
}
((ChromeOptions) desiredCapabilities).addArguments(args);
break;
}
((ChromeOptions) desiredCapabilities).addArguments(args);
break;
}
case "extensions": {
List<File> extensions = new ArrayList<>();
for (Object extension : (JSONArray)jsonObject.get(key)) {
extensions.add(new File(extension.toString().toString().replace('/', File.separatorChar)));
case "extensions": {
List<File> extensions = new ArrayList<>();
for (Object extension : (JSONArray) jsonObject.get(key)) {
extensions.add(new File(extension.toString().replace('/', File.separatorChar)));
}
((ChromeOptions) desiredCapabilities).addExtensions(extensions);
break;
}
((ChromeOptions) desiredCapabilities).addExtensions(extensions);
break;
}
case "disable-extensions":
// change casing
((ChromeOptions) desiredCapabilities).setExperimentalOption("useAutomationExtension", false);
break;
default:
// all unknonw properties are passed as is
((ChromeOptions) desiredCapabilities).setExperimentalOption(key, jsonObject.get(key));
break;
case "disable-extensions":
// change casing
((ChromeOptions) desiredCapabilities).setExperimentalOption("useAutomationExtension", false);
break;
default:
// all unknonw properties are passed as is
((ChromeOptions) desiredCapabilities).setExperimentalOption(key, jsonObject.get(key));
break;
}
}
} else {
Expand All @@ -800,16 +842,14 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
JSONObject jsonObject = (JSONObject) JSONValue.parse(browserOptions);
if (jsonObject != null) {
FirefoxProfile firefoxProfile = new FirefoxProfile();
Iterator<?> iterator = jsonObject.entrySet().iterator();
while (iterator.hasNext()) {
Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
for (Object o1 : jsonObject.entrySet()) {
Entry<?, ?> entry = (Entry<?, ?>) o1;
String key = entry.getKey().toString();
if (key.equals("preferences")) {
// Preferences
JSONObject preferences = (JSONObject) entry.getValue();
Iterator<?> iteratorPreferences = preferences.entrySet().iterator();
while (iteratorPreferences.hasNext()) {
Entry<?, ?> entryPreferences = (Entry<?, ?>) iteratorPreferences.next();
for (Object o : preferences.entrySet()) {
Entry<?, ?> entryPreferences = (Entry<?, ?>) o;
Object valuePreferences = entryPreferences.getValue();
logging.debug(String.format("Adding property: %s with value: %s",
entryPreferences.getKey().toString(), valuePreferences));
Expand All @@ -818,7 +858,7 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
((Number) valuePreferences).intValue());
} else if (valuePreferences instanceof Boolean) {
firefoxProfile.setPreference(entryPreferences.getKey().toString(),
((Boolean) valuePreferences).booleanValue());
(Boolean) valuePreferences);
} else {
firefoxProfile.setPreference(entryPreferences.getKey().toString(),
valuePreferences.toString());
Expand All @@ -827,9 +867,8 @@ protected void parseBrowserOptionsFirefox(String browserOptions, Capabilities de
} else if (key.equals("extensions")) {
// Extensions
JSONArray extensions = (JSONArray) entry.getValue();
Iterator<?> iteratorExtensions = extensions.iterator();
while (iteratorExtensions.hasNext()) {
File file = new File(iteratorExtensions.next().toString().replace('/', File.separatorChar));
for (Object extension : extensions) {
File file = new File(extension.toString().replace('/', File.separatorChar));
firefoxProfile.addExtension(file);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.github.markusbernhardt.seleniumlibrary.keywords;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.Autowired;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywordOverload;
import org.robotframework.javalib.annotation.RobotKeywords;

import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.Autowired;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywordOverload;
import org.robotframework.javalib.annotation.RobotKeywords;

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

if (actualXpathCount != expectedXpathCount) {
if (message == null || message.equals("")) {
if (message.isEmpty()) {
message = String.format("Xpath %s should have matched %s times but matched %s times.", xpath,
expectedXpathCount, actualXpathCount);
}
Expand All @@ -892,6 +891,22 @@ public void xpathShouldMatchXTimes(String xpath, int expectedXpathCount, String.
logLevel);
}

@RobotKeyword("Click to element from list elements by locator ``xpath``.")
@ArgumentNames({"xpath", "index=0", "message=NONE"})
public void clickElementByIndex(String xpath, String... params) {
String message = robot.getParamsValue(params, 0, "");
int index = robot.getParamsValue(params, 1, 0);
List<WebElement> elements = elementFind(xpath, false, false);
if (elements.isEmpty()) {
if (message.isEmpty()) {
message = String.format("The Element was not found by locator '%s' with index '%d'", xpath, index);
}
throw new SeleniumLibraryNonFatalException(message);
}
WebElement element = elements.get(index);
element.click();
}

// ##############################
// Internal Methods
// ##############################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.Autowired;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywordOverload;
import org.robotframework.javalib.annotation.RobotKeywords;

import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Map;

import org.python.util.PythonInterpreter;
import org.robotframework.javalib.annotation.Autowired;
import org.robotframework.javalib.annotation.RobotKeywords;

import com.google.gson.Gson;
Expand All @@ -31,6 +30,8 @@ public <T> T getParamsValue(String[] params, int index, T defaultValue) {
value = (T) givenValue;
} else if (defaultValue instanceof List) {
value = (T) parseRobotList(givenValue);
} else if (Boolean.valueOf(givenValue)) {
value = (T) Boolean.valueOf(givenValue);
}
}
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.Autowired;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywordOverload;
import org.robotframework.javalib.annotation.RobotKeywords;

import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.Autowired;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywordOverload;
import org.robotframework.javalib.annotation.RobotKeywords;

import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.Autowired;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywordOverload;
import org.robotframework.javalib.annotation.RobotKeywords;

import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;
Expand Down
Loading