首页 > 解决方案 > 改变 TestFX 机器人的速度?

问题描述

特别是WriteRobot/ WriteRobotImpl。它似乎写得很慢,我想让它写得更快。

编辑
为了回应MS的评论,我尝试了这个(注意此时我还没有弄清楚WriteRobot所涉及的内容,而不是TypeRobot):

setup(){
...
    setFinalStatic( org.testfx.robot.impl.TypeRobotImpl.class.getDeclaredField("SLEEP_AFTER_KEY_CODE_IN_MILLIS"), 5 );
}
...
static void setFinalStatic(Field field, Object newValue) throws Exception {
    field.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, newValue);
}

不幸的是,即使设置为 1 毫秒,它似乎对打字速度也没有影响。

编辑
我注意到 Slaw 的评论。

我在运行测试之前设置了该System属性testfx.robot.write_sleep:尽管可以从 WriteRobotImpl.java 顶部的源代码中看到它可能有,但这没有效果(见下文)。当我将其设置为 500 毫秒时,它也没有任何效果,这让我得出结论,由于某种原因,那里的代码没有看到该属性,因此设置了默认的 25 毫秒。

注意可能的其他原因:按照那里的代码,似乎WriteRobot.write总是导致调用WriteRobot.typeCharacterInScene,然后调用BaseRobot.typeKeyboardand WaitForAsyncUtils.waitForFxEvents。后者可能是一个“难缠的客户”:如果每个按下的键都必须“等待事件”冒泡,那么事情很可能无事可做。

仍在尝试找出为什么 org.testfx.robot.impl.WriteRobotImpl.java 顶部的以下行将无法看到该System属性:

private static final int SLEEP_AFTER_CHARACTER_IN_MILLIS;

static {
    int writeSleep;
    try {
        writeSleep = Integer.getInteger("testfx.robot.write_sleep", 25);
    }
    catch (NumberFormatException e) {
        System.err.println("\"testfx.robot.write_sleep\" property must be a number but was: \"" +
                System.getProperty("testfx.robot.write_sleep") + "\".\nUsing default of \"25\" milliseconds.");
        e.printStackTrace();
        writeSleep = 25;
    }
    SLEEP_AFTER_CHARACTER_IN_MILLIS = writeSleep;
}

我还想知道该static{...}代码块是否发生得早于您需要System在测试运行之前设置属性。我尝试在 gradle.build 中设置此属性。仍然没有成功。

标签: javajavafxrobottestfx

解决方案


推荐阅读