public void clear() { for(Map.Entry<String, Object> entry : variables.entrySet()) { LOG.keywordAppender().appendProperty(String.format("Remove Variable ['%s']", entry.getKey()), entry.getValue()); } variables.clear(); } }
public void add(String key, Object value) { LOG.keywordAppender().appendProperty("Add Variable Key", key); if(value != null) { LOG.keywordAppender().appendProperty("Add Variable Class", value.getClass().getName()); } LOG.keywordAppender().appendProperty("Add Variable Value", value); variables.put(key, value); }
public static Object singleProperty(JSONObject obj, String propertyName) { LOG.keywordAppender().appendProperty("JSON Property Name", propertyName); Object propertyValue = obj.get(propertyName); LOG.keywordAppender().appendProperty("JSON Property Value", propertyValue); return propertyValue; }
@Override public Object evaluate(String expression) throws Exception { LOG.keywordAppender().appendProperty("Expression Handler", "Expression Language (JUEL)"); DefaultELContext context = new DefaultELContext(functionManager, getVariables()); TreeValueExpression expr = (TreeValueExpression) factory.createValueExpression(context, String.format("${%s}", expression), TypeExpressionHolder.peek()); Set<String> vars = VariableNodeCapture.capture((Node) nodeField.get(expr)); for(Map.Entry<String, Object> var : getVariables().entrySet()) { if(vars.contains(var.getKey())) { LOG.keywordAppender().appendProperty(String.format("EL Variable ['%s']", var.getKey()), var.getValue()); } } Object result = expr.getValue(context); LOG.keywordAppender().appendProperty("EL Result", result); return result; } }
public static String text(Object... args) throws TransformerException, IOException, SAXException, XPathExpressionException, ParserConfigurationException { if(Element.class.isInstance(args[0]) && args.length == 1) { Element element = (Element) args[0]; String result = element.getTextContent(); LOG.keywordAppender().appendProperty("Result", result); return result; } else if(args.length == 2 && Element.class.isInstance(args[0]) && String.class.isInstance(args[1])) { return element(args).getTextContent(); } else if(args.length == 2 && args[0] == null && String.class.isInstance(args[1])) { return text(args[1]); } else if(String.class.isInstance(args[0])) { String xpath = (String) args[0]; LOG.keywordAppender().appendProperty("Xpath", xpath); List<Element> els = elements(xpath); if(CollectionUtils.isEmpty(els)) { throw new IllegalArgumentException(String.format("No elements found for xpath '%s'.", xpath)); } String result = els.iterator().next().getTextContent(); LOG.keywordAppender().appendProperty("Result", result); return result; } throw new IllegalArgumentException("Expected 'xml:text(element)' or 'xml:text(xpath)'."); }
public static String replaceVars(String string) throws Exception { StringBuilder buf = new StringBuilder(string); Matcher matcher = PATTERN.matcher(buf); int startIndex = 0; while (startIndex < buf.length() && matcher.find(startIndex)) { String name = matcher.group(1); Object value = getVariables().getVariables().get(name); LOG.keywordAppender().appendProperty("Replacement EL Value ['" + name + "']", value); if (value == null) { value = robotVar(name); LOG.keywordAppender().appendProperty("Replacement Robot Value ['" + name + "']", value); } if (value == null) { value = System.getProperty(name); LOG.keywordAppender().appendProperty("Replacement System Property Value ['" + name + "']", value); } if (value == null) { value = System.getenv(name); LOG.keywordAppender().appendProperty("Replacement System Environment Value ['" + name + "']", value); } String strValue = String.valueOf(value); buf.replace(matcher.start(), matcher.end(), strValue); startIndex = matcher.start() + strValue.length(); } if (!StringUtils.equals(string, buf.toString())) { LOG.keywordAppender().appendProperty(String.format("Replacement ['%s']", string), buf.toString()); } return buf.toString(); }
.appendProperty("Attribute", attribute) .appendProperty("Result", result); .appendProperty("Xpath", xpath) .appendProperty("Attribute", attribute); LOG.keywordAppender().appendProperty("Result", result); String attribute = (String) args[0]; LOG.keywordAppender().appendProperty("Root Attribute", attribute); LOG.keywordAppender().appendProperty("Result", result);
LOG.keywordAppender().appendProperty("Random index", indices.get(i));
private static Object doPythonExec(String keyword, List<Object> params) { PythonInterpreter interpreter = MainContextHolder.get().getBean(PythonInterpreter.class); interpreter.set("keyword", keyword); interpreter.set("args", params); interpreter.exec( "from robot.libraries.BuiltIn import BuiltIn\n" + "result= BuiltIn().run_keyword(keyword, *args)\n" ); Object result = interpreter.get("result"); if (result != null) { LOG.keywordAppender().appendProperty(String.format("runKeyword('%s')", keyword), result.getClass()); } else { LOG.keywordAppender().appendProperty(String.format("runKeyword('%s')", keyword), null); return null; } Object javaObject = PythonUtils.toJava(result); if (javaObject != null) { LOG.keywordAppender().appendProperty(String.format("runKeyword('%s')", keyword), javaObject.getClass()); } LOG.keywordAppender().appendProperty(String.format("runKeyword('%s')", keyword), javaObject); return javaObject; }
public static Object robotVar(String name) throws NoSuchFieldException, IllegalAccessException { if (MainContextHolder.get() == null) { throw new IllegalStateException("Not running on robot framework runtime."); } String robotVarName = String.format("\\${%s}", name); PythonInterpreter interpreter = MainContextHolder.get().getBean(PythonInterpreter.class); interpreter.set("name", robotVarName); interpreter.exec( "from robot.libraries.BuiltIn import BuiltIn\n" + "result= BuiltIn().get_variable_value(name)\n" ); Object result = interpreter.get("result"); if (result != null) { LOG.keywordAppender().appendProperty(String.format("robotVar('%s')", name), result.getClass()); } else { LOG.keywordAppender().appendProperty(String.format("robotVar('%s')", name), null); return null; } Object javaObject = PythonUtils.toJava(result); if (javaObject != null) { LOG.keywordAppender().appendProperty(String.format("robotVar('%s')", name), javaObject.getClass()); } LOG.keywordAppender().appendProperty(String.format("robotVar('%s')", name), javaObject); return javaObject; }
public static Object property(Object... args) throws TransformerException { if(args[0] == null && args.length == 2 && String.class.isInstance(args[1])) { return property(args[1]); } else if(JSONObject.class.isInstance(args[0]) && args.length == 2) { JSONObject obj = (JSONObject) args[0]; String property = (String) args[1]; return singleProperty(obj, property); } else if(String.class.isInstance(args[0]) && args.length == 2) { String jsonPath = (String) args[0]; String property = (String) args[1]; LOG.keywordAppender().appendProperty("JSON Path", jsonPath); List items = objects(jsonPath); if(CollectionUtils.isEmpty(items)) { throw new IllegalArgumentException(String.format("No objects found for jsonPath '%s'", jsonPath)); } JSONObject obj = (JSONObject) items.iterator().next(); return singleProperty(obj, property); } else if(String.class.isInstance(args[0]) && args.length == 1) { String jsonPath = (String) args[0]; LOG.keywordAppender().appendProperty("JSON Path", jsonPath); return getHelper().getJsonValue(jsonPath); } throw new IllegalArgumentException("Expected 'json:property(propertyName)' or 'json:property(jsonObject, propertyName)' or 'json:property(jsonPath, propertyName)'."); }
public static Object eval(final String expression, Object... args) throws Exception { LOG.keywordAppender().appendProperty("(eval) Expression", expression); if (args != null && args.length > 0) { LOG.keywordAppender().appendProperty("(eval) Arguments", args); } if (args == null || args.length == 0) { return getHelper().evaluate(expression); } return getHelper().variableScope(Arrays.asList(args), new Callable<Object>() { @Override public Object call() throws Exception { return getHelper().evaluate(expression); } }); }