Feedback changes - final and refactored code

This commit is contained in:
Colin But 2016-07-03 21:12:09 +01:00
parent 4eac37c6b5
commit b1e40d9c92
7 changed files with 73 additions and 63 deletions

View File

@ -26,6 +26,7 @@ package com.iluwatar.pageobject;
import com.gargoylesoftware.htmlunit.WebClient;
import com.iluwatar.pageobject.pages.AlbumListPage;
import com.iluwatar.pageobject.pages.AlbumPage;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
@ -33,12 +34,17 @@ import static org.junit.Assert.assertTrue;
public class AlbumListPageTest {
private AlbumListPage albumListPage = new AlbumListPage(new WebClient());
@Before
public void setUp() {
albumListPage.navigateToPage();
}
@Test
public void testSelectAlbum() {
AlbumListPage albumListPage = new AlbumListPage(new WebClient());
AlbumPage albumPage = albumListPage.selectAlbum("21");
albumPage.navigateToPage();
assertTrue(albumPage.isAt());
}

View File

@ -25,6 +25,7 @@ package com.iluwatar.pageobject;
import com.gargoylesoftware.htmlunit.WebClient;
import com.iluwatar.pageobject.pages.AlbumListPage;
import com.iluwatar.pageobject.pages.AlbumPage;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
@ -33,10 +34,16 @@ public class AlbumPageTest {
private AlbumPage albumPage = new AlbumPage(new WebClient());
@Before
public void setUp() {
albumPage.navigateToPage();
}
@Test
public void testSaveAlbum() {
AlbumPage albumPageAfterChanges = albumPage.changeAlbumTitle("25")
AlbumPage albumPageAfterChanges = albumPage
.changeAlbumTitle("25")
.changeArtist("Adele Laurie Blue Adkins")
.changeAlbumYear(2015)
.changeAlbumRating("B")
@ -50,6 +57,7 @@ public class AlbumPageTest {
@Test
public void testCancelChanges() {
AlbumListPage albumListPage = albumPage.cancelChanges();
albumListPage.navigateToPage();
assertTrue(albumListPage.isAt());
}

View File

@ -25,20 +25,27 @@ package com.iluwatar.pageobject;
import com.gargoylesoftware.htmlunit.WebClient;
import com.iluwatar.pageobject.pages.AlbumListPage;
import com.iluwatar.pageobject.pages.LoginPage;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class LoginPageTest {
private LoginPage loginPage = new LoginPage(new WebClient());
@Before
public void setUp() {
loginPage.navigateToPage();
}
@Test
public void testLogin() {
LoginPage loginPage = new LoginPage(new WebClient());
AlbumListPage albumListPage = loginPage.enterUsername("admin")
AlbumListPage albumListPage = loginPage
.enterUsername("admin")
.enterPassword("password")
.login();
albumListPage.navigateToPage();
assertTrue(albumListPage.isAt());
}

View File

@ -27,7 +27,6 @@ import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
/**
@ -40,26 +39,27 @@ public class AlbumListPage extends Page {
private HtmlPage page;
private List<HtmlAnchor> albumLinks;
/**
* Constructor
*/
public AlbumListPage(WebClient webClient) {
super(webClient);
}
/**
* Navigates to the Album List Page
*
* @return {@link AlbumListPage}
*/
public AlbumListPage navigateToPage() {
try {
page = this.webClient.getPage(PAGE_URL);
// uses XPath to find list of html anchor tags with the class album in it
albumLinks = (List<HtmlAnchor>) page.getByXPath("//tr[@class='album']//a");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
/**
@ -77,6 +77,8 @@ public class AlbumListPage extends Page {
* @return the album page
*/
public AlbumPage selectAlbum(String albumTitle) {
// uses XPath to find list of html anchor tags with the class album in it
List<HtmlAnchor> albumLinks = (List<HtmlAnchor>) page.getByXPath("//tr[@class='album']//a");
for (HtmlAnchor anchor : albumLinks) {
if (anchor.getTextContent().equals(albumTitle)) {
try {

View File

@ -31,7 +31,6 @@ import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.io.IOException;
import java.net.MalformedURLException;
/**
* Page Object encapsulating the Album Page (album-page.html)
@ -43,43 +42,27 @@ public class AlbumPage extends Page {
private HtmlPage page;
private HtmlTextInput albumTitleInputTextField;
private HtmlTextInput artistInputTextField;
private HtmlSelect albumYearSelectOption;
private HtmlTextInput albumRatingInputTextField;
private HtmlNumberInput numberOfSongsNumberField;
private HtmlSubmitInput cancelButton;
private HtmlSubmitInput saveButton;
/**
* Constructor
*/
public AlbumPage(WebClient webClient) {
super(webClient);
try {
page = this.webClient.getPage(PAGE_URL);
initializeHtmlElements();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private void initializeHtmlElements() {
albumTitleInputTextField = (HtmlTextInput) page.getElementById("albumTitle");
artistInputTextField = (HtmlTextInput) page.getElementById("albumArtist");
albumYearSelectOption = (HtmlSelect) page.getElementById("albumYear");
albumRatingInputTextField = (HtmlTextInput) page.getElementById("albumRating");
numberOfSongsNumberField = (HtmlNumberInput) page.getElementById("numberOfSongs");
cancelButton = (HtmlSubmitInput) page.getElementById("cancelButton");
saveButton = (HtmlSubmitInput) page.getElementById("saveButton");
/**
* Navigates to the album page
*
* @return {@link AlbumPage}
*/
public AlbumPage navigateToPage() {
try {
page = this.webClient.getPage(PAGE_URL);
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
@ -99,6 +82,7 @@ public class AlbumPage extends Page {
* @return {@link AlbumPage}
*/
public AlbumPage changeAlbumTitle(String albumTitle) {
HtmlTextInput albumTitleInputTextField = (HtmlTextInput) page.getElementById("albumTitle");
albumTitleInputTextField.setText(albumTitle);
return this;
}
@ -111,6 +95,7 @@ public class AlbumPage extends Page {
* @return {@link AlbumPage}
*/
public AlbumPage changeArtist(String artist) {
HtmlTextInput artistInputTextField = (HtmlTextInput) page.getElementById("albumArtist");
artistInputTextField.setText(artist);
return this;
}
@ -123,6 +108,7 @@ public class AlbumPage extends Page {
* @return {@link AlbumPage}
*/
public AlbumPage changeAlbumYear(int year) {
HtmlSelect albumYearSelectOption = (HtmlSelect) page.getElementById("albumYear");
HtmlOption yearOption = albumYearSelectOption.getOptionByValue(Integer.toString(year));
albumYearSelectOption.setSelectedAttribute(yearOption, true);
return this;
@ -136,6 +122,7 @@ public class AlbumPage extends Page {
* @return {@link AlbumPage}
*/
public AlbumPage changeAlbumRating(String albumRating) {
HtmlTextInput albumRatingInputTextField = (HtmlTextInput) page.getElementById("albumRating");
albumRatingInputTextField.setText(albumRating);
return this;
}
@ -147,6 +134,7 @@ public class AlbumPage extends Page {
* @return {@link AlbumPage}
*/
public AlbumPage changeNumberOfSongs(int numberOfSongs) {
HtmlNumberInput numberOfSongsNumberField = (HtmlNumberInput) page.getElementById("numberOfSongs");
numberOfSongsNumberField.setText(Integer.toString(numberOfSongs));
return this;
}
@ -158,6 +146,7 @@ public class AlbumPage extends Page {
* @return {@link AlbumListPage}
*/
public AlbumListPage cancelChanges() {
HtmlSubmitInput cancelButton = (HtmlSubmitInput) page.getElementById("cancelButton");
try {
cancelButton.click();
} catch (IOException e) {
@ -173,6 +162,7 @@ public class AlbumPage extends Page {
* @return {@link AlbumPage}
*/
public AlbumPage saveChanges() {
HtmlSubmitInput saveButton = (HtmlSubmitInput) page.getElementById("saveButton");
try {
saveButton.click();
} catch (IOException e) {

View File

@ -28,7 +28,6 @@ import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.io.IOException;
import java.net.MalformedURLException;
/**
* Page Object encapsulating the Login Page (login.html)
@ -40,11 +39,6 @@ public class LoginPage extends Page {
private HtmlPage page;
private HtmlTextInput usernameInputTextField;
private HtmlPasswordInput passwordInputPasswordField;
private HtmlSubmitInput loginButton;
/**
* Constructor
*
@ -52,21 +46,22 @@ public class LoginPage extends Page {
*/
public LoginPage(WebClient webClient) {
super(webClient);
}
/**
* Navigates to the Login page
*
* @return {@link LoginPage}
*/
public LoginPage navigateToPage() {
try {
page = this.webClient.getPage(PAGE_URL);
usernameInputTextField = (HtmlTextInput) page.getElementById("username");
passwordInputPasswordField = (HtmlPasswordInput) page.getElementById("password");
loginButton = (HtmlSubmitInput) page.getElementById("loginButton");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
/**
* {@inheritDoc}
*/
@ -83,6 +78,7 @@ public class LoginPage extends Page {
* @return {@link LoginPage}
*/
public LoginPage enterUsername(String username) {
HtmlTextInput usernameInputTextField = (HtmlTextInput) page.getElementById("username");
usernameInputTextField.setText(username);
return this;
}
@ -95,6 +91,7 @@ public class LoginPage extends Page {
* @return {@link LoginPage}
*/
public LoginPage enterPassword(String password) {
HtmlPasswordInput passwordInputPasswordField = (HtmlPasswordInput) page.getElementById("password");
passwordInputPasswordField.setText(password);
return this;
}
@ -107,6 +104,7 @@ public class LoginPage extends Page {
* - this is the page that user gets navigated to once successfully logged in
*/
public AlbumListPage login() {
HtmlSubmitInput loginButton = (HtmlSubmitInput) page.getElementById("loginButton");
try {
loginButton.click();
} catch (IOException e) {
@ -115,5 +113,4 @@ public class LoginPage extends Page {
return new AlbumListPage(webClient);
}
}

View File

@ -35,7 +35,7 @@ public abstract class Page {
*/
public static final String AUT_PATH = "src/main/resources/sample-ui/";
protected WebClient webClient;
protected final WebClient webClient;
/**
* Constructor