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?
- Create new Web Application - Call it MyAppSelenium
- (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'.
- Right click on project > Properties > Libraries do Add Library > JUnit for both Compile Tests and Run Tests in two correspond tabs.
- 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.
- Right click on Test Packages > New > Java class > testMyApp.java, write your test
- Run server 'java -jar selenium-server-***.jar'
- Run Play app 'play run myapp'
- 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