diff --git a/pom.xml b/pom.xml index ba697b4..50013c1 100644 --- a/pom.xml +++ b/pom.xml @@ -134,6 +134,11 @@ 2.18.3 test + + io.github.bonigarcia + webdrivermanager + 3.4.0 + diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/BrowserManagement.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/BrowserManagement.java index de09911..21949d5 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/BrowserManagement.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/BrowserManagement.java @@ -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; @@ -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; @@ -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" + @@ -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); @@ -665,7 +673,7 @@ protected WebDriver createLocalWebDriver(String browserName, Capabilities desire case "ipad": case "iphone": try { - return new IOSDriver(new URL(""), desiredCapabilities); + return new IOSDriver<>(new URL(""), desiredCapabilities); } catch (Exception e) { throw new SeleniumLibraryFatalException("Creating " + browserName + " instance failed.", e); } @@ -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); @@ -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": @@ -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); } @@ -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 args = new ArrayList<>(); - for (Object arg : (JSONArray)jsonObject.get(key)) { - args.add("--"+arg.toString().replace("--", "")); + case "args": { + // args is a list of strings + List 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 extensions = new ArrayList<>(); - for (Object extension : (JSONArray)jsonObject.get(key)) { - extensions.add(new File(extension.toString().toString().replace('/', File.separatorChar))); + case "extensions": { + List 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 { @@ -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)); @@ -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()); @@ -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 { diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Cookie.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Cookie.java index cf11112..db21fc2 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Cookie.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Cookie.java @@ -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; diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java index cf33782..706c841 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java @@ -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; @@ -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); } @@ -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 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 // ############################## diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Logging.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Logging.java index 2ecead7..3047fba 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Logging.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Logging.java @@ -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; diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Robot.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Robot.java index 818baa8..987be24 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Robot.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Robot.java @@ -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; @@ -31,6 +30,8 @@ public 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; diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Screenshot.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Screenshot.java index d2a4ac2..4645957 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Screenshot.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Screenshot.java @@ -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; diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/SelectElement.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/SelectElement.java index ae20fb4..43b2b8c 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/SelectElement.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/SelectElement.java @@ -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; diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/TableElement.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/TableElement.java index 5d66441..507c3d6 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/TableElement.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/TableElement.java @@ -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; diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Waiting.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Waiting.java index c5a44a2..06842e9 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Waiting.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Waiting.java @@ -5,7 +5,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; diff --git a/src/main/java/com/github/markusbernhardt/seleniumlibrary/locators/ElementFinder.java b/src/main/java/com/github/markusbernhardt/seleniumlibrary/locators/ElementFinder.java index d658248..2566cf0 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/locators/ElementFinder.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/locators/ElementFinder.java @@ -13,11 +13,9 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.python.util.PythonInterpreter; -import org.robotframework.javalib.annotation.Autowired; import com.github.markusbernhardt.seleniumlibrary.SeleniumLibraryNonFatalException; import com.github.markusbernhardt.seleniumlibrary.keywords.Element; -import com.github.markusbernhardt.seleniumlibrary.keywords.Logging; import com.github.markusbernhardt.seleniumlibrary.utils.Python; public class ElementFinder {