Feedback changes - final and refactored code
This commit is contained in:
parent
4eac37c6b5
commit
b1e40d9c92
@ -26,6 +26,7 @@ package com.iluwatar.pageobject;
|
|||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
import com.iluwatar.pageobject.pages.AlbumListPage;
|
import com.iluwatar.pageobject.pages.AlbumListPage;
|
||||||
import com.iluwatar.pageobject.pages.AlbumPage;
|
import com.iluwatar.pageobject.pages.AlbumPage;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
@ -33,12 +34,17 @@ import static org.junit.Assert.assertTrue;
|
|||||||
|
|
||||||
public class AlbumListPageTest {
|
public class AlbumListPageTest {
|
||||||
|
|
||||||
|
private AlbumListPage albumListPage = new AlbumListPage(new WebClient());
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
albumListPage.navigateToPage();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSelectAlbum() {
|
public void testSelectAlbum() {
|
||||||
AlbumListPage albumListPage = new AlbumListPage(new WebClient());
|
|
||||||
|
|
||||||
AlbumPage albumPage = albumListPage.selectAlbum("21");
|
AlbumPage albumPage = albumListPage.selectAlbum("21");
|
||||||
|
albumPage.navigateToPage();
|
||||||
assertTrue(albumPage.isAt());
|
assertTrue(albumPage.isAt());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ package com.iluwatar.pageobject;
|
|||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
import com.iluwatar.pageobject.pages.AlbumListPage;
|
import com.iluwatar.pageobject.pages.AlbumListPage;
|
||||||
import com.iluwatar.pageobject.pages.AlbumPage;
|
import com.iluwatar.pageobject.pages.AlbumPage;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
@ -33,10 +34,16 @@ public class AlbumPageTest {
|
|||||||
|
|
||||||
private AlbumPage albumPage = new AlbumPage(new WebClient());
|
private AlbumPage albumPage = new AlbumPage(new WebClient());
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
albumPage.navigateToPage();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSaveAlbum() {
|
public void testSaveAlbum() {
|
||||||
|
|
||||||
AlbumPage albumPageAfterChanges = albumPage.changeAlbumTitle("25")
|
AlbumPage albumPageAfterChanges = albumPage
|
||||||
|
.changeAlbumTitle("25")
|
||||||
.changeArtist("Adele Laurie Blue Adkins")
|
.changeArtist("Adele Laurie Blue Adkins")
|
||||||
.changeAlbumYear(2015)
|
.changeAlbumYear(2015)
|
||||||
.changeAlbumRating("B")
|
.changeAlbumRating("B")
|
||||||
@ -50,6 +57,7 @@ public class AlbumPageTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testCancelChanges() {
|
public void testCancelChanges() {
|
||||||
AlbumListPage albumListPage = albumPage.cancelChanges();
|
AlbumListPage albumListPage = albumPage.cancelChanges();
|
||||||
|
albumListPage.navigateToPage();
|
||||||
assertTrue(albumListPage.isAt());
|
assertTrue(albumListPage.isAt());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,20 +25,27 @@ package com.iluwatar.pageobject;
|
|||||||
import com.gargoylesoftware.htmlunit.WebClient;
|
import com.gargoylesoftware.htmlunit.WebClient;
|
||||||
import com.iluwatar.pageobject.pages.AlbumListPage;
|
import com.iluwatar.pageobject.pages.AlbumListPage;
|
||||||
import com.iluwatar.pageobject.pages.LoginPage;
|
import com.iluwatar.pageobject.pages.LoginPage;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class LoginPageTest {
|
public class LoginPageTest {
|
||||||
|
|
||||||
|
private LoginPage loginPage = new LoginPage(new WebClient());
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
loginPage.navigateToPage();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLogin() {
|
public void testLogin() {
|
||||||
LoginPage loginPage = new LoginPage(new WebClient());
|
AlbumListPage albumListPage = loginPage
|
||||||
|
.enterUsername("admin")
|
||||||
AlbumListPage albumListPage = loginPage.enterUsername("admin")
|
|
||||||
.enterPassword("password")
|
.enterPassword("password")
|
||||||
.login();
|
.login();
|
||||||
|
albumListPage.navigateToPage();
|
||||||
assertTrue(albumListPage.isAt());
|
assertTrue(albumListPage.isAt());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,6 @@ import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
|||||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,26 +39,27 @@ public class AlbumListPage extends Page {
|
|||||||
|
|
||||||
private HtmlPage page;
|
private HtmlPage page;
|
||||||
|
|
||||||
private List<HtmlAnchor> albumLinks;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public AlbumListPage(WebClient webClient) {
|
public AlbumListPage(WebClient webClient) {
|
||||||
super(webClient);
|
super(webClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Navigates to the Album List Page
|
||||||
|
*
|
||||||
|
* @return {@link AlbumListPage}
|
||||||
|
*/
|
||||||
|
public AlbumListPage navigateToPage() {
|
||||||
try {
|
try {
|
||||||
page = this.webClient.getPage(PAGE_URL);
|
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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -77,6 +77,8 @@ public class AlbumListPage extends Page {
|
|||||||
* @return the album page
|
* @return the album page
|
||||||
*/
|
*/
|
||||||
public AlbumPage selectAlbum(String albumTitle) {
|
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) {
|
for (HtmlAnchor anchor : albumLinks) {
|
||||||
if (anchor.getTextContent().equals(albumTitle)) {
|
if (anchor.getTextContent().equals(albumTitle)) {
|
||||||
try {
|
try {
|
||||||
|
@ -31,7 +31,6 @@ import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
|||||||
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Page Object encapsulating the Album Page (album-page.html)
|
* Page Object encapsulating the Album Page (album-page.html)
|
||||||
@ -43,43 +42,27 @@ public class AlbumPage extends Page {
|
|||||||
|
|
||||||
private HtmlPage 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
|
* Constructor
|
||||||
*/
|
*/
|
||||||
public AlbumPage(WebClient webClient) {
|
public AlbumPage(WebClient webClient) {
|
||||||
super(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");
|
* Navigates to the album page
|
||||||
artistInputTextField = (HtmlTextInput) page.getElementById("albumArtist");
|
*
|
||||||
albumYearSelectOption = (HtmlSelect) page.getElementById("albumYear");
|
* @return {@link AlbumPage}
|
||||||
albumRatingInputTextField = (HtmlTextInput) page.getElementById("albumRating");
|
*/
|
||||||
numberOfSongsNumberField = (HtmlNumberInput) page.getElementById("numberOfSongs");
|
public AlbumPage navigateToPage() {
|
||||||
|
try {
|
||||||
cancelButton = (HtmlSubmitInput) page.getElementById("cancelButton");
|
page = this.webClient.getPage(PAGE_URL);
|
||||||
saveButton = (HtmlSubmitInput) page.getElementById("saveButton");
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -99,6 +82,7 @@ public class AlbumPage extends Page {
|
|||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
*/
|
*/
|
||||||
public AlbumPage changeAlbumTitle(String albumTitle) {
|
public AlbumPage changeAlbumTitle(String albumTitle) {
|
||||||
|
HtmlTextInput albumTitleInputTextField = (HtmlTextInput) page.getElementById("albumTitle");
|
||||||
albumTitleInputTextField.setText(albumTitle);
|
albumTitleInputTextField.setText(albumTitle);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -111,6 +95,7 @@ public class AlbumPage extends Page {
|
|||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
*/
|
*/
|
||||||
public AlbumPage changeArtist(String artist) {
|
public AlbumPage changeArtist(String artist) {
|
||||||
|
HtmlTextInput artistInputTextField = (HtmlTextInput) page.getElementById("albumArtist");
|
||||||
artistInputTextField.setText(artist);
|
artistInputTextField.setText(artist);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -123,6 +108,7 @@ public class AlbumPage extends Page {
|
|||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
*/
|
*/
|
||||||
public AlbumPage changeAlbumYear(int year) {
|
public AlbumPage changeAlbumYear(int year) {
|
||||||
|
HtmlSelect albumYearSelectOption = (HtmlSelect) page.getElementById("albumYear");
|
||||||
HtmlOption yearOption = albumYearSelectOption.getOptionByValue(Integer.toString(year));
|
HtmlOption yearOption = albumYearSelectOption.getOptionByValue(Integer.toString(year));
|
||||||
albumYearSelectOption.setSelectedAttribute(yearOption, true);
|
albumYearSelectOption.setSelectedAttribute(yearOption, true);
|
||||||
return this;
|
return this;
|
||||||
@ -136,6 +122,7 @@ public class AlbumPage extends Page {
|
|||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
*/
|
*/
|
||||||
public AlbumPage changeAlbumRating(String albumRating) {
|
public AlbumPage changeAlbumRating(String albumRating) {
|
||||||
|
HtmlTextInput albumRatingInputTextField = (HtmlTextInput) page.getElementById("albumRating");
|
||||||
albumRatingInputTextField.setText(albumRating);
|
albumRatingInputTextField.setText(albumRating);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -147,6 +134,7 @@ public class AlbumPage extends Page {
|
|||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
*/
|
*/
|
||||||
public AlbumPage changeNumberOfSongs(int numberOfSongs) {
|
public AlbumPage changeNumberOfSongs(int numberOfSongs) {
|
||||||
|
HtmlNumberInput numberOfSongsNumberField = (HtmlNumberInput) page.getElementById("numberOfSongs");
|
||||||
numberOfSongsNumberField.setText(Integer.toString(numberOfSongs));
|
numberOfSongsNumberField.setText(Integer.toString(numberOfSongs));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -158,6 +146,7 @@ public class AlbumPage extends Page {
|
|||||||
* @return {@link AlbumListPage}
|
* @return {@link AlbumListPage}
|
||||||
*/
|
*/
|
||||||
public AlbumListPage cancelChanges() {
|
public AlbumListPage cancelChanges() {
|
||||||
|
HtmlSubmitInput cancelButton = (HtmlSubmitInput) page.getElementById("cancelButton");
|
||||||
try {
|
try {
|
||||||
cancelButton.click();
|
cancelButton.click();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -173,6 +162,7 @@ public class AlbumPage extends Page {
|
|||||||
* @return {@link AlbumPage}
|
* @return {@link AlbumPage}
|
||||||
*/
|
*/
|
||||||
public AlbumPage saveChanges() {
|
public AlbumPage saveChanges() {
|
||||||
|
HtmlSubmitInput saveButton = (HtmlSubmitInput) page.getElementById("saveButton");
|
||||||
try {
|
try {
|
||||||
saveButton.click();
|
saveButton.click();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -28,7 +28,6 @@ import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
|
|||||||
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
||||||
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Page Object encapsulating the Login Page (login.html)
|
* Page Object encapsulating the Login Page (login.html)
|
||||||
@ -40,11 +39,6 @@ public class LoginPage extends Page {
|
|||||||
|
|
||||||
private HtmlPage page;
|
private HtmlPage page;
|
||||||
|
|
||||||
private HtmlTextInput usernameInputTextField;
|
|
||||||
private HtmlPasswordInput passwordInputPasswordField;
|
|
||||||
private HtmlSubmitInput loginButton;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
@ -52,21 +46,22 @@ public class LoginPage extends Page {
|
|||||||
*/
|
*/
|
||||||
public LoginPage(WebClient webClient) {
|
public LoginPage(WebClient webClient) {
|
||||||
super(webClient);
|
super(webClient);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Navigates to the Login page
|
||||||
|
*
|
||||||
|
* @return {@link LoginPage}
|
||||||
|
*/
|
||||||
|
public LoginPage navigateToPage() {
|
||||||
try {
|
try {
|
||||||
page = this.webClient.getPage(PAGE_URL);
|
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) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@ -83,6 +78,7 @@ public class LoginPage extends Page {
|
|||||||
* @return {@link LoginPage}
|
* @return {@link LoginPage}
|
||||||
*/
|
*/
|
||||||
public LoginPage enterUsername(String username) {
|
public LoginPage enterUsername(String username) {
|
||||||
|
HtmlTextInput usernameInputTextField = (HtmlTextInput) page.getElementById("username");
|
||||||
usernameInputTextField.setText(username);
|
usernameInputTextField.setText(username);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -95,6 +91,7 @@ public class LoginPage extends Page {
|
|||||||
* @return {@link LoginPage}
|
* @return {@link LoginPage}
|
||||||
*/
|
*/
|
||||||
public LoginPage enterPassword(String password) {
|
public LoginPage enterPassword(String password) {
|
||||||
|
HtmlPasswordInput passwordInputPasswordField = (HtmlPasswordInput) page.getElementById("password");
|
||||||
passwordInputPasswordField.setText(password);
|
passwordInputPasswordField.setText(password);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -107,6 +104,7 @@ public class LoginPage extends Page {
|
|||||||
* - this is the page that user gets navigated to once successfully logged in
|
* - this is the page that user gets navigated to once successfully logged in
|
||||||
*/
|
*/
|
||||||
public AlbumListPage login() {
|
public AlbumListPage login() {
|
||||||
|
HtmlSubmitInput loginButton = (HtmlSubmitInput) page.getElementById("loginButton");
|
||||||
try {
|
try {
|
||||||
loginButton.click();
|
loginButton.click();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -115,5 +113,4 @@ public class LoginPage extends Page {
|
|||||||
return new AlbumListPage(webClient);
|
return new AlbumListPage(webClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ public abstract class Page {
|
|||||||
*/
|
*/
|
||||||
public static final String AUT_PATH = "src/main/resources/sample-ui/";
|
public static final String AUT_PATH = "src/main/resources/sample-ui/";
|
||||||
|
|
||||||
protected WebClient webClient;
|
protected final WebClient webClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
Loading…
x
Reference in New Issue
Block a user