001/*
002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Gabriel Barata
018 *     Yannis JULIENNE
019 */
020
021package org.nuxeo.functionaltests.pages.tabs;
022
023import static org.junit.Assert.assertEquals;
024import static org.junit.Assert.assertTrue;
025
026import org.apache.commons.lang3.StringUtils;
027import org.nuxeo.functionaltests.Required;
028import org.nuxeo.functionaltests.pages.DocumentBasePage;
029import org.openqa.selenium.By;
030import org.openqa.selenium.NoSuchElementException;
031import org.openqa.selenium.WebDriver;
032import org.openqa.selenium.WebElement;
033import org.openqa.selenium.support.FindBy;
034
035/**
036 * @since 8.3
037 */
038public class TopicTabSubPage extends DocumentBasePage {
039
040    private static final String COMMENT_XPATH_BASE = ".//div[@class='commentAuthor' and starts-with(text(),'%s')]/..";
041
042    public static final String COMMENT_STATUS_PUBLISHED = "Published";
043
044    public static final String COMMENT_STATUS_WAITING_APPROVAL = "Waiting for approval";
045
046    public static final String COMMENT_STATUS_REJECTED = "Rejected";
047
048    @Required
049    @FindBy(xpath = "//ul[contains(@class,'commentsOutput')]")
050    public WebElement commentsList;
051
052    @FindBy(xpath = "//input[contains(@id,'post_title')]")
053    public WebElement titleInput;
054
055    @FindBy(xpath = "//textarea[contains(@id,'post_description')]")
056    public WebElement descriptionInput;
057
058    @FindBy(xpath = "//input[@type='submit' and @value='Add']")
059    public WebElement addButton;
060
061    @FindBy(xpath = "//input[@type='submit' and @value='Cancel']")
062    public WebElement cancelButton;
063
064    public TopicTabSubPage(WebDriver driver) {
065        super(driver);
066    }
067
068    public void checkComment(String title, String author, String description, String status, boolean canReply,
069            boolean canApproveOrReject, boolean canDelete) {
070        WebElement comment = getComment(title);
071        checkAuthor(comment, author);
072        checkDescription(comment, description);
073        checkStatus(comment, status);
074        assertEquals(canReply, hasReplyLink(comment));
075        assertEquals(canApproveOrReject, hasApproveLink(comment) && hasRejectLink(comment));
076        assertEquals(canDelete, hasDeleteLink(comment));
077    }
078
079    public boolean hasComment(String title) {
080        try {
081            return getComment(title) != null;
082        } catch (NoSuchElementException e) {
083            return false;
084        }
085    }
086
087    public boolean isCommentFormDisplayed() {
088        try {
089            return titleInput.isDisplayed() && descriptionInput.isDisplayed();
090        } catch (NoSuchElementException e) {
091            return false;
092        }
093    }
094
095    public TopicTabSubPage showCommentForm() {
096        if (!isCommentFormDisplayed()) {
097            addButton.click();
098            return asPage(TopicTabSubPage.class);
099        }
100        return this;
101    }
102
103    public TopicTabSubPage addComment(String title, String description) {
104        if (StringUtils.isNotBlank(title)) {
105            titleInput.sendKeys(title);
106        }
107        if (description != null) {
108            descriptionInput.sendKeys(description);
109        }
110        addButton.click();
111        return asPage(TopicTabSubPage.class);
112    }
113
114    public TopicTabSubPage reply(String title, String description) {
115        getReplyLink(getComment(title)).click();
116        TopicTabSubPage page = asPage(TopicTabSubPage.class);
117        return page.addComment(null, description);
118    }
119
120    public TopicTabSubPage delete(String title) {
121        getDeleteLink(getComment(title)).click();
122        return asPage(TopicTabSubPage.class);
123    }
124
125    public TopicTabSubPage reject(String title) {
126        getRejectLink(getComment(title)).click();
127        return asPage(TopicTabSubPage.class);
128    }
129
130    public TopicTabSubPage approve(String title) {
131        getApproveLink(getComment(title)).click();
132        return asPage(TopicTabSubPage.class);
133    }
134
135    public WebElement getComment(String title) {
136        String xpath = String.format(COMMENT_XPATH_BASE, title);
137        return commentsList.findElement(By.xpath(xpath));
138    }
139
140    private WebElement getReplyLink(WebElement comment) {
141        return comment.findElement(By.linkText("Reply"));
142    }
143
144    private WebElement getApproveLink(WebElement comment) {
145        return comment.findElement(By.linkText("Approve"));
146    }
147
148    private WebElement getRejectLink(WebElement comment) {
149        return comment.findElement(By.linkText("Reject"));
150    }
151
152    private WebElement getDeleteLink(WebElement comment) {
153        return comment.findElement(By.linkText("Delete"));
154    }
155
156    private boolean hasReplyLink(WebElement comment) {
157        try {
158            return getReplyLink(comment) != null;
159        } catch (NoSuchElementException e) {
160            return false;
161        }
162    }
163
164    private boolean hasApproveLink(WebElement comment) {
165        try {
166            return getApproveLink(comment) != null;
167        } catch (NoSuchElementException e) {
168            return false;
169        }
170    }
171
172    private boolean hasRejectLink(WebElement comment) {
173        try {
174            return getRejectLink(comment) != null;
175        } catch (NoSuchElementException e) {
176            return false;
177        }
178    }
179
180    private boolean hasDeleteLink(WebElement comment) {
181        try {
182            return getDeleteLink(comment) != null;
183        } catch (NoSuchElementException e) {
184            return false;
185        }
186    }
187
188    private void checkAuthor(WebElement comment, String author) {
189        assertTrue(comment.findElement(By.className("commentAuthor")).getText().contains("by " + author));
190    }
191
192    private void checkDescription(WebElement comment, String description) {
193        assertTrue(comment.findElement(By.className("commentQuote")).getText().equals(description));
194    }
195
196    private void checkStatus(WebElement comment, String status) {
197        assertTrue(comment.findElement(By.className("commentCreationDate")).getText().contains(status));
198    }
199
200}