WELCOME ABOARD.

FEST-Swing -Swing GUI testing

上一篇 / 下一篇  2010-04-24 23:04:29 / 个人分类:java

 


FEST-Swing is a Java library that provides afluent interfacefor functional Swing GUI testing. This library provides an easy-to-use API that makes creation and maintenance of GUI tests easy.

Examples

The following example shows a test verifying that an error message is displayed if the user forgets to enter her password when trying to log in an application:

dialog.comboBox("domain").select("Users");
  dialog.textBox("username").enterText("leia.organa");
  dialog.button("login").click();
  dialog.optionPane().requireErrorMessage()
                     .requireMessage("Please enter your password");

http://code.google.com/p/fest/downloads/list
http://www.javaworld.com/javaworld/jw-07-2007/jw-07-fest.html

Writing your first GUI test

When writing GUI tests, use the fixtures in the packageorg.fest.swing.fixture. These fixtures provide specific methods to simulate user interaction with a GUI component and also provide assertion methods that verify the state of such GUI component. Although you could work with the FESTRobotdirectly, theRobotis too low-level and requires considerably more code than the fixtures.

There is one fixture per Swing component. Each fixture has the same name as the Swing component they can handle ending with "Fixture." For example, aJButtonFixtureknows how to simulate user interaction and verify state of aJButton.

For our first test, let's assume we have a very simpleJFramethat contains aJTextField, aJLabeland aJButton:

The expected behavior. of this GUI is: when user clicks on theJButton, the text of theJTextFieldshould be copied to theJLabel.

The following sections describe the steps necessary to test our GUI.

Creating a test from scratch

1. Enable checks for EDT access violation

FEST provides the classFailOnThreadViolationRepaintManagerthat forces a test failure if access to GUI components is not performed on the EDT. You can find more detailshere.

2. Create a fixture for a Frame. or Dialog

Depending on the GUI to test, create a fixture to handle either aFrameor aDialogin the "setUp" method of your test. The "setUp" method is the method that initializes the test fixturebeforerunningeachtest method:

  • setUpmethod (JUnit 3.8.x)
  • a method marked with@Before(JUnit 4.x)
  • a method marked with@BeforeMethod(TestNG)

The following example uses aFrameFixture:

privateFrameFixture window;

@BeforeMethodpublicvoid setUp() {
  MyFrame. frame. = GuiActionRunner.execute(newGuiQuery<MyFrame>() {protectedMyFrame. executeInEDT() {returnnewMyFrame();  
      }
  });
  window =newFrameFixture(frame);
  window.show();// shows the frame. to test}

It may seem a little weird the way we create a new instance ofMyFrame. Since creation of a frame. triggers a "paint" action, we need to create the frame. in the Event Dispatch Thread (EDT.) More details about the EDT and Swing threading can be foundhere.

3. Clean up resources used by FEST-Swing

FEST-Swing forces sequential test execution, regardless of the testing framework (JUnit or TestNG.) To do so, it uses a semaphore to give access to the keyboard and mouse to a single test. Cleaning up resources after running each test method releases the lock on such semaphore. To clean up resources simply call the methodcleanUpin the FEST-Swing fixture inside:

  • tearDownmethod (JUnit 3.8.x)
  • any method marked with@After(JUnit 4.x)
  • any method marked with@AfterMethod(TestNG)

Example:

@AfterMethodpublicvoid tearDown() {
  window.cleanUp();
}

4. Write methods to test your GUI's behavior

Start usingFEST-Swing fixturesto test your GUI. FEST-Swing fixtures simulate a user interacting with a GUI in order to verify that such GUI behaves as we expect. For our example, we need to verify that the text in theJTextFieldis copied to theJLabelwhen theJButtonis clicked:

@Testpublicvoid shouldCopyTextInLabelWhenClickingButton() {
  window.textBox("textToCopy").enterText("Some random text");
  window.button("copyButton").click();
  window.label("copiedText").requireText("Some random text");
}

As you probably guessed by now, in our example we look up UI components by their unique name.

Putting everything together

The following code listing shows the whole test that verifies the described GUI is behaving correctly:

importorg.testng.annotations.*;importorg.fest.swing.fixture.FrameFixture;publicclass FirstGUITest {privateFrameFixture window;

  @BeforeClasspublicvoid setUpOnce() {
    FailOnThreadViolationRepaintManager.install();
  }

  @BeforeMethodpublicvoid setUp() {
    MyFrame. frame. = GuiActionRunner.execute(newGuiQuery<MyFrame>() {protectedMyFrame. executeInEDT() {returnnewMyFrame();  
        }
    });
    window =newFrameFixture(frame);
    window.show();// shows the frame. to test}

  @AfterMethodpublicvoid tearDown() {
    window.cleanUp();
  }

  @Testpublicvoid shouldCopyTextInLabelWhenClickingButton() {
    window.textBox("textToCopy").enterText("Some random text");
    window.button("copyButton").click();
    window.label("copiedText").requireText("Some random text");
  }
}

Alternatively, extend a FEST-Swing test case

Starting with version 1.1, FEST-Swing provides a base test case class, to simplify creation of GUI tests. The following code listing provides the same functionality as the example from the previous section, with less code, thanks toFestSwingTestngTestCase:

importorg.testng.annotations.*;importorg.fest.swing.fixture.FrameFixture;importorg.fest.swing.testng.testcase.FestSwingTestngTestCase;publicclass FirstGUITestextendsFestSwingTestngTestCase {privateFrameFixture window;protectedvoid onSetUp() {
    MyFrame. frame. = GuiActionRunner.execute(newGuiQuery<MyFrame>() {protectedMyFrame. executeInEDT() {returnnewMyFrame();  
        }
    });// IMPORTANT: note the call to 'robot()'// we must use the Robot from FestSwingTestngTestCasewindow =newFrameFixture(robot(), frame);
    window.show();// shows the frame. to test}

  @Testpublicvoid shouldCopyTextInLabelWhenClickingButton() {
    window.textBox("textToCopy").enterText("Some random text");
    window.button("copyButton").click();
    window.label("copiedText").requireText("Some random text");
  }
}
Warning

When using a baseTestCase, do not create a newRobot. The baseTestCasecreates one for you. If there is more than oneRobotin your test, only the first one will have access to the screen, while the rest will block till they get the 'screen lock'. ARobotcan be created manually or indirectly using the constructorsFrameFixture(Frame)orDialogFixture(Dialog). Please use the overloaded versions that take aRobotas parameter, passing the already createdRobot(by calling the methodrobot().)


TAG: fest fest-swing GUI java Java swing Testing 测试 界面 testing gui

 

评分:0

我来说两句

Open Toolbar