Skip to content

Commit b1e40d9

Browse files
committed
Feedback changes - final and refactored code
1 parent 4eac37c commit b1e40d9

File tree

7 files changed

+71
-61
lines changed

7 files changed

+71
-61
lines changed

page-object/src/test/java/com/iluwatar/pageobject/AlbumListPageTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,25 @@
2626
import com.gargoylesoftware.htmlunit.WebClient;
2727
import com.iluwatar.pageobject.pages.AlbumListPage;
2828
import com.iluwatar.pageobject.pages.AlbumPage;
29+
import org.junit.Before;
2930
import org.junit.Test;
3031

3132
import static org.junit.Assert.assertTrue;
3233

3334

3435
public class AlbumListPageTest {
3536

37+
private AlbumListPage albumListPage = new AlbumListPage(new WebClient());
38+
39+
@Before
40+
public void setUp() {
41+
albumListPage.navigateToPage();
42+
}
43+
3644
@Test
3745
public void testSelectAlbum() {
38-
AlbumListPage albumListPage = new AlbumListPage(new WebClient());
39-
4046
AlbumPage albumPage = albumListPage.selectAlbum("21");
41-
47+
albumPage.navigateToPage();
4248
assertTrue(albumPage.isAt());
4349
}
4450

page-object/src/test/java/com/iluwatar/pageobject/AlbumPageTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.gargoylesoftware.htmlunit.WebClient;
2626
import com.iluwatar.pageobject.pages.AlbumListPage;
2727
import com.iluwatar.pageobject.pages.AlbumPage;
28+
import org.junit.Before;
2829
import org.junit.Test;
2930

3031
import static org.junit.Assert.assertTrue;
@@ -33,10 +34,16 @@ public class AlbumPageTest {
3334

3435
private AlbumPage albumPage = new AlbumPage(new WebClient());
3536

37+
@Before
38+
public void setUp() {
39+
albumPage.navigateToPage();
40+
}
41+
3642
@Test
3743
public void testSaveAlbum() {
3844

39-
AlbumPage albumPageAfterChanges = albumPage.changeAlbumTitle("25")
45+
AlbumPage albumPageAfterChanges = albumPage
46+
.changeAlbumTitle("25")
4047
.changeArtist("Adele Laurie Blue Adkins")
4148
.changeAlbumYear(2015)
4249
.changeAlbumRating("B")
@@ -50,6 +57,7 @@ public void testSaveAlbum() {
5057
@Test
5158
public void testCancelChanges() {
5259
AlbumListPage albumListPage = albumPage.cancelChanges();
60+
albumListPage.navigateToPage();
5361
assertTrue(albumListPage.isAt());
5462
}
5563

page-object/src/test/java/com/iluwatar/pageobject/LoginPageTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,27 @@
2525
import com.gargoylesoftware.htmlunit.WebClient;
2626
import com.iluwatar.pageobject.pages.AlbumListPage;
2727
import com.iluwatar.pageobject.pages.LoginPage;
28+
import org.junit.Before;
2829
import org.junit.Test;
2930

3031
import static org.junit.Assert.assertTrue;
3132

3233
public class LoginPageTest {
3334

35+
private LoginPage loginPage = new LoginPage(new WebClient());
36+
37+
@Before
38+
public void setUp() {
39+
loginPage.navigateToPage();
40+
}
41+
3442
@Test
3543
public void testLogin() {
36-
LoginPage loginPage = new LoginPage(new WebClient());
37-
38-
AlbumListPage albumListPage = loginPage.enterUsername("admin")
44+
AlbumListPage albumListPage = loginPage
45+
.enterUsername("admin")
3946
.enterPassword("password")
4047
.login();
41-
48+
albumListPage.navigateToPage();
4249
assertTrue(albumListPage.isAt());
4350
}
4451

page-object/src/test/java/com/iluwatar/pageobject/pages/AlbumListPage.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.gargoylesoftware.htmlunit.html.HtmlPage;
2828

2929
import java.io.IOException;
30-
import java.net.MalformedURLException;
3130
import java.util.List;
3231

3332
/**
@@ -40,26 +39,27 @@ public class AlbumListPage extends Page {
4039

4140
private HtmlPage page;
4241

43-
private List<HtmlAnchor> albumLinks;
44-
4542

4643
/**
4744
* Constructor
4845
*/
4946
public AlbumListPage(WebClient webClient) {
5047
super(webClient);
51-
try {
52-
page = this.webClient.getPage(PAGE_URL);
48+
}
5349

54-
// uses XPath to find list of html anchor tags with the class album in it
55-
albumLinks = (List<HtmlAnchor>) page.getByXPath("//tr[@class='album']//a");
5650

57-
} catch (MalformedURLException e) {
58-
e.printStackTrace();
51+
/**
52+
* Navigates to the Album List Page
53+
*
54+
* @return {@link AlbumListPage}
55+
*/
56+
public AlbumListPage navigateToPage() {
57+
try {
58+
page = this.webClient.getPage(PAGE_URL);
5959
} catch (IOException e) {
6060
e.printStackTrace();
6161
}
62-
62+
return this;
6363
}
6464

6565
/**
@@ -77,6 +77,8 @@ public boolean isAt() {
7777
* @return the album page
7878
*/
7979
public AlbumPage selectAlbum(String albumTitle) {
80+
// uses XPath to find list of html anchor tags with the class album in it
81+
List<HtmlAnchor> albumLinks = (List<HtmlAnchor>) page.getByXPath("//tr[@class='album']//a");
8082
for (HtmlAnchor anchor : albumLinks) {
8183
if (anchor.getTextContent().equals(albumTitle)) {
8284
try {

page-object/src/test/java/com/iluwatar/pageobject/pages/AlbumPage.java

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
3232

3333
import java.io.IOException;
34-
import java.net.MalformedURLException;
3534

3635
/**
3736
* Page Object encapsulating the Album Page (album-page.html)
@@ -43,43 +42,27 @@ public class AlbumPage extends Page {
4342

4443
private HtmlPage page;
4544

46-
private HtmlTextInput albumTitleInputTextField;
47-
private HtmlTextInput artistInputTextField;
48-
private HtmlSelect albumYearSelectOption;
49-
private HtmlTextInput albumRatingInputTextField;
50-
private HtmlNumberInput numberOfSongsNumberField;
51-
52-
private HtmlSubmitInput cancelButton;
53-
private HtmlSubmitInput saveButton;
5445

5546
/**
5647
* Constructor
5748
*/
5849
public AlbumPage(WebClient webClient) {
5950
super(webClient);
51+
}
52+
53+
54+
/**
55+
* Navigates to the album page
56+
*
57+
* @return {@link AlbumPage}
58+
*/
59+
public AlbumPage navigateToPage() {
6060
try {
6161
page = this.webClient.getPage(PAGE_URL);
62-
initializeHtmlElements();
63-
} catch (MalformedURLException e) {
64-
e.printStackTrace();
6562
} catch (IOException e) {
6663
e.printStackTrace();
67-
} catch (Exception e) {
68-
e.printStackTrace();
6964
}
70-
71-
}
72-
73-
74-
private void initializeHtmlElements() {
75-
albumTitleInputTextField = (HtmlTextInput) page.getElementById("albumTitle");
76-
artistInputTextField = (HtmlTextInput) page.getElementById("albumArtist");
77-
albumYearSelectOption = (HtmlSelect) page.getElementById("albumYear");
78-
albumRatingInputTextField = (HtmlTextInput) page.getElementById("albumRating");
79-
numberOfSongsNumberField = (HtmlNumberInput) page.getElementById("numberOfSongs");
80-
81-
cancelButton = (HtmlSubmitInput) page.getElementById("cancelButton");
82-
saveButton = (HtmlSubmitInput) page.getElementById("saveButton");
65+
return this;
8366
}
8467

8568

@@ -99,6 +82,7 @@ public boolean isAt() {
9982
* @return {@link AlbumPage}
10083
*/
10184
public AlbumPage changeAlbumTitle(String albumTitle) {
85+
HtmlTextInput albumTitleInputTextField = (HtmlTextInput) page.getElementById("albumTitle");
10286
albumTitleInputTextField.setText(albumTitle);
10387
return this;
10488
}
@@ -111,6 +95,7 @@ public AlbumPage changeAlbumTitle(String albumTitle) {
11195
* @return {@link AlbumPage}
11296
*/
11397
public AlbumPage changeArtist(String artist) {
98+
HtmlTextInput artistInputTextField = (HtmlTextInput) page.getElementById("albumArtist");
11499
artistInputTextField.setText(artist);
115100
return this;
116101
}
@@ -123,6 +108,7 @@ public AlbumPage changeArtist(String artist) {
123108
* @return {@link AlbumPage}
124109
*/
125110
public AlbumPage changeAlbumYear(int year) {
111+
HtmlSelect albumYearSelectOption = (HtmlSelect) page.getElementById("albumYear");
126112
HtmlOption yearOption = albumYearSelectOption.getOptionByValue(Integer.toString(year));
127113
albumYearSelectOption.setSelectedAttribute(yearOption, true);
128114
return this;
@@ -136,6 +122,7 @@ public AlbumPage changeAlbumYear(int year) {
136122
* @return {@link AlbumPage}
137123
*/
138124
public AlbumPage changeAlbumRating(String albumRating) {
125+
HtmlTextInput albumRatingInputTextField = (HtmlTextInput) page.getElementById("albumRating");
139126
albumRatingInputTextField.setText(albumRating);
140127
return this;
141128
}
@@ -147,6 +134,7 @@ public AlbumPage changeAlbumRating(String albumRating) {
147134
* @return {@link AlbumPage}
148135
*/
149136
public AlbumPage changeNumberOfSongs(int numberOfSongs) {
137+
HtmlNumberInput numberOfSongsNumberField = (HtmlNumberInput) page.getElementById("numberOfSongs");
150138
numberOfSongsNumberField.setText(Integer.toString(numberOfSongs));
151139
return this;
152140
}
@@ -158,6 +146,7 @@ public AlbumPage changeNumberOfSongs(int numberOfSongs) {
158146
* @return {@link AlbumListPage}
159147
*/
160148
public AlbumListPage cancelChanges() {
149+
HtmlSubmitInput cancelButton = (HtmlSubmitInput) page.getElementById("cancelButton");
161150
try {
162151
cancelButton.click();
163152
} catch (IOException e) {
@@ -173,6 +162,7 @@ public AlbumListPage cancelChanges() {
173162
* @return {@link AlbumPage}
174163
*/
175164
public AlbumPage saveChanges() {
165+
HtmlSubmitInput saveButton = (HtmlSubmitInput) page.getElementById("saveButton");
176166
try {
177167
saveButton.click();
178168
} catch (IOException e) {

page-object/src/test/java/com/iluwatar/pageobject/pages/LoginPage.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
2929
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
3030
import java.io.IOException;
31-
import java.net.MalformedURLException;
3231

3332
/**
3433
* Page Object encapsulating the Login Page (login.html)
@@ -40,33 +39,29 @@ public class LoginPage extends Page {
4039

4140
private HtmlPage page;
4241

43-
private HtmlTextInput usernameInputTextField;
44-
private HtmlPasswordInput passwordInputPasswordField;
45-
private HtmlSubmitInput loginButton;
46-
47-
4842
/**
4943
* Constructor
5044
*
5145
* @param webClient {@link WebClient}
5246
*/
5347
public LoginPage(WebClient webClient) {
5448
super(webClient);
49+
}
50+
51+
/**
52+
* Navigates to the Login page
53+
*
54+
* @return {@link LoginPage}
55+
*/
56+
public LoginPage navigateToPage() {
5557
try {
5658
page = this.webClient.getPage(PAGE_URL);
57-
58-
usernameInputTextField = (HtmlTextInput) page.getElementById("username");
59-
passwordInputPasswordField = (HtmlPasswordInput) page.getElementById("password");
60-
loginButton = (HtmlSubmitInput) page.getElementById("loginButton");
61-
62-
} catch (MalformedURLException e) {
63-
e.printStackTrace();
6459
} catch (IOException e) {
6560
e.printStackTrace();
6661
}
62+
return this;
6763
}
6864

69-
7065
/**
7166
* {@inheritDoc}
7267
*/
@@ -83,6 +78,7 @@ public boolean isAt() {
8378
* @return {@link LoginPage}
8479
*/
8580
public LoginPage enterUsername(String username) {
81+
HtmlTextInput usernameInputTextField = (HtmlTextInput) page.getElementById("username");
8682
usernameInputTextField.setText(username);
8783
return this;
8884
}
@@ -95,6 +91,7 @@ public LoginPage enterUsername(String username) {
9591
* @return {@link LoginPage}
9692
*/
9793
public LoginPage enterPassword(String password) {
94+
HtmlPasswordInput passwordInputPasswordField = (HtmlPasswordInput) page.getElementById("password");
9895
passwordInputPasswordField.setText(password);
9996
return this;
10097
}
@@ -107,6 +104,7 @@ public LoginPage enterPassword(String password) {
107104
* - this is the page that user gets navigated to once successfully logged in
108105
*/
109106
public AlbumListPage login() {
107+
HtmlSubmitInput loginButton = (HtmlSubmitInput) page.getElementById("loginButton");
110108
try {
111109
loginButton.click();
112110
} catch (IOException e) {
@@ -115,5 +113,4 @@ public AlbumListPage login() {
115113
return new AlbumListPage(webClient);
116114
}
117115

118-
119116
}

page-object/src/test/java/com/iluwatar/pageobject/pages/Page.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public abstract class Page {
3535
*/
3636
public static final String AUT_PATH = "src/main/resources/sample-ui/";
3737

38-
protected WebClient webClient;
38+
protected final WebClient webClient;
3939

4040
/**
4141
* Constructor

0 commit comments

Comments
 (0)