From 2ceb2924f33515aa8e9c5939fc4c52476a408067 Mon Sep 17 00:00:00 2001 From: YauheniPo Date: Sat, 29 Jun 2019 12:35:28 +0300 Subject: [PATCH 1/5] added webdrivermanager of io.github.bonigarcia library interaction --- pom.xml | 5 + .../keywords/BrowserManagement.java | 127 +++++++++++------- .../seleniumlibrary/keywords/Cookie.java | 2 - .../seleniumlibrary/keywords/Element.java | 22 ++- .../seleniumlibrary/keywords/Logging.java | 1 - .../seleniumlibrary/keywords/Robot.java | 1 - .../seleniumlibrary/keywords/Screenshot.java | 1 - .../keywords/SelectElement.java | 1 - .../keywords/TableElement.java | 1 - .../seleniumlibrary/keywords/Waiting.java | 1 - .../locators/ElementFinder.java | 2 - 11 files changed, 108 insertions(+), 56 deletions(-) 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..4b9232e 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; @@ -184,13 +185,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", "webDriverManager=None" }) + 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"); + String webDriverManager = robot.getParamsValue(args, 5, "None"); + + if (!webDriverManager.equals("None")) { + webDriverManagerSetup(browserName); + } try { logging.info("browserName: " + browserName); @@ -665,7 +671,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 +680,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 +723,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 +775,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 +799,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 +840,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 +856,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 +865,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..35907b0 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; @@ -892,6 +891,27 @@ public void xpathShouldMatchXTimes(String xpath, int expectedXpathCount, String. logLevel); } + @RobotKeyword("Click to First Item from List Items by Locator.") + @ArgumentNames({"locator", "childLocator=NONE", "message=NONE"}) + public void clickToFirstItem(String locator, String... params) { + String child = robot.getParamsValue(params, 0, ""); + String message = robot.getParamsValue(params, 1, ""); + List elements = browserManagement.getCurrentWebDriver().findElements(By.xpath(locator)); + if (elements.size() == 0) { + if (message == null || message.equals("")) { + message = String.format("The Element was not found by locator '%s' and child locator '%s'.", + locator, child); + } + throw new SeleniumLibraryNonFatalException(message); + } + WebElement element = elements.get(0); + if (!child.isEmpty()) { + element.findElements(By.xpath(child)).get(0).click(); + } else { + 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..40023dd 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; 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 { From b14c678729f1a0e6eba9b2ad494f8bb961f26e8e Mon Sep 17 00:00:00 2001 From: YauheniPo Date: Sat, 29 Jun 2019 13:19:05 +0300 Subject: [PATCH 2/5] clickToFirstItem method refactoring --- .../seleniumlibrary/keywords/Element.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 35907b0..229d38d 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java @@ -880,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); } @@ -894,19 +894,19 @@ public void xpathShouldMatchXTimes(String xpath, int expectedXpathCount, String. @RobotKeyword("Click to First Item from List Items by Locator.") @ArgumentNames({"locator", "childLocator=NONE", "message=NONE"}) public void clickToFirstItem(String locator, String... params) { - String child = robot.getParamsValue(params, 0, ""); + String childLocator = robot.getParamsValue(params, 0, ""); String message = robot.getParamsValue(params, 1, ""); List elements = browserManagement.getCurrentWebDriver().findElements(By.xpath(locator)); - if (elements.size() == 0) { - if (message == null || message.equals("")) { + if (elements.isEmpty()) { + if (message.isEmpty()) { message = String.format("The Element was not found by locator '%s' and child locator '%s'.", - locator, child); + locator, childLocator); } throw new SeleniumLibraryNonFatalException(message); } WebElement element = elements.get(0); - if (!child.isEmpty()) { - element.findElements(By.xpath(child)).get(0).click(); + if (!childLocator.isEmpty()) { + element.findElements(By.xpath(childLocator)).get(0).click(); } else { element.click(); } From 36eb5d8b2bb81332d0a725be44d433cd88684226 Mon Sep 17 00:00:00 2001 From: YauheniPo Date: Tue, 2 Jul 2019 23:29:01 +0300 Subject: [PATCH 3/5] Updated openBrowser method of BrowserManagement class and Robot class for working with boolean parameters --- .../keywords/BrowserManagement.java | 22 ++++++++++--------- .../seleniumlibrary/keywords/Robot.java | 2 ++ 2 files changed, 14 insertions(+), 10 deletions(-) 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 4b9232e..f38c2ab 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/BrowserManagement.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/BrowserManagement.java @@ -103,7 +103,7 @@ public class BrowserManagement extends RunOnFailureKeywordsAdapter { protected Element element; @Autowired - private Robot robot; + private Robot robot = new Robot(); // ############################## // Getter / Setter @@ -170,7 +170,9 @@ 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 ``isWebDriverManager`` is a flag of using automation download driver of browser and setting system variable for driver path.\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" + @@ -184,17 +186,17 @@ public void closeBrowser() { "| `Open Browser` | http://example.com | Edge | remote_url=http://127.0.0.1:4444/wd/hub |\r\n" + "\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", "webDriverManager=None" }) + @ArgumentNames({ "url", "browserName=firefox", "isWebDriverManager=false", "alias=None", "remoteUrl=None", + "desiredCapabilities=None", "browserOptions=None" }) 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"); - String webDriverManager = robot.getParamsValue(args, 5, "None"); + boolean isWebDriverManager = robot.getParamsValue(args, 1, false); + String alias = robot.getParamsValue(args, 2, "None"); + String remoteUrl = robot.getParamsValue(args, 3, "None"); + String desiredCapabilities = robot.getParamsValue(args, 4, "None"); + String browserOptions = robot.getParamsValue(args, 5, "None"); - if (!webDriverManager.equals("None")) { + if (isWebDriverManager) { webDriverManagerSetup(browserName); } 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 40023dd..987be24 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Robot.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Robot.java @@ -30,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; From fe5b8bb1d3fce3919c4e0b9f04b2e54083663a83 Mon Sep 17 00:00:00 2001 From: YauheniPo Date: Thu, 4 Jul 2019 22:58:03 +0300 Subject: [PATCH 4/5] Corrected by review comments --- .../keywords/BrowserManagement.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) 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 f38c2ab..21949d5 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/BrowserManagement.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/BrowserManagement.java @@ -103,7 +103,7 @@ public class BrowserManagement extends RunOnFailureKeywordsAdapter { protected Element element; @Autowired - private Robot robot = new Robot(); + private Robot robot; // ############################## // Getter / Setter @@ -171,14 +171,14 @@ public void closeBrowser() { "| 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" + - "\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" + "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" + @@ -186,15 +186,15 @@ public void closeBrowser() { "| `Open Browser` | http://example.com | Edge | remote_url=http://127.0.0.1:4444/wd/hub |\r\n" + "\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", "isWebDriverManager=false", "alias=None", "remoteUrl=None", - "desiredCapabilities=None", "browserOptions=None" }) + @ArgumentNames({ "url", "browserName=firefox", "alias=None", "remoteUrl=None", "desiredCapabilities=None", + "browserOptions=None", "isWebDriverManager=false" }) public String openBrowser(String url, String... args) { String browserName = robot.getParamsValue(args, 0, "firefox"); - boolean isWebDriverManager = robot.getParamsValue(args, 1, false); - String alias = robot.getParamsValue(args, 2, "None"); - String remoteUrl = robot.getParamsValue(args, 3, "None"); - String desiredCapabilities = robot.getParamsValue(args, 4, "None"); - String browserOptions = robot.getParamsValue(args, 5, "None"); + 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); From 38b2a5dcd30bdf5dfe33eb027b54c3bd2642daf5 Mon Sep 17 00:00:00 2001 From: YauheniPo Date: Sat, 6 Jul 2019 13:43:07 +0300 Subject: [PATCH 5/5] Updated clickToFirstItem to clickElementByIndex method --- .../seleniumlibrary/keywords/Element.java | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) 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 229d38d..706c841 100644 --- a/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java +++ b/src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java @@ -891,26 +891,21 @@ public void xpathShouldMatchXTimes(String xpath, int expectedXpathCount, String. logLevel); } - @RobotKeyword("Click to First Item from List Items by Locator.") - @ArgumentNames({"locator", "childLocator=NONE", "message=NONE"}) - public void clickToFirstItem(String locator, String... params) { - String childLocator = robot.getParamsValue(params, 0, ""); - String message = robot.getParamsValue(params, 1, ""); - List elements = browserManagement.getCurrentWebDriver().findElements(By.xpath(locator)); - if (elements.isEmpty()) { - if (message.isEmpty()) { - message = String.format("The Element was not found by locator '%s' and child locator '%s'.", - locator, childLocator); - } - throw new SeleniumLibraryNonFatalException(message); - } - WebElement element = elements.get(0); - if (!childLocator.isEmpty()) { - element.findElements(By.xpath(childLocator)).get(0).click(); - } else { - element.click(); - } - } + @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