Sunday, February 5, 2012

Testing Play! Framework Web App with Netbeans + Selenium RC

Why?
  • Play's selenium test is not reliable since it's flash scope is cleared every 2 seconds (means that test can be success or fail base on the loading time).
  • Upload file not work
What?
How?
  1. Create new Web Application - Call it MyAppSelenium
  2. (If there's no Test Packages label) Right click on project > Properties > Sources look for Test Packages Folders . Create a new folder with test folder name (default 'test') and the same level with 'src'.
  3. Right click on project > Properties > Libraries do Add Library > JUnit for both Compile Tests and Run Tests in two correspond tabs.
  4. Unzip Selenium RC client driver. Right click on project > Properties > Libraries do Add JAR/Folder > select selenium client jar file (e.g. selenium-java-2.18.0.jar) for both Compile Tests and Run Tests.
  5. Right click on Test Packages > New > Java class > testMyApp.java, write your test
  6. Run server 'java -jar selenium-server-***.jar'
  7. Run Play app 'play run myapp'
  8. Right click 'testMyApp.java' > Test File(Ctrl+F6)
Done.

  • Template for writing 'testMyApp.java':
import com.thoughtworks.selenium.*;
import org.junit.*;

public class testMyApp {

    static private String appServerURL = "http://localhost:9000/";

    @Test
    public void testSimple() throws Exception {

        Selenium selBrowser = new DefaultSelenium("localhost", 4444, "*firefox", appServerURL);
        selBrowser.start();
      
        //Home page
        selBrowser.open("/");
        selBrowser.waitForPageToLoad("1000");
        Assert.assertTrue(selBrowser.isTextPresent("Hello World!"));       
        selBrowser.stop();

    }
}
  • Write a play.Unittest (call it 'preTest') and do following in case need to setup data for test.
Fixtures.deleteAll()
Fixtures.load('test-data.yml')
Run 'play test myapp' instead, go to http://localhost:9000/@tests, choose  'preTest' > Start. Back to Netbeans and run your test.






No comments:

Post a Comment