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

Commit 15982cc

Browse files
committed
Added keywords for data utils
1 parent 891dbe5 commit 15982cc

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

src/main/java/com/github/markusbernhardt/seleniumlibrary/CustomRobotDriverElement.java

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

33
import com.github.markusbernhardt.seleniumlibrary.keywords.BrowserManagement;
4+
import com.github.markusbernhardt.seleniumlibrary.keywords.Logging;
45
import com.github.markusbernhardt.seleniumlibrary.keywords.Robot;
56
import org.openqa.selenium.WebDriver;
67

@@ -16,7 +17,8 @@ public class CustomRobotDriverElement {
1617

1718
private static SeleniumLibrary s;
1819
private static BrowserManagement b;
19-
private Robot robot = new Robot();
20+
protected Robot robot = new Robot();
21+
protected Logging logging = new Logging();
2022

2123
public CustomRobotDriverElement() throws NoSuchFieldException, IllegalAccessException {
2224
try {
@@ -41,9 +43,4 @@ private static SeleniumLibrary getLibraryInstance() throws ScriptException {
4143
protected WebDriver getCurrentBrowser() {
4244
return b.getCurrentWebDriver();
4345
}
44-
45-
protected Robot getRobot() {
46-
return robot;
47-
}
48-
4946
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.github.markusbernhardt.seleniumlibrary.keywords;
2+
3+
import com.github.markusbernhardt.seleniumlibrary.RunOnFailureKeywordsAdapter;
4+
import org.robotframework.javalib.annotation.ArgumentNames;
5+
import org.robotframework.javalib.annotation.Autowired;
6+
import org.robotframework.javalib.annotation.RobotKeyword;
7+
import org.robotframework.javalib.annotation.RobotKeywords;
8+
9+
import java.util.ArrayList;
10+
import java.util.Collections;
11+
import java.util.List;
12+
import java.util.regex.Matcher;
13+
import java.util.regex.Pattern;
14+
15+
@RobotKeywords
16+
public class DataUtils extends RunOnFailureKeywordsAdapter {
17+
18+
private static final String ASCENDING = "ascending";
19+
private static final String DESCENDING = "descending";
20+
21+
@Autowired
22+
protected Robot robot;
23+
24+
/**
25+
* Instantiated Logging keyword bean
26+
*/
27+
@Autowired
28+
protected Logging logging;
29+
30+
// ##############################
31+
// Keywords
32+
// ##############################
33+
34+
@RobotKeyword
35+
@ArgumentNames({"list", "order=ascending"})
36+
public List<String> sortStringList(String listToString, String... params) {
37+
List<String> listOfStrings = robot.parseRobotList(listToString);
38+
String order = robot.getParamsValue(params, 0, ASCENDING);
39+
List<String> sortedList = new ArrayList<>(listOfStrings);
40+
if (order.equalsIgnoreCase(DESCENDING)) {
41+
listOfStrings.sort(Collections.reverseOrder());
42+
} else {
43+
Collections.sort(listOfStrings);
44+
}
45+
logging.info(String.format("Sorted list '%s' by %s", sortedList, order.toUpperCase()));
46+
return sortedList;
47+
}
48+
49+
@RobotKeyword
50+
@ArgumentNames({"text", "regExpPattern", "groupIndex"})
51+
public String getStringByRegexpGroup(String text, String regExpPattern, int groupIndex) {
52+
Matcher matcher = Pattern.compile(regExpPattern).matcher(text);
53+
matcher.find();
54+
String matchedText = matcher.group(groupIndex);
55+
logging.info(String.format("Matched text '%s' by pattern '%s' and group index '%d' from the text '%s'", matchedText, regExpPattern, groupIndex, text));
56+
return matchedText;
57+
}
58+
59+
@RobotKeyword
60+
@ArgumentNames({"text", "regExpPattern"})
61+
public boolean isTextMatchPattern(String text, String regExpPattern) {
62+
return Pattern.compile(regExpPattern).matcher(text).matches();
63+
}
64+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,8 @@ public void xpathShouldMatchXTimes(String xpath, int expectedXpathCount, String.
934934
@RobotKeyword("Click to element from list elements by locator ``xpath``.")
935935
@ArgumentNames({"xpath", "index=0", "message=NONE"})
936936
public void clickElementByIndex(String xpath, String... params) {
937-
String message = robot.getParamsValue(params, 0, "");
938-
int index = robot.getParamsValue(params, 1, 0);
937+
int index = robot.getParamsValue(params, 0, 0);
938+
String message = robot.getParamsValue(params, 1, "");
939939
List<WebElement> elements = elementFind(xpath, false, false);
940940
if (elements.isEmpty()) {
941941
if (message.isEmpty()) {

0 commit comments

Comments
 (0)