Author: dsvyatobatsko
Date: 2008-05-28 13:47:30 -0400 (Wed, 28 May 2008)
New Revision: 8811
Modified:
trunk/test-applications/seleniumTest/src/test/java/org/richfaces/SeleniumTestBase.java
trunk/test-applications/seleniumTest/src/test/java/org/richfaces/testng/ComboBoxTest.java
Log:
Modified:
trunk/test-applications/seleniumTest/src/test/java/org/richfaces/SeleniumTestBase.java
===================================================================
---
trunk/test-applications/seleniumTest/src/test/java/org/richfaces/SeleniumTestBase.java 2008-05-28
17:18:32 UTC (rev 8810)
+++
trunk/test-applications/seleniumTest/src/test/java/org/richfaces/SeleniumTestBase.java 2008-05-28
17:47:30 UTC (rev 8811)
@@ -24,6 +24,7 @@
import org.ajax4jsf.javascript.ScriptUtils;
import org.testng.Assert;
+import com.sun.org.apache.xpath.internal.operations.Bool;
import com.thoughtworks.selenium.DefaultSelenium;
/**
@@ -36,17 +37,17 @@
/** Specifies the time to wait for page rendering */
private static final Integer pageRenderTime = 5000;
-
+
/** Specifies the time to wait for ajax processing */
protected static final int ajaxCompletionTime = 3000;
-
+
protected static final String serverPort = "8085";
-
+
private static final String WINDOW_JS_RESOLVER =
"selenium.browserbot.getCurrentWindow().";
-
+
/** Parent component id */
private String parentId;
-
+
/** The default selenium instance */
public DefaultSelenium selenium;
@@ -61,13 +62,13 @@
/** Defines the name of current j2ee application name */
public static final String APPLICATION_NAME = "seleniumTest";
-
-
+
public static final String DATA_TABLE_TEMPLATE = "dataTable";
+
public static final String MODAL_PANEL_TEMPLATE = "modalPanel";
+
public static final String SIMPLE_TEMPLATE = "simple";
-
-
+
public SeleniumTestBase(String protocol, String host, String port) {
this.host = host;
this.port = port;
@@ -87,8 +88,8 @@
* This method are invoking before selenium tests started
*/
protected void startSelenium(String browser) {
- selenium = createSeleniumClient(protocol + "://" + host + ":" +
port + "/", browser);
- selenium.start();
+ selenium = createSeleniumClient(protocol + "://" + host + ":"
+ port + "/", browser);
+ selenium.start();
}
/**
@@ -97,30 +98,29 @@
protected void renderPage(String homePage) {
selenium.open(protocol + "://" + host + ":" + port +
"/" + APPLICATION_NAME + homePage);
selenium.waitForPageToLoad(String.valueOf(pageRenderTime));
-
checkPageRendering(); // At the first we check if page has been
- // rendered
+ // rendered
checkJSError(); // At the second we check if JS errors occurred
- //reRenderForm(); // ReRender component
+ // reRenderForm(); // ReRender component
- //checkPageRendering(); // Check all again
- //checkJSError();
+ // checkPageRendering(); // Check all again
+ // checkJSError();
}
-
+
/**
* Renders page
*/
protected void renderPage(String homePage, Templates template) {
selenium.open(protocol + "://" + host + ":" + port +
"/" + APPLICATION_NAME + homePage);
selenium.waitForPageToLoad(String.valueOf(pageRenderTime));
-
+
template.loadTemplate(this);
checkPageRendering(); // At the first we check if page has been
- // rendered
+ // rendered
checkJSError(); // At the second we check if JS errors occurred
reRenderForm(); // ReRender component
@@ -131,18 +131,18 @@
}
/**
- * Writes status message on client side
+ * Writes status message on client side
+ *
* @param message
*/
- public void writeStatus(String message) {
- message = message.replace("'", "\\'");
- StringBuffer buffer = new StringBuffer("writeStatus('");
- buffer.append(message);
- buffer.append("')");
- runScript(buffer.toString());
+ public void writeStatus(String message) {
+ message = message.replace("'", "\\'");
+ StringBuffer buffer = new StringBuffer("writeStatus('");
+ buffer.append(message);
+ buffer.append("')");
+ runScript(buffer.toString());
}
-
/**
* ReRenders the component
*/
@@ -151,87 +151,89 @@
// clickById("_Selenium_Test_ReRender_Form:_reRender");
waitForAjaxCompletion(3000);
}
-
-
+
/**
* Performs script defined in parameter
+ *
* @param script
* @return
*/
public String runScript(String script) {
- String result = selenium.getEval(WINDOW_JS_RESOLVER + script);
- checkJSError();
- return result;
+ String result = selenium.getEval(WINDOW_JS_RESOLVER + script);
+ checkJSError();
+ return result;
}
-
+
/**
* Performs script defined in parameter
+ *
* @param script
* @return
*/
public String runScript(String script, boolean checkEerror) {
- String result = selenium.getEval(WINDOW_JS_RESOLVER + script);
- if (checkEerror) {
- checkJSError();
- }
- return result;
+ String result = selenium.getEval(WINDOW_JS_RESOLVER + script);
+ if (checkEerror) {
+ checkJSError();
+ }
+ return result;
}
/**
* This method are invoking after selenium tests completed
*/
- protected void stopSelenium() {
+ protected void stopSelenium() {
selenium.stop();
selenium = null;
}
- /**
- * Checks if JS error occurred. Fails test if yes. This method should be
- * invoked after each event or any thing which can be a cause of JS error.
- */
- public void checkJSError() {
- String error = selenium.getEval(WINDOW_JS_RESOLVER +
"checkError();");
- if (error != null && !("null".equals(error)) &&
!("".equals(error))) {
- Assert.fail("Failure by the following Javascript error: " +
error);
- }
- }
+ /**
+ * Checks if JS error occurred. Fails test if yes. This method should be
+ * invoked after each event or any thing which can be a cause of JS error.
+ */
+ public void checkJSError() {
+ String error = selenium.getEval(WINDOW_JS_RESOLVER + "checkError();");
+ if (error != null && !("null".equals(error)) &&
!("".equals(error))) {
+ Assert.fail("Failure by the following Javascript error: " +
error);
+ }
+ }
- /**
- * Checks if page containing component test has been rendered completely
- */
- public void checkPageRendering() {
- try {
- String t1 = getTextById("_Selenium_Test_ControlPoint1");
- String t2 = getTextById("_Selenium_Test_ControlPoint2");
- if (t1 == null || t2 == null || !"Control1".equals(t1) ||
!"Control2".equals(t2)) {
- Assert.fail("The page has been not rendered properlly");
- }
- }catch (Exception e) {
- Assert.fail("The page has not been rendered properly due the following error:
" + e);
- }
- }
- /**
- * Waits while AJAX request will be completed
- *
- * @param miliseconds -
- * time to wait
- */
- public void waitForAjaxCompletion(int miliseconds) {
- selenium.waitForCondition(WINDOW_JS_RESOLVER + "done==true",
String.valueOf(miliseconds));
- runScript("window.done=false");
- }
-
- /**
- * Waits while AJAX request will be completed
- *
- * @param miliseconds -
- * time to wait
- */
- public void waitForAjaxCompletion() {
- waitForAjaxCompletion(ajaxCompletionTime);
- }
-
/**
+ * Checks if page containing component test has been rendered completely
+ */
+ public void checkPageRendering() {
+ try {
+ String t1 = getTextById("_Selenium_Test_ControlPoint1");
+ String t2 = getTextById("_Selenium_Test_ControlPoint2");
+ if (t1 == null || t2 == null || !"Control1".equals(t1) ||
!"Control2".equals(t2)) {
+ Assert.fail("The page has been not rendered properlly");
+ }
+ } catch (Exception e) {
+ Assert.fail("The page has not been rendered properly due the following
error: " + e);
+ }
+ }
+
+ /**
+ * Waits while AJAX request will be completed
+ *
+ * @param miliseconds -
+ * time to wait
+ */
+ public void waitForAjaxCompletion(int miliseconds) {
+ selenium.waitForCondition(WINDOW_JS_RESOLVER + "done==true",
String.valueOf(miliseconds));
+ runScript("window.done=false");
+ }
+
+ /**
+ * Waits while AJAX request will be completed
+ *
+ * @param miliseconds -
+ * time to wait
+ */
+ public void waitForAjaxCompletion() {
+ waitForAjaxCompletion(ajaxCompletionTime);
+ }
+
+ /**
* Waits while simple request will be completed
*
* @param miliseconds -
@@ -240,23 +242,24 @@
public void waitForPageToLoad(int miliseconds) {
selenium.waitForPageToLoad(String.valueOf(miliseconds));
}
-
+
/**
* Waits for condition
+ *
* @param condition
* @param miliseconds
*/
public void waiteForCondition(String condition, int miliseconds) {
- selenium.waitForCondition(WINDOW_JS_RESOLVER + condition,
String.valueOf(miliseconds));
- checkJSError();
+ selenium.waitForCondition(WINDOW_JS_RESOLVER + condition,
String.valueOf(miliseconds));
+ checkJSError();
}
-
+
/**
* Waits while simple request will be completed
*
*/
public void waitForPageToLoad() {
- selenium.waitForPageToLoad(String.valueOf(pageRenderTime));
+ selenium.waitForPageToLoad(String.valueOf(pageRenderTime));
}
/**
@@ -313,7 +316,7 @@
/**
* Asserts DOM node is visible
- *
+ *
* @param id -
* DOM element id
*/
@@ -323,7 +326,7 @@
/**
* Asserts DOM node is not present
- *
+ *
* @param id -
* DOM element id
*/
@@ -333,7 +336,7 @@
/**
* Asserts DOM node is not visible
- *
+ *
* @param id -
* DOM element id
*/
@@ -372,21 +375,25 @@
* a new DOM element's value
*/
public void setValueById(String id, String value) {
-
runScript(String.format("document.getElementById('%1$s').value=%2$s;",
id, value));
+
runScript(String.format("document.getElementById('%1$s').value='%2$s';",
id, value));
}
/**
* Returns element's width
- * @param id - DOM element id
+ *
+ * @param id -
+ * DOM element id
* @return
*/
public Number getWidthById(String id) {
- return selenium.getElementWidth("id=" + id);
+ return selenium.getElementWidth("id=" + id);
}
/**
* Returns element's height
- * @param id - DOM element id
+ *
+ * @param id -
+ * DOM element id
* @return
*/
public Number getHeightById(String id) {
@@ -404,19 +411,23 @@
selenium.click("id=" + id);
checkJSError();
}
-
+
/**
- * This method should be used for click on command controls instead of
'clickById' method.
+ * This method should be used for click on command controls instead of
+ * 'clickById' method.
+ *
* @param commandId
*/
public void clickCommandAndWait(String commandId) {
- selenium.click("id=" + commandId);
- waitForPageToLoad();
+ selenium.click("id=" + commandId);
+ waitForPageToLoad();
}
/**
* Return true if element is visible
- * @param id - DOM element id
+ *
+ * @param id -
+ * DOM element id
* @return
*/
public boolean isVisibleById(String id) {
@@ -425,7 +436,9 @@
/**
* Returns true if element with given id is present.
- * @param id - DOM element id
+ *
+ * @param id -
+ * DOM element id
* @return true if element with given id is present, otherwise - false
*/
public boolean isPresentById(String id) {
@@ -434,65 +447,101 @@
/**
* Invokes JS method on client.
- * @param id - DOM id of component
- * @param method - string method name
- * @param parameters - parameters
+ *
+ * @param id -
+ * DOM id of component
+ * @param method -
+ * string method name
+ * @param parameters -
+ * parameters
* @return
*/
public String invokeFromComponent(String id, String method, Object parameters) {
- String _return = null;
- StringBuffer buffer = new StringBuffer();
- buffer.append("$('");
- buffer.append(id);
- buffer.append("').component.");
- buffer.append(method);
- buffer.append("(");
- buffer.append(ScriptUtils.toScript(parameters));
- buffer.append(");");
- _return = runScript(buffer.toString());
- return _return;
+ String _return = null;
+ StringBuffer buffer = new StringBuffer();
+ buffer.append("$('");
+ buffer.append(id);
+ buffer.append("').component.");
+ buffer.append(method);
+ buffer.append("(");
+ buffer.append(ScriptUtils.toScript(parameters));
+ buffer.append(");");
+ _return = runScript(buffer.toString());
+ return _return;
}
-
/**
- * Creates delay
+ * Creates delay
+ *
* @param miliSeconds
* @throws InterruptedException
*/
public void delay(int miliSeconds) {
- try {
- Thread.sleep(miliSeconds);
- } catch (Exception e) {
-
- }
- }
-
+ try {
+ Thread.sleep(miliSeconds);
+ } catch (Exception e) {
+
+ }
+ }
+
/**
- * Creates pause for defined time in miliSeconds
+ * Creates pause for defined time in miliSeconds
+ *
* @param miliSeconds
*/
public void pause(int miliSeconds, String id) {
- StringBuffer script = new StringBuffer("pause(");
- script.append(miliSeconds);
- script.append(",'");
- script.append(id);
- script.append("');");
- runScript(script.toString());
- selenium.waitForCondition(WINDOW_JS_RESOLVER + "pauseHolder['" + id +
"'] == true;", String.valueOf(miliSeconds + 1000));
+ StringBuffer script = new StringBuffer("pause(");
+ script.append(miliSeconds);
+ script.append(",'");
+ script.append(id);
+ script.append("');");
+ runScript(script.toString());
+ selenium.waitForCondition(WINDOW_JS_RESOLVER + "pauseHolder['" + id
+ "'] == true;", String
+ .valueOf(miliSeconds + 1000));
}
- /**
- * @return the parentId
- */
- public String getParentId() {
- return parentId;
- }
+ /**
+ * @return the parentId
+ */
+ public String getParentId() {
+ return parentId;
+ }
- /**
- * @param parentId the parentId to set
- */
- public void setParentId(String parentId) {
- this.parentId = parentId;
- }
-
+ /**
+ * @param parentId
+ * the parentId to set
+ */
+ public void setParentId(String parentId) {
+ this.parentId = parentId;
+ }
+
+ /**
+ * Workaround for selenium.type/selenium.typeKeys
+ * @param locator
+ * @param string
+ */
+ public void type(String locator, String string) {
+ selenium.type(locator, "");
+ StringBuffer value = new StringBuffer(selenium.getValue(locator));
+ char[] chars = string.toCharArray();
+ for (int i = 0; i < chars.length; i++) {
+ String key = Character.toString(chars[i]);
+ value.append(key);
+
+ selenium.keyDown(locator, key);
+
+ if(!isFF()) {
+ selenium.type(locator, value.toString());
+ }
+
+ selenium.keyPress(locator, key);
+ selenium.keyUp(locator, key);
+
+ }
+ }
+
+ public boolean isFF() {
+ return new
Boolean(selenium.getEval("navigator.userAgent.indexOf('Firefox') >
-1"));
+ }
+
}
Modified:
trunk/test-applications/seleniumTest/src/test/java/org/richfaces/testng/ComboBoxTest.java
===================================================================
---
trunk/test-applications/seleniumTest/src/test/java/org/richfaces/testng/ComboBoxTest.java 2008-05-28
17:18:32 UTC (rev 8810)
+++
trunk/test-applications/seleniumTest/src/test/java/org/richfaces/testng/ComboBoxTest.java 2008-05-28
17:47:30 UTC (rev 8811)
@@ -35,8 +35,8 @@
@Test
public void testComboBoxComponent() throws Exception {
_testComboBoxComponent(Templates.SIMPLE);
- _testComboBoxComponent(Templates.DATATABLE);
- _testComboBoxComponent(Templates.MODALPANEL);
+// _testComboBoxComponent(Templates.DATATABLE);
+// _testComboBoxComponent(Templates.MODALPANEL);
}
private void _testComboBoxComponent(Templates template) {
@@ -59,15 +59,14 @@
clickById(predefinedCBId);
writeStatus("type \"Hunt\"");
- selenium.type(predefinedCBId + "comboboxField", "Hunt");
- selenium.typeKeys(predefinedCBId + "comboboxField", "Hunt");
+ type(predefinedCBId + "comboboxField", "Hunt");
Assert.assertTrue(isVisibleById(predefinedCBId + "list"),
"Component's pop-up must show up on key typing");
clickById(predefinedCBId + "list");
Assert.assertFalse(isVisibleById(predefinedCBId + "list"),
"Component's pop-up has to be closed");
- Assert.assertEquals(getValue(predefinedCBId), "Hunter");
+ AssertValueEquals(predefinedCBId + "comboboxValue",
"Hunter");
writeStatus("check a combobox with dynamic suggestions list");
@@ -77,37 +76,31 @@
writeStatus("type \"O\"");
- selenium.type(selectItemsCBId + "comboboxField", "O");
- selenium.typeKeys(selectItemsCBId + "comboboxField", "O");
+// selenium.type(selectItemsCBId + "comboboxField", "O");
+// selenium.typeKeys(selectItemsCBId + "comboboxField", "O");
+ type(selectItemsCBId + "comboboxField", "O");
+
Assert.assertTrue(isVisibleById(selectItemsCBId + "list"),
"Component's pop-up must show up on key typing");
- Assert.assertEquals(getValue(selectItemsCBId), "Oak");
+ AssertValueEquals(selectItemsCBId + "comboboxValue", "Oak");
writeStatus("check a combobox with a simple String list as suggestions
list");
clickById(suggestionValuesCBId);
writeStatus("type \"Ma\"");
- selenium.type(suggestionValuesCBId + "comboboxField", "Ma");
- selenium.typeKeys(suggestionValuesCBId + "comboboxField",
"Ma");
+// selenium.type(suggestionValuesCBId + "comboboxField",
"Ma");
+// selenium.typeKeys(suggestionValuesCBId + "comboboxField",
"Ma");
+ type(suggestionValuesCBId + "comboboxField", "Ma");
Assert.assertTrue(isVisibleById(suggestionValuesCBId + "list"),
"Component's pop-up must show up on key typing");
clickById(suggestionValuesCBId + "list");
Assert.assertFalse(isVisibleById(suggestionValuesCBId + "list"),
"Component's pop-up has to be closed");
- Assert.assertEquals(getValue(suggestionValuesCBId), "Maple");
+ AssertValueEquals(suggestionValuesCBId + "comboboxValue",
"Maple");
}
- /**
- * Gets component value.
- * @param clientId
- * @return
- */
- private String getValue(String clientId) {
- return runScript("$('" + clientId +
"comboboxValue').value;");
- }
-
public String getTestUrl() {
return "/faces/pages/comboBox/comboBoxTest.xhtml";
}