001/*
002 * (C) Copyright 2014 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 *     <a href="mailto:glefevre@nuxeo.com">Gildas</a>
018 */
019package org.nuxeo.functionaltests.forms;
020
021import org.nuxeo.functionaltests.Locator;
022import org.openqa.selenium.By;
023import org.openqa.selenium.JavascriptExecutor;
024import org.openqa.selenium.WebDriver;
025import org.openqa.selenium.WebElement;
026
027/**
028 * Represent a rich editor widget.
029 *
030 * @since 5.9.4
031 */
032public class RichEditorElement extends WidgetElement {
033
034    /**
035     * @param driver
036     * @param id
037     */
038    public RichEditorElement(WebDriver driver, String id) {
039        super(driver, id);
040    }
041
042    /**
043     * Insert content in the editor of the document.
044     *
045     * @param content The content to define in the document.
046     */
047    @Override
048    public void setInputValue(String content) {
049        Locator.waitUntilElementPresent(By.className("mce-tinymce"));
050        // Define the script which sets the content of the editor
051        String scriptToExecute = String.format("tinyMCE.editors['%s'].insertContent('%s')", id, content);
052        // Set the content of the editor
053        ((JavascriptExecutor) driver).executeScript(scriptToExecute);
054    }
055
056    /**
057     * Actions a click on the "Bold" button in the editor
058     */
059    public void clickBoldButton() {
060        // Get the bold button
061        WebElement button = driver.findElement(By.cssSelector(".mce-btn[aria-label='Bold'] button"));
062        button.click();
063    }
064
065    public void clickItalicButton() {
066        // Get the italic button
067        WebElement button = driver.findElement(By.cssSelector(".mce-btn[aria-label='Italic'] button"));
068        button.click();
069    }
070
071    /**
072     * @since 7.1
073     */
074    public String getRawContent() {
075        String scriptToExecute = String.format("return tinyMCE.editors['%s'].getBody().textContent", id);
076        String result = (String) ((JavascriptExecutor) driver).executeScript(scriptToExecute);
077        if (result == null) {
078            return "";
079        }
080        return result.replaceAll("[\uFEFF-\uFFFF]", "");
081    }
082
083    /**
084     * @since 7.1
085     */
086    public String getHtmlContent() {
087        String scriptToExecute = String.format("return tinyMCE.editors['%s'].getContent()", id);
088        String result = (String) ((JavascriptExecutor) driver).executeScript(scriptToExecute);
089        return result;
090    }
091
092    @Override
093    public String getInputValue() {
094        return getHtmlContent();
095    }
096
097}