Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add keywords to call methods from custom components #65

Merged
merged 1 commit into from
Apr 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywordOverload;
import org.robotframework.javalib.annotation.RobotKeywords;
import org.robotframework.javalib.reflection.ArgumentConverter;
import org.robotframework.swing.comparator.EqualsStringComparator;
import org.robotframework.swing.component.ComponentOperator;
import org.robotframework.swing.component.ComponentOperatorFactory;
Expand All @@ -33,7 +34,8 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.util.ArrayList;
import java.lang.reflect.Method;
import java.util.*;
import java.util.List;

@RobotKeywords
Expand Down Expand Up @@ -163,6 +165,53 @@ public void scrollComponentToView(String identifier) {
operator(identifier).scrollRectToVisible(new Rectangle(100, 100));
}

@RobotKeyword("List Component Methods")
@ArgumentNames({"identifier"})
public String[] listComponentMethods(String identifier) {
Class klass = operator(identifier).getSource().getClass();
ArrayList<String> list = new ArrayList<String>();

for (Method m : klass.getMethods()) {
String entry = "";
entry += m.getReturnType().getName() + " ";
entry += m.getName();
entry += "(";
Class[] args = m.getParameterTypes();
for (int i = 0; i < args.length; i++) {
entry += args[i].getName();
if (i != args.length - 1)
entry += ", ";
}
entry += ")";
System.out.println(entry);
list.add(entry);
}
return list.toArray(new String[list.size()]);
}

private Method getMethodByNameAndArgumentCount(Class klass, String name, int argCount) {
for (Method m : klass.getMethods()) {
if (m.getName().equals(name) && m.getParameterTypes().length == argCount)
return m;
}
throw new RuntimeException(String.format("Method \"%s\" with %d argument(s) doesn't exist.", name, argCount));
}

@RobotKeyword("Call Component Method")
@ArgumentNames({"identifier", "method", "*args"})
public Object callComponentMethod(String identifier, String method, String[] args) {
Object component = operator(identifier).getSource();
Class klass = component.getClass();
Method m = getMethodByNameAndArgumentCount(klass, method, args.length);

try {
return m.invoke(component, new ArgumentConverter(m.getParameterTypes()).convertArguments(args));
} catch (Exception e) {
throw new RuntimeException(e);
}
}


private ComponentOperator operator(String identifier) {
return operatorFactory.createOperator(identifier);
}
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/robot-tests/componentkeywords.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*** Settings ***
Library TestSwingLibrary
Library Collections
Variables platform_info.py

*** Variables ***
Expand Down Expand Up @@ -63,6 +64,16 @@ Get Tooltip Text
${tooltip}= getToolTipText testLabel
shouldBeEqual TEST LABEL ${tooltip}

List Component Methods
${methods}= listComponentMethods ${buttonName}
listShouldContainValue ${methods} java.lang.String getToolTipText()

Call Component Method
${tooltipText}= setVariable tooltip test
callComponentMethod ${buttonName} setToolTipText ${tooltipText}
${tooltipValue}= callComponentMethod ${buttonName} getToolTipText
Should Be Equal ${tooltipText} ${tooltipValue}

*** Keywords ***
resetButton
setButtonText ${buttonName} Test Button
Expand Down