1

I am using assertj-swing to test a simple GUI application. When I enter text in a JTextField, I am getting: IllegalArgumentException: Invalid key code '65406'

It seems it happens because I am using a german keyboard layout, so to solve this problem I added the code below into onSetUp method:

window.target().getInputContext().selectInputMethod(new Locale("en", "US"));

By changing the input method the test passed, but I could not restore the original keyboard layout after the test, thus after testing my layout is always en-US, instead of de-DE. I tried to set direct to de-DE in the method onTearDown using the line above, or store the current layout onSetUp and restore onTearDown, but the value I am writing into selectInputMethod is always overwritten before I restore it or even after, so that sometimes I see it changes to de-DE and them back en-US.

Does someone had similar problems trying to configure the keyboard layout?

Below you can read an example of the tests I am having trouble with:

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.util.Locale;

import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class GuiExampleTest extends AssertJSwingJUnitTestCase {

    private FrameFixture window;
    private TransformationGui gui;

    @Rule
    public TemporaryFolder workingPath = new TemporaryFolder();

    @Override
    protected void onSetUp() {
        gui = GuiActionRunner.execute(() -> new TransformationGui());
        window = new FrameFixture(robot(), gui);
        window.target().getInputContext().selectInputMethod(new Locale("en", "US"));
        window.show();
    }

    @Override
    protected void onTearDown() {
        window.cleanUp();
    }

    @Test  
    public void acceptMyField() throws Exception {
        window.textBox("myfield").enterText(givenSomeExcelFile().toString());
        
        assertThat(window.textBox("myfield").text()).endsWith("SomeFile.xlsx");
    }
    
    private File givenSomeExcelFile() throws Exception {
        return workingPath.newFile("SomeFile.xlsx");
    }
}
  • 1
    I'm not sure if this can help you, but I ran in a different (but also similar) problem recently. Instead of changing the layout like this have you tried setting it via VM arguments? In your case something like this `-Dassertj.swing.keyboard.locale=en` I think. See https://github.com/assertj/assertj-swing/issues/199 – ClaudiaR Feb 12 '22 at 17:10

0 Answers0