Skip to content

Commit

Permalink
Merge pull request #239 from hastebrot/fix-focus-of-write-robot
Browse files Browse the repository at this point in the history
(fix) `WriteRobot::write()` to prefer the `Scene` of the focused window.
  • Loading branch information
hastebrot committed Dec 5, 2015
2 parents 9cfa735 + 29d31c0 commit aabafdb
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.stage.Window;

import com.google.common.collect.Lists;
import org.testfx.api.annotation.Unstable;
Expand Down Expand Up @@ -61,13 +62,15 @@ public WriteRobotImpl(BaseRobot baseRobot,

@Override
public void write(char character) {
pushCharacter(character);
Scene scene = fetchTargetWindow().getScene();
typeCharacterInScene(character, scene);
}

@Override
public void write(String text) {
Scene scene = fetchTargetWindow().getScene();
for (char character : Lists.charactersOf(text)) {
pushCharacter(character);
typeCharacterInScene(character, scene);
sleepRobot.sleep(SLEEP_AFTER_CHARACTER_IN_MILLIS);
}
}
Expand All @@ -76,8 +79,16 @@ public void write(String text) {
// PRIVATE METHODS.
//---------------------------------------------------------------------------------------------

private void pushCharacter(char character) {
Scene scene = windowFinder.targetWindow().getScene();
private Window fetchTargetWindow() {
Window targetWindow = windowFinder.window(window -> window.isFocused());
if (targetWindow == null) {
targetWindow = windowFinder.targetWindow();
}
return targetWindow;
}

private void typeCharacterInScene(char character,
Scene scene) {
KeyCode key = determineKeyCode(character);
baseRobot.typeKeyboard(scene, key, Character.toString(character));
baseRobot.awaitEvents();
Expand Down

0 comments on commit aabafdb

Please sign in to comment.