review adjustments
@@ -6,6 +6,8 @@ import java.util.List;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
+import edu.hm.hafner.util.NoSuchElementException;
+
/**
* Page Object for the Overview Carousel.
*
@@ -13,48 +15,84 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage;
*/
public class OverviewCarousel {
private final DomElement overview;
- private final DomElement prev;
+ private final DomElement previous;
private final DomElement next;
private List<DomElement> items;
+
private static final String CAROUSEL_ITEMS_XPATH = ".//div[contains(@class, 'carousel-item')]";
public OverviewCarousel(final HtmlPage page) {
this.overview = page.getElementById("overview-carousel");
- this.prev = page.getElementById("overview-carousel-prev");
+ this.previous = page.getElementById("overview-carousel-prev");
this.next = page.getElementById("overview-carousel-next");
- this.items = overview.getByXPath(CAROUSEL_ITEMS_XPATH);
+ this.items = retrieveCarouselItems();
}
/**
- * TODO: replace with Carousel Element once it's done.
+ * Helper-method to retrieve overview carousel items.
*
+ * @return List of overview carousel items.
*/
- public DomElement getActiveItem(){
- for (DomElement item: items){
- if (item.getAttribute("class").contains("active"))
- return item;
+ private List<DomElement> retrieveCarouselItems() {
+ if (overview == null) {
+ throw new AssertionError("No overview carousel found");
}
- return null;
+ List<DomElement> itemList = overview.getByXPath(CAROUSEL_ITEMS_XPATH);
+ if (itemList.size() == 0) {
+ throw new AssertionError("No overview carousel items found");
+ }
+ return itemList;
}
- public void prev() {
+ /**
+ * Helper-method for clicking on a button.
+ *
+ * @param element
+ * a {@link DomElement} which will trigger the carousel element switch .
+ */
+ private void clickOnButton(final DomElement element) {
try {
- prev.click();
- this.items = overview.getByXPath(CAROUSEL_ITEMS_XPATH);
+ element.click();
}
catch (IOException e) {
- e.printStackTrace();
+ throw new AssertionError(e);
}
}
- public void next() {
- try {
- next.click();
- this.items = overview.getByXPath(CAROUSEL_ITEMS_XPATH);
+ /**
+ * Clicks the button to switch to the next element of overview carousel and sets new overview items.
+ *
+ */
+ public void clickNext() {
+ if (next == null) {
+ throw new AssertionError("No next element link found");
+ }
+ clickOnButton(next);
+ this.items = retrieveCarouselItems();
+ }
+
+ /**
+ * Clicks the button to switch to the previous element of overview carousel and sets new overview items.
+ *
+ */
+ public void clickPrevious() {
+ if (previous == null) {
+ throw new AssertionError("No previous element link found");
+ }
+ clickOnButton(previous);
+ this.items = retrieveCarouselItems();
+ }
+
+ /**
+ * TODO: replace with Carousel Element once it's done.
+ */
+ public DomElement getActiveItem() {
+ for (DomElement item : items) {
+ if (item.getAttribute("class").contains("active")) {
+ return item;
}
- catch (IOException e) {
- e.printStackTrace();
}
+ throw new NoSuchElementException("No active element in overview carousel found");
}
public List<DomElement> getItems() {
