英文:
Change speed of TestFX robots?
问题
具体地说,WriteRobot
/ WriteRobotImpl
部分。它似乎写东西的速度比较慢,我希望能让它写得更快。
编辑
针对M.S.的评论,我尝试了以下操作(注意,在这个时候我还没有弄清楚是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.typeKeyboard
和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
中设置这个属性,但仍然没有成功。
英文:
Specifically the WriteRobot
/ WriteRobotImpl
. It seems to write things rather slowly and I'd like to make it write faster.
Edit<br>
In response to M.S.'s comment I tried this (NB at this point I hadn't worked out that WriteRobot
was involved, not 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);
}
Unfortunately it appears to make no difference to the typing speed, even when set to 1 ms.
Edit<br>
I note the comment by Slaw.
I set the System
property testfx.robot.write_sleep
before running the test: this had no effect, despite one being able to see that it might have, from the source code at the top of WriteRobotImpl.java (see below). When I set this to 500 ms it also had no effect, making me conclude that the property was not being seen by the code there for some reason, so the default 25 ms was being set.
NB possible other causes: following the code there, it appears that WriteRobot.write
always results in a call to WriteRobot.typeCharacterInScene
, which in turn calls BaseRobot.typeKeyboard
and WaitForAsyncUtils.waitForFxEvents
. The latter might be a "difficult customer": if each pressed key then has to "wait for events" to bubble up, there may well be nothing to be done about things.
Still trying to work out why the following lines at the top of org.testfx.robot.impl.WriteRobotImpl.java would fail to see the System
property:
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;
}
I also wondered whether maybe that static{...}
code block happens so early than you need to set the System
property before the tests are run. I tried setting this property in gradle.build. Still no success.
专注分享java语言的经验与见解,让所有开发者获益!
评论