Skip to content

Commit fb7af9c

Browse files
Initial commit
1 parent 0663b0b commit fb7af9c

21 files changed

+2226
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.polopoly.ps.psselenium.agent;
2+
3+
import org.openqa.selenium.JavascriptExecutor;
4+
import org.openqa.selenium.WebDriver;
5+
6+
/**
7+
* This agent triggers javascript action events based on the Polopoly Orchid Framework.
8+
* This agent should be used with caution.
9+
*/
10+
public class ActionEventAgent {
11+
12+
private final GUIAgent guiAgent;
13+
14+
public ActionEventAgent(GUIAgent guiAgent) {
15+
this.guiAgent = guiAgent;
16+
}
17+
18+
/**
19+
* Triggers an action event on the specified content id and waits for page to load .
20+
* @param action the action to trigger
21+
* @param contentId the content the action will be trigger on
22+
* @return
23+
*/
24+
public ActionEventAgent triggerActionEvent(String action, String contentId, String target) {
25+
26+
WebDriver webDriver = guiAgent.getWebDriver();
27+
String openScript =
28+
"actionEventData({$action:'"+ action +"', $contentId:'"
29+
+ contentId + "', $target:'"+target+"'});";
30+
31+
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
32+
executor.executeScript(openScript);
33+
34+
return this;
35+
}
36+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.polopoly.ps.psselenium.agent;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.JavascriptExecutor;
5+
import org.openqa.selenium.WebDriver;
6+
/**
7+
* This agent interacts with the clipboard in the Polopoly Admin GUI.
8+
*/
9+
public class ClipboardAgent {
10+
11+
private final GUIAgent guiAgent;
12+
13+
public ClipboardAgent(GUIAgent guiAgent) {
14+
this.guiAgent = guiAgent;
15+
}
16+
17+
/**
18+
* Copy the current opened content into the clipboard
19+
* @return this agent
20+
* @throws Exception
21+
*/
22+
public ClipboardAgent copyOpenedContent() throws Exception {
23+
24+
WebDriver webDriver = guiAgent.getWebDriver();
25+
webDriver.findElement(
26+
By.xpath("//div[contains(@class,'content-info')]/button[contains(@class, 'clipboard')]")).click();
27+
28+
return this;
29+
}
30+
31+
/**
32+
* Copy a specific content into the clipboard present in the current frame pane
33+
* @param contentIdString the content's id to copy
34+
* @return this agent
35+
* @throws Exception
36+
*/
37+
public ClipboardAgent copyContent(String contentIdString) throws Exception {
38+
WebDriver webDriver = guiAgent.getWebDriver();
39+
webDriver.findElement(By.xpath("//button[contains(@class, 'clipboard') and contains(@onclick, '"
40+
+ contentIdString + "')]")).click();
41+
return this;
42+
}
43+
44+
/**
45+
* Paste the current content in the clipboard into a specific target
46+
* @param target the target's label to paste in
47+
* @return this agent
48+
* @throws Exception
49+
*/
50+
public ClipboardAgent pasteContent(String target)
51+
throws Exception {
52+
53+
String pasteSelectorPrefix = "//h2[contains(text(),'"+target+"')]";
54+
String pasteSelectorSuffix = "/button[contains(@class, 'clipboard') and contains(@title, 'Paste')]";
55+
56+
// Slots
57+
String pasteSelector = pasteSelectorPrefix + "/../" + pasteSelectorSuffix;
58+
59+
WebDriver webDriver = guiAgent.getWebDriver();
60+
61+
if (webDriver.findElements(By.xpath(pasteSelector)).isEmpty()) {
62+
// Content lists
63+
pasteSelector = pasteSelectorPrefix + pasteSelectorSuffix;
64+
}
65+
66+
if (webDriver.findElements(By.xpath(pasteSelector)).isEmpty()) {
67+
pasteSelector = "//fieldset[contains(@class, '"+target+"']/" + pasteSelectorSuffix;
68+
}
69+
70+
webDriver.findElement(By.xpath(pasteSelector)).click();
71+
guiAgent.waitAgent().waitForAjaxPageToLoad();
72+
73+
return this;
74+
}
75+
76+
/**
77+
* Returns the size of clipboard
78+
* @return the size of the clipboard
79+
*/
80+
public int getClipboardSize()
81+
{
82+
JavascriptExecutor executer = (JavascriptExecutor) guiAgent.getWebDriver();
83+
Long count = (Long) executer.executeScript("return window.JSClipboard.getInstance().getLatestClip().isMultiClip == null ? "
84+
+ "1 : window.JSClipboard.getInstance().getLatestClip().getClipCount();");
85+
return count.intValue();
86+
}
87+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.polopoly.ps.psselenium.agent;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.support.ui.Select;
7+
8+
/**
9+
* This agent interacts with Polopoly content creator fields in the Polopoly Admin GUI.
10+
*/
11+
public class ContentCreatorAgent {
12+
13+
private final GUIAgent guiAgent;
14+
15+
public ContentCreatorAgent(GUIAgent guiAgent) {
16+
this.guiAgent = guiAgent;
17+
}
18+
19+
/**
20+
* Creates a content using the content creator with a specific section title.
21+
* @param sectionTitle the title of the content creator container
22+
*/
23+
public ContentCreatorAgent createContent(String sectionTitle) {
24+
25+
WebDriver webDriver = guiAgent.getWebDriver();
26+
String selectLocator = "//h2[contains(text(),'"+ sectionTitle+"')]";
27+
28+
WebElement webElement = webDriver.findElement(By.xpath(selectLocator + "/..//button[1]"));
29+
webElement.click();
30+
guiAgent.waitAgent().waitForPageToLoad();
31+
32+
return this;
33+
}
34+
35+
/**
36+
* Creates a content using the content creator with a specific section title and content type label.
37+
* @param sectionTitle the title of the content creator container
38+
* @param contentTypeOptionLabel the label of the content type
39+
*/
40+
public ContentCreatorAgent createContent(String sectionTitle, String contentTypeOptionLabel) {
41+
42+
WebDriver webDriver = guiAgent.getWebDriver();
43+
String selectLocator = selectOnPosition(sectionTitle,
44+
contentTypeOptionLabel, 1);
45+
46+
WebElement webElement = webDriver.findElement(By.xpath(selectLocator + "/../button[1]"));
47+
webElement.click();
48+
guiAgent.waitAgent().waitForPageToLoad();
49+
50+
return this;
51+
}
52+
/**
53+
* Creates a content using the content creator with a specific section title; category type label
54+
* and content type label.
55+
* @param sectionTitle the title of the content creator container
56+
* @param categoryTypeOptionLabel the label of the content creator category type
57+
* @param contentTypeOptionLabel the label of the content type
58+
*/
59+
public ContentCreatorAgent createContent(String sectionTitle,
60+
String categoryTypeOptionLabel,
61+
String contentTypeOptionLabel) {
62+
WebDriver webDriver = guiAgent.getWebDriver();
63+
selectOnPosition(sectionTitle, categoryTypeOptionLabel, 1);
64+
String selectLocator = selectOnPosition(sectionTitle,
65+
contentTypeOptionLabel, 2);
66+
WebElement webElement = webDriver.findElement(By.xpath(selectLocator + "/../button[1]"));
67+
webElement.click();
68+
guiAgent.waitAgent().waitForPageToLoad();
69+
70+
return this;
71+
}
72+
73+
/**
74+
* Selects an option with a specific label in a select input on specific position.
75+
* @param sectionTitle the content creator title
76+
* @param optionLabel the option label
77+
* @param position the position of the select input
78+
*/
79+
private String selectOnPosition(String sectionTitle, String optionLabel, int position) {
80+
81+
WebDriver webDriver = guiAgent.getWebDriver();
82+
83+
String selectLocatorPrefix = "//h2[contains(text(),'"+ sectionTitle+"')]";
84+
String selectLocatorSuffix = "select[position()="+position+"]/option[text()='"+optionLabel + "']/..";
85+
86+
String selectLocator = selectLocatorPrefix + "/../" + selectLocatorSuffix;
87+
if (webDriver.findElements(By.xpath(selectLocator)).isEmpty()) {
88+
selectLocator = selectLocatorPrefix + "/following-sibling::*/" + selectLocatorSuffix;
89+
}
90+
Select selectBox = new Select(webDriver.findElement(By.xpath(selectLocator)));
91+
selectBox.selectByVisibleText(optionLabel);
92+
93+
return selectLocator;
94+
}
95+
}

0 commit comments

Comments
 (0)