|
| 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 | +} |
0 commit comments