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