-
Notifications
You must be signed in to change notification settings - Fork 16
added RobotString class for working with system properties and string… #74
Changes from 3 commits
d7c69cc
562d59f
47c60db
6b3b3ec
68e00e1
5cc545d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.github.markusbernhardt.seleniumlibrary; | ||
|
||
import com.github.markusbernhardt.seleniumlibrary.keywords.BrowserManagement; | ||
import com.github.markusbernhardt.seleniumlibrary.keywords.Robot; | ||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.openqa.selenium.WebDriver; | ||
|
||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
import javax.script.ScriptException; | ||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* For extends of custom keywords of WebDriver actions | ||
*/ | ||
public class CustomRobotDriverElement { | ||
|
||
private static SeleniumLibrary s; | ||
private static BrowserManagement b; | ||
private Robot robot = new Robot(); | ||
|
||
public CustomRobotDriverElement() throws NoSuchFieldException, IllegalAccessException { | ||
try { | ||
CustomRobotDriverElement.s = getLibraryInstance(); | ||
} catch (ScriptException e) { | ||
throw new SeleniumLibraryNonFatalException("Cannot create SeleniumLibrary instance: \n" | ||
+ ExceptionUtils.getStackTrace(e)); | ||
} | ||
Field bmField = SeleniumLibrary.class.getDeclaredField("bm"); | ||
bmField.setAccessible(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we change the visibility back to the previous state after getting what we need? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
b = (BrowserManagement) bmField.get(s); | ||
bmField.setAccessible(false); | ||
} | ||
|
||
private static SeleniumLibrary getLibraryInstance() throws ScriptException { | ||
ScriptEngine engine = new ScriptEngineManager().getEngineByName("python"); | ||
engine.put("library", "SeleniumLibrary"); | ||
engine.eval("from robot.libraries.BuiltIn import BuiltIn"); | ||
engine.eval("instance = BuiltIn().get_library_instance(library)"); | ||
return (SeleniumLibrary) engine.get("instance"); | ||
} | ||
|
||
protected static WebDriver getCurrentBrowser() { | ||
Hi-Fi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return b.getCurrentWebDriver(); | ||
} | ||
|
||
protected Robot getRobot() { | ||
return robot; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.github.markusbernhardt.seleniumlibrary.keywords; | ||
|
||
import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.robotframework.javalib.annotation.ArgumentNames; | ||
import org.robotframework.javalib.annotation.Autowired; | ||
import org.robotframework.javalib.annotation.RobotKeyword; | ||
import org.robotframework.javalib.annotation.RobotKeywords; | ||
|
||
@RobotKeywords | ||
public class RobotString extends RunOnFailureKeywordsAdapter { | ||
|
||
/** | ||
* Instantiated Logging keyword bean | ||
*/ | ||
@Autowired | ||
protected Logging logging; | ||
|
||
@RobotKeyword("Set System Property") | ||
Hi-Fi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@ArgumentNames({"key", "value"}) | ||
public void setSystemProperty(String key, String value) { | ||
System.setProperty(key, value); | ||
} | ||
|
||
@RobotKeyword("Get System Property") | ||
Hi-Fi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@ArgumentNames({"key"}) | ||
public String getSystemProperty(String key) { | ||
return System.getProperty(key); | ||
} | ||
|
||
@RobotKeyword("Is Contain String Ignore Case") | ||
@ArgumentNames({"str", "searchStr"}) | ||
public boolean isContainStringIgnoreCase(String str, String searchStr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this duplicate witn https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Should%20Contain? Also, if it's useful to redefine, same naming should be used. E.g. "Should Contain With Case Ignored" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with you) Deleted |
||
logging.info(String.format("Is Contain String Ignore Case: Value - '%s'; String - '%s'", str, searchStr)); | ||
return StringUtils.containsIgnoreCase(str, searchStr); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code can be simplified by just providing an exception as the second parameter:
https://github.com/Hi-Fi/robotframework-seleniumlibrary-java/blob/cbe8ecb27069dfc5f24f5dd0e3e944683a37caaf/src/main/java/com/github/markusbernhardt/seleniumlibrary/SeleniumLibraryNonFatalException.java#L27-L29
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done