|
| 1 | +package com.polopoly.ps.psselenium.agent; |
| 2 | + |
| 3 | + |
| 4 | +import org.json.simple.JSONValue; |
| 5 | +import org.openqa.selenium.By; |
| 6 | +import org.openqa.selenium.JavascriptExecutor; |
| 7 | +import org.openqa.selenium.WebElement; |
| 8 | + |
| 9 | +/** |
| 10 | + * This agent interacts on a CKEditor field in Polopoly Admin GUI. |
| 11 | + */ |
| 12 | +public class CKEditorAgent implements WYSIWYGEditorAgent { |
| 13 | + private final GUIAgent guiAgent; |
| 14 | + |
| 15 | + public CKEditorAgent(GUIAgent guiAgent) { |
| 16 | + this.guiAgent = guiAgent; |
| 17 | + } |
| 18 | + |
| 19 | + /** |
| 20 | + * Sets a text content into the all present CKEditor fields |
| 21 | + * @param text the text content to set |
| 22 | + */ |
| 23 | + public void setText(final String text) throws Exception { |
| 24 | + |
| 25 | + waitForCKEditor(); |
| 26 | + JavascriptExecutor executor = (JavascriptExecutor) guiAgent.getWebDriver(); |
| 27 | + executor.executeScript("var fck; for (var f in window.CKEDITOR.instances)" |
| 28 | + + "{ fck = window.CKEDITOR.instances[f]; };" |
| 29 | + + "fck.setData('" |
| 30 | + + JSONValue.escape(text) |
| 31 | + + "');"); |
| 32 | + |
| 33 | + waitforDirtyStateOnCKEditor(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Sets a text content into the CKEditor field with a specific field label |
| 38 | + * @param fieldLabel the field label |
| 39 | + * @param textValue the text content to set |
| 40 | + */ |
| 41 | + public void setText(String fieldLabel, String textValue) |
| 42 | + throws Exception { |
| 43 | + |
| 44 | + String editorId = getEditorId(fieldLabel); |
| 45 | + waitForCKEditor(editorId); |
| 46 | + |
| 47 | + JavascriptExecutor executor = (JavascriptExecutor) guiAgent.getWebDriver(); |
| 48 | + executor.executeScript(getCKEditorJSString(editorId) + "fck.setData('" + textValue + "');"); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns the text content for a CKEditor present in the current frame pane |
| 53 | + * @return the text content |
| 54 | + */ |
| 55 | + public String getText() throws Exception { |
| 56 | + waitForCKEditor(); |
| 57 | + JavascriptExecutor executor = (JavascriptExecutor) guiAgent.getWebDriver(); |
| 58 | + String result = (String) executor.executeScript("var fck; for (var f in window.CKEDITOR.instances)" |
| 59 | + + "{ fck = window.CKEDITOR.instances[f]; };" |
| 60 | + + " return fck.getData();"); |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the text content for a CKEditor with a specific field label |
| 66 | + * @return the text content |
| 67 | + */ |
| 68 | + public String getText(String fieldLabel) throws Exception { |
| 69 | + |
| 70 | + String editorId = getEditorId(fieldLabel); |
| 71 | + waitForCKEditor(editorId); |
| 72 | + JavascriptExecutor executor = (JavascriptExecutor) guiAgent.getWebDriver(); |
| 73 | + String result = (String) executor.executeScript(getCKEditorJSString(editorId) |
| 74 | + + "return fck.getData();"); |
| 75 | + return result; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Executes a command on a CKEditor that is present in the current frame pane |
| 80 | + * @param commandName the command name to execute |
| 81 | + * @throws Exception |
| 82 | + */ |
| 83 | + public void executeCommand(String commandName) throws Exception { |
| 84 | + waitForCKEditor(); |
| 85 | + JavascriptExecutor executor = (JavascriptExecutor) guiAgent.getWebDriver(); |
| 86 | + executor.executeScript("var fck; for (var f in window.CKEDITOR.instances)" |
| 87 | + + "{ fck = window.CKEDITOR.instances[f]; };" |
| 88 | + + "fck.execCommand('" + commandName + "');"); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Executes a command on a CKEditor with a field label |
| 93 | + * @param fieldLabel the field label |
| 94 | + * @param command the command to execute |
| 95 | + * |
| 96 | + */ |
| 97 | + public void executeCommand(String fieldLabel, String command) throws Exception { |
| 98 | + String editorId = getEditorId(fieldLabel); |
| 99 | + waitForCKEditor(editorId); |
| 100 | + JavascriptExecutor executor = (JavascriptExecutor) guiAgent.getWebDriver(); |
| 101 | + executor.executeScript(getCKEditorJSString(editorId) + "fck.execCommand('" |
| 102 | + + command + "');"); |
| 103 | + } |
| 104 | + |
| 105 | + private String getCKEditorJSString(String id) { |
| 106 | + return "var fck = window.CKEDITOR.instances['" |
| 107 | + + id + "'];"; |
| 108 | + } |
| 109 | + |
| 110 | + public void waitForCKEditor(final String editorId) throws Exception { |
| 111 | + |
| 112 | + guiAgent.waitAgent().waitForCondition("return window.CKEDITOR != null"); |
| 113 | + guiAgent.waitAgent().waitForCondition(getCKEditorJSString(editorId) |
| 114 | + + "try{fck.EditorWindow.document}catch(e){} return fck != null;"); |
| 115 | + } |
| 116 | + |
| 117 | + public void waitForCKEditor() throws Exception { |
| 118 | + |
| 119 | + guiAgent.waitAgent().waitForCondition("return window.CKEDITOR != null"); |
| 120 | + |
| 121 | + String secondCondition = "var fck = null; var fckInstId = null; for (var f in window.CKEDITOR.instances)" |
| 122 | + + "{ fck = window.CKEDITOR.instances[f]; fckInstId = f; } " |
| 123 | + + " try{fck.EditorWindow.document}catch(e){} return fckInstId != null"; |
| 124 | + guiAgent.waitAgent().waitForCondition(secondCondition); |
| 125 | + } |
| 126 | + |
| 127 | + public void waitforDirtyStateOnCKEditor() |
| 128 | + throws Exception { |
| 129 | + guiAgent.waitAgent().waitForCondition("var fck; for (var f in window.CKEDITOR.instances)" |
| 130 | + + "{ fck = window.CKEDITOR.instances[f]; };" |
| 131 | + + "return fck.checkDirty();"); |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + private String getEditorId(String label) |
| 136 | + throws Exception { |
| 137 | + |
| 138 | + WebElement webElement = |
| 139 | + guiAgent.getWebDriver().findElement(By.xpath("//fieldset/h2[text() = '" |
| 140 | + + label |
| 141 | + + "']/../textarea")); |
| 142 | + return webElement.getAttribute("id"); |
| 143 | + } |
| 144 | +} |
0 commit comments