001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:glefevre@nuxeo.com">Gildas</a>
016 */
017package org.nuxeo.functionaltests.forms;
018
019import org.openqa.selenium.By;
020import org.openqa.selenium.JavascriptExecutor;
021import org.openqa.selenium.WebDriver;
022import org.openqa.selenium.WebElement;
023
024/**
025 * Represent a rich editor widget.
026 *
027 * @since 5.9.4
028 */
029public class RichEditorElement extends WidgetElement {
030
031    /**
032     * @param driver
033     * @param id
034     */
035    public RichEditorElement(WebDriver driver, String id) {
036        super(driver, id);
037    }
038
039    /**
040     * Insert content in the editor of the document.
041     *
042     * @param content The content to define in the document.
043     */
044    @Override
045    public void setInputValue(String content) {
046        // Define the script which sets the content of the editor
047        String scriptToExecute = String.format("tinyMCE.editors['%s'].insertContent('%s')", id, content);
048        // Set the content of the editor
049        ((JavascriptExecutor) driver).executeScript(scriptToExecute);
050    }
051
052    /**
053     * Actions a click on the "Bold" button in the editor
054     */
055    public void clickBoldButton() {
056        // Get the bold button
057        WebElement button = driver.findElement(By.cssSelector(".mce-btn[aria-label='Bold'] button"));
058        button.click();
059    }
060
061    public void clickItalicButton() {
062        // Get the italic button
063        WebElement button = driver.findElement(By.cssSelector(".mce-btn[aria-label='Italic'] button"));
064        button.click();
065    }
066
067    /**
068     * @since 7.1
069     */
070    public String getRawContent() {
071        String scriptToExecute = String.format("return tinyMCE.editors['%s'].getBody().textContent", id);
072        String result = (String) ((JavascriptExecutor) driver).executeScript(scriptToExecute);
073        if (result == null) {
074            return "";
075        }
076        return result.replaceAll("[\uFEFF-\uFFFF]", "");
077    }
078
079    /**
080     * @since 7.1
081     */
082    public String getHtmlContent() {
083        String scriptToExecute = String.format("return tinyMCE.editors['%s'].getContent()", id);
084        String result = (String) ((JavascriptExecutor) driver).executeScript(scriptToExecute);
085        return result;
086    }
087
088    @Override
089    public String getInputValue() {
090        return getHtmlContent();
091    }
092
093}