1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.ui.po.editor.wysiwyg

File RichTextAreaElement.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

2
28
9
1
161
75
10
0.36
3.11
9
1.11

Classes

Class Line # Actions
RichTextAreaElement 33 28 0% 10 8
0.794871879.5%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.test.ui.po.editor.wysiwyg;
21   
22    import org.openqa.selenium.JavascriptExecutor;
23    import org.openqa.selenium.WebDriver;
24    import org.openqa.selenium.WebElement;
25    import org.xwiki.test.ui.po.BaseElement;
26   
27    /**
28    * Models the rich text area of the WYSIWYG content editor.
29    *
30    * @version $Id: 2321ac7d0af61deb87554319b2c4d069bcf6b1fe $
31    * @since 3.2M3
32    */
 
33    public class RichTextAreaElement extends BaseElement
34    {
35    /**
36    * The in-line frame element.
37    */
38    private final WebElement iframe;
39   
40    /**
41    * Creates a new rich text area element.
42    *
43    * @param iframe the in-line frame used by the rich text area
44    */
 
45  22 toggle public RichTextAreaElement(WebElement iframe)
46    {
47  22 this.iframe = iframe;
48    }
49   
50    /**
51    * @return the inner text of the rich text area
52    */
 
53  13 toggle public String getText()
54    {
55  13 String windowHandle = getDriver().getWindowHandle();
56  13 try {
57  13 return getActiveElement().getText();
58    } finally {
59  13 getDriver().switchTo().window(windowHandle);
60    }
61    }
62   
63    /**
64    * Clears the content of the rich text area.
65    */
 
66  7 toggle public void clear()
67    {
68  7 String windowHandle = getDriver().getWindowHandle();
69  7 try {
70  7 getActiveElement().clear();
71    } finally {
72  7 getDriver().switchTo().window(windowHandle);
73    }
74    }
75   
76    /**
77    * Clicks on the rich text area.
78    */
 
79  0 toggle public void click()
80    {
81  0 String windowHandle = getDriver().getWindowHandle();
82  0 try {
83  0 getActiveElement().click();
84    } finally {
85  0 getDriver().switchTo().window(windowHandle);
86    }
87    }
88   
89    /**
90    * Simulate typing in the rich text area.
91    *
92    * @param keysToSend the sequence of keys to by typed
93    */
 
94  15 toggle public void sendKeys(CharSequence... keysToSend)
95    {
96  15 if (keysToSend.length > 0) {
97  15 String windowHandle = getDriver().getWindowHandle();
98  15 try {
99  15 getActiveElement().sendKeys(keysToSend);
100    } finally {
101  15 getDriver().switchTo().window(windowHandle);
102    }
103    }
104    }
105   
106    /**
107    * Executes the given script in the context of the rich text area.
108    *
109    * @param script the script to be executed
110    * @param arguments the script arguments
111    * @return the result of the script execution
112    * @see JavascriptExecutor#executeScript(String, Object...)
113    */
 
114  2 toggle public Object executeScript(String script, Object... arguments)
115    {
116  2 String windowHandle = getDriver().getWindowHandle();
117  2 try {
118  2 getActiveElement();
119  2 return ((JavascriptExecutor) getDriver()).executeScript(script.toString(), arguments);
120    } finally {
121  2 getDriver().switchTo().window(windowHandle);
122    }
123    }
124   
125    /**
126    * @return the HTML content of the rich text area
127    */
 
128  0 toggle public String getContent()
129    {
130  0 return (String) executeScript("return document.body.innerHTML");
131    }
132   
133    /**
134    * Sets the HTML content of the rich text area
135    *
136    * @param content the new HTML content
137    */
 
138  2 toggle public void setContent(String content)
139    {
140  2 executeScript("document.body.innerHTML = arguments[0];", content);
141    }
142   
143    /**
144    * @return the HTML element that has the focus in the Rich editor
145    */
 
146  37 toggle private WebElement getActiveElement()
147    {
148    // Switch to the iframe containing the rich text area content.
149    //
150    // Note: ATM this line doesn't work with Ghostdriver 1.0.3/PhantomJS 1.9, see
151    // https://github.com/detro/ghostdriver/issues/226
152    // Current workaround: WebDriver frameDriver = getDriver().switchTo().frame(1);
153  37 WebDriver frameDriver = getDriver().switchTo().frame(iframe);
154   
155    // Select the locator allowing us to find the current active element (element with the focus).
156  37 WebDriver.TargetLocator locator = frameDriver.switchTo();
157   
158    // Return the active element
159  37 return locator.activeElement();
160    }
161    }