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 *     Thomas Roger
018 *
019 */
020package org.nuxeo.functionaltests.pages.tabs;
021
022import java.util.List;
023
024import org.nuxeo.functionaltests.Locator;
025import org.nuxeo.functionaltests.Required;
026import org.nuxeo.functionaltests.pages.DocumentBasePage;
027import org.openqa.selenium.By;
028import org.openqa.selenium.NoSuchElementException;
029import org.openqa.selenium.WebDriver;
030import org.openqa.selenium.WebElement;
031import org.openqa.selenium.support.FindBy;
032
033/**
034 * Representation of a Comments tab page.
035 *
036 * @since 8.3
037 */
038public class CommentsTabSubPage extends DocumentBasePage {
039
040    @Required
041    @FindBy(linkText = "Add a Comment")
042    WebElement addComment;
043
044    @FindBy(xpath = "//textarea")
045    WebElement commentTextarea;
046
047    @FindBy(xpath = "//input[@value='Add']")
048    WebElement add;
049
050    @FindBy(linkText = "Reply")
051    WebElement replyLink;
052
053    @FindBy(linkText = "Delete")
054    WebElement deleteLink;
055
056    public CommentsTabSubPage(WebDriver driver) {
057        super(driver);
058    }
059
060    public CommentsTabSubPage addComment(String comment) {
061        return addComment(comment, false);
062    }
063
064    public CommentsTabSubPage addComment(String comment, boolean reply) {
065        if (reply) {
066            replyLink.click();
067        } else {
068            addComment.click();
069        }
070        commentTextarea.clear();
071        commentTextarea.sendKeys(comment);
072        Locator.scrollAndForceClick(add);
073        return asPage(CommentsTabSubPage.class);
074    }
075
076    public boolean hasComment(String comment) {
077        List<WebElement> elements = driver.findElements(By.xpath("//div[contains(@class, 'commentQuote')]"));
078        for (WebElement element : elements) {
079            if (element.getText().trim().equals(comment)) {
080                return true;
081            }
082        }
083        return false;
084    }
085
086    public CommentsTabSubPage reply(String reply) {
087        return addComment(reply, true);
088    }
089
090    /**
091     * Deletes first comment.
092     */
093    public CommentsTabSubPage delete() {
094        deleteLink.click();
095        return asPage(CommentsTabSubPage.class);
096    }
097
098    public boolean canDelete() {
099        try {
100            return deleteLink.isEnabled();
101        } catch (NoSuchElementException e) {
102            return false;
103        }
104    }
105
106}