我在OS X上的windowlicker有问题(在Windows上一切正常)。
问题是,当我尝试模拟用户对任何文本字段的输入时,数据未正确插入(某些字母被切出)。

例如:

JTextFieldDriver txField = new JTextFieldDriver(this,
                                                JTextField.class,
                                                named(fieldName));
txField.focusWithMouse();
txField.typeText(input);


前面的代码将导致我观察到windowlicker将输入插入到名为fieldName的文本字段中,并且输入将不完整(Peter将是Peer或Fred将是Fre,依此类推)。在Windows上一切正常。

我不确定这是否与警告有关。我在Windows上得到类似的东西。警告是:
“警告:无法使用功能降低的后备布局加载键盘布局Mac-(在/Users/odo/.m2/repository/com/googlecode/windowlicker/windowlicker中找不到JAR条目com / objogate / wl / keyboard / Mac- -core / r268 / windowlicker-core-r268.jar)”

最佳答案

Windowlicker似乎不是很流行的工具。不过,我设法找出了根本原因。显示警告,指出无法设置键盘布局,因为我没有使用英语语言环境。看起来windowlicker仅支持Mac-GB键盘布局。
如果设置了适当的系统属性,警告将消失。
例如:

System.setProperty("com.objogate.wl.keyboard", "Mac-GB");


但是,这不能解决主要问题。经过几次试验,我发现只对'a'和'd'字符进行了修整。这是因为windowlicker会插入它们,就像用户将按住“ a”或“ d”键一会儿一样。按住这些键将导致一个帮助菜单调用,该菜单允许选择特殊字符。为了修复该问题,我使用了JTextComponentDriver并找到了解决方法。解决方案是不使用驱动程序的typeText插入文本。 JTextComponentDriver的component()方法可用于检索实际的Guy组件,然后可以调用实例setText()来设置文本。

下面,我介绍使用描述的解决方案的助手类:

public class TextTyper {
    private final String inputText;

    privte TextTyper(String inputText) {
        this.inputText = inputText;
    }

    public static TextTyper typeText( final String inputText ){
        return new TextTyper( inputText );
    }

    public void into( JTextComponentDriver<?> driver ) throws Exception{
        driver.focusWithMouse();
        driver.clearText();

        Component cmp = driver.component().component();
        if(cmp instanceof JPasswordField ){
            JPasswordField pwField = (JPasswordField) cmp;
            pwField.setText(this.inputText);
        }
        else if( cmp instanceof JTextField){
            JTextField txField = (JTextField) cmp;
            txField.setText(this.inputText);
        }

        else
            throw new Exception("Component is not an instance of JTextField or JPasswordField");
    }
}

08-06 13:31