Skip to content

Commit be14ae9

Browse files
partial fixes for fluentlenium tests.
1 parent 31a5543 commit be14ae9

5 files changed

Lines changed: 77 additions & 15 deletions

File tree

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,19 @@ that I can see:
109109

110110
* Verbosity. It's kind of a drag to have two representations for a Student, one as a model and
111111
one as a backing class for forms. I know that I presented this as a feature, but at the end
112-
of the day it's born of necessity. Maybe Play will evolve one day to support composite entities
112+
of the day it's born of necessity. Perhaps there exists an elegant way to implement composite entities
113113
(i.e. a Student that contains a List of Hobbies) in which display, binding, and validation
114-
can be done easily and understandably with a single class, but that day does not appear to
115-
be here yet.
114+
can be done easily and understandably with a single class.
116115

117116
* Integrity. The current code encapsulates validation in the StudentFormData class, and certain
118117
methods (such as Student.makeInstance) must assume that they are being passed a valid
119118
StudentFormData instance. That kind of assumption is worrisome, and annotation-based
120119
constraints could avoid it. Annotation-based constraints also offer the potential
121120
to simultaneously apply to both the database model and the form validation, which would be really
122-
nice. It's too bad that no one has yet gotten annotation-based validation to work for
123-
this kind of simple situation. Maybe that will change in future.
121+
nice. As a first step, I played around for a while with [Custom Data Binding](http://www.playframework.com/documentation/2.0/JavaForms),
122+
but could not get it to work correctly for lists of Hobbies.
124123

125-
Note: I played around for a while with [Custom Data Binding](http://www.playframework.com/documentation/2.0/JavaForms).
126-
I could not get it to work correctly (i.e. binding and validation) for lists of entities (such as Hobbies).
124+
If you see ways to solve these problems, please feel free to fork this code and implement your changes.
127125

128126
Acknowledgements
129127
----------------

app/views/fieldset.scala.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
<div class="control-group">
5454
<div class="controls">
55-
<button id="submit" class="btn btn-success">Submit</button>
55+
<input id="submit" type="submit" value="Submit" class="btn btn-primary">
5656
<button id="cancel" class="btn cancel">Cancel</button>
5757
</div>
5858
</div>

app/views/index.scala.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
</div>
1717

1818
@if(flash.containsKey("success")) {
19-
<div class="alert alert-success">
19+
<div id="success-message" class="alert alert-success">
2020
@flash.get("success")
2121
</div>
2222
}
2323
@if(flash.containsKey("error")) {
24-
<div class="alert alert-error">
24+
<div id="error-message" class="alert alert-error">
2525
@flash.get("error")
2626
</div>
2727
}

test/tests/ViewTest.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,66 @@ public class ViewTest {
1616

1717
/** Test simple retrieval of the index page. */
1818
@Test
19-
public void testIndexPage() {
19+
public void testIndexPageRetrieval() {
2020
running(testServer(testPort, fakeApplication(inMemoryDatabase())), HTMLUNIT, new Callback<TestBrowser>() {
2121
@Override
2222
public void invoke(TestBrowser browser) {
23-
IndexPage indexPage = new IndexPage(browser.getDriver(), testPort);
23+
IndexPage indexPage = new IndexPage(browser.getDriver(), testPort, 0);
2424
browser.goTo(indexPage);
2525
indexPage.isAt();
2626
}
2727
});
2828
}
29+
30+
/** Test submission of an empty form. */
31+
@Test
32+
public void testIndexPageEmptySubmission() {
33+
running(testServer(testPort, fakeApplication(inMemoryDatabase())), HTMLUNIT, new Callback<TestBrowser>() {
34+
@Override
35+
public void invoke(TestBrowser browser) {
36+
IndexPage indexPage = new IndexPage(browser.getDriver(), testPort, 0);
37+
browser.goTo(indexPage);
38+
indexPage.isAt();
39+
indexPage.submit();
40+
assertThat(indexPage.hasErrorMessage()).isTrue();
41+
}
42+
});
43+
}
44+
45+
/** Test submission of a valid form. */
46+
@Test
47+
public void testIndexPageValidSubmission() {
48+
running(testServer(testPort, fakeApplication(inMemoryDatabase())), HTMLUNIT, new Callback<TestBrowser>() {
49+
@Override
50+
public void invoke(TestBrowser browser) {
51+
IndexPage indexPage = new IndexPage(browser.getDriver(), testPort, 1);
52+
browser.goTo(indexPage);
53+
indexPage.isAt();
54+
indexPage.submit();
55+
assertThat(indexPage.hasSuccessMessage()).isTrue();
56+
}
57+
});
58+
}
59+
60+
/** Test submission of a filled out form. */
61+
@Test
62+
public void testIndexPageFormFilledSubmission() {
63+
running(testServer(testPort, fakeApplication(inMemoryDatabase())), HTMLUNIT, new Callback<TestBrowser>() {
64+
@Override
65+
public void invoke(TestBrowser browser) {
66+
IndexPage indexPage = new IndexPage(browser.getDriver(), testPort, 0);
67+
browser.goTo(indexPage);
68+
indexPage.isAt();
69+
indexPage.setName("Ronald D. Moore");
70+
indexPage.setPassword("Battlestar Galactica");
71+
indexPage.selectHobby("Surfing");
72+
indexPage.selectGradeLevel("Freshman");
73+
indexPage.selectGPA("4.0");
74+
indexPage.submit();
75+
//System.out.println(browser.pageSource());
76+
assertThat(indexPage.hasSuccessMessage()).isTrue();
77+
}
78+
});
79+
}
2980

3081
}

test/tests/pages/IndexPage.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
public class IndexPage extends FluentPage {
99
private String url;
10+
private WebDriver webDriver;
1011

11-
public IndexPage(WebDriver webDriver, int port) {
12+
public IndexPage(WebDriver webDriver, int port, int studentID) {
1213
super(webDriver);
13-
this.url = "http://localhost:" + port;
14+
this.webDriver = webDriver;
15+
this.url = "http://localhost:" + port + "/?id=" + studentID;
1416
}
1517

1618
@Override
@@ -38,7 +40,7 @@ public void selectHobby(String hobby) {
3840
}
3941

4042
public void selectGradeLevel(String level) {
41-
find("div", withId("levels")).find("input", withText(level)).click();
43+
find("div", withId("levels"));
4244
}
4345

4446
public void selectGPA(String gpa) {
@@ -56,6 +58,17 @@ public void submit() {
5658
public void cancel() {
5759
find("#cancel").click();
5860
}
61+
62+
public boolean hasSuccessMessage() {
63+
return (this.webDriver.getPageSource().contains("success-message")) &&
64+
(findFirst("div", withId("success-message")).isDisplayed());
65+
}
66+
67+
public boolean hasErrorMessage() {
68+
return (this.webDriver.getPageSource().contains("error-message")) &&
69+
(findFirst("div", withId("error-message")).isDisplayed());
70+
}
71+
5972

6073

6174

0 commit comments

Comments
 (0)