Author: lfryc(a)redhat.com
Date: 2009-10-26 14:32:06 -0400 (Mon, 26 Oct 2009)
New Revision: 15759
Added:
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/java/org/jboss/test/selenium/utils/ColorUtils.java
Modified:
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/java/org/jboss/test/selenium/AbstractSeleniumTestCase.java
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/resources/jbossqa-extensions.js
Log:
- added ColorUtils to support conversions of colours in format of IE and Firefox
(RFPL-208)
- added method to implement loading scripts to tested application
Modified:
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/java/org/jboss/test/selenium/AbstractSeleniumTestCase.java
===================================================================
---
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/java/org/jboss/test/selenium/AbstractSeleniumTestCase.java 2009-10-26
12:42:49 UTC (rev 15758)
+++
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/java/org/jboss/test/selenium/AbstractSeleniumTestCase.java 2009-10-26
18:32:06 UTC (rev 15759)
@@ -29,6 +29,7 @@
import java.text.MessageFormat;
import java.util.Properties;
+import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.jboss.test.selenium.waiting.Condition;
@@ -818,5 +819,27 @@
}
throw new IllegalStateException("getAttributeOrNull unexpected state -
" + e.getMessage(), e);
}
- }
+ }
+
+ /**
+ * Add required script to page once.
+ *
+ * (This code will refuse adding script to the page duplicitly due to usage
+ * of hash, so you can add it thought you are not sure script is already
+ * added or not - this is usefull for adding libraries directly to the
+ * page).
+ *
+ * This script will be appended to end of the body tag within the script
+ * tag.
+ *
+ * @param script what should be added to the page
+ */
+ public void addRequiredScript(String script) {
+ final String escapedScript = StringEscapeUtils.escapeJavaScript(script);
+ final String id = "selenium_script_" +
Integer.toString(escapedScript.hashCode());
+ if (selenium.isElementPresent(id)) {
+ return;
+ }
+ selenium.getEval(format("selenium.addScriptLocally('{0}',
'{1}')", id, escapedScript));
+ }
}
Added:
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/java/org/jboss/test/selenium/utils/ColorUtils.java
===================================================================
---
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/java/org/jboss/test/selenium/utils/ColorUtils.java
(rev 0)
+++
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/java/org/jboss/test/selenium/utils/ColorUtils.java 2009-10-26
18:32:06 UTC (rev 15759)
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ *******************************************************************************/
+
+package org.jboss.test.selenium.utils;
+
+import java.awt.Color;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.commons.lang.Validate;
+
+/**
+ * Provides Color manipulations and functionality.
+ *
+ * @author <a href="mailto:lfryc@redhat.com">Lukas Fryc</a>
+ * @version $Revision$
+ */
+public class ColorUtils {
+ /**
+ * <p>
+ * Converts a string representation of color to integer.
+ * </p>
+ *
+ * <p>
+ * Works with two formats:
+ * </p>
+ *
+ * <ul>
+ * <li><code>#09FE4A</code> -
<b>hexadecimal</b></li>
+ * <li><code>rgb(132, 5, 18)</code> -
<b>decimal</b></li>
+ * </ul>
+ *
+ * @param colorValue
+ * string represented in one of two formats
+ * @return integer value of color derived from string representation
+ */
+ public static int convertToInteger(String colorValue) {
+ Validate.notNull(colorValue);
+
+ int result = 0;
+
+ if (colorValue.charAt(0) == '#') {
+ result = Integer.parseInt(colorValue.substring(1), 16);
+ } else {
+ Matcher matcher = Pattern.compile("(\\d+)").matcher(colorValue);
+ for (int i = 1; i <= 3; i++) {
+ if (!matcher.find()) {
+ throw new IllegalArgumentException(colorValue);
+ }
+
+ if (i != 1) {
+ result <<= 8;
+ }
+
+ result |= (0xFF & Short.parseShort(matcher.group(1)));
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * <p>
+ * Converts a string representation of color to AWT Color object.
+ * </p>
+ *
+ * <p>
+ * Works with two formats:
+ * </p>
+ *
+ * <ul>
+ * <li><code>#09FE4A</code> -
<b>hexadecimal</b></li>
+ * <li><code>rgb(132, 5, 18)</code> -
<b>decimal</b></li>
+ * </ul>
+ *
+ * @param colorValue
+ * string represented in one of two formats
+ * @return AWT's Color value representation of string-represented
+ * colorValue; if colorValue is null, returns null
+ */
+ public static Color convertToAWTColor(String colorValue) {
+ if (colorValue == null) {
+ return null;
+ }
+ int convertedValue = convertToInteger(colorValue);
+ return new Color(convertedValue);
+ }
+}
Modified:
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/resources/jbossqa-extensions.js
===================================================================
---
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/resources/jbossqa-extensions.js 2009-10-26
12:42:49 UTC (rev 15758)
+++
branches/community/3.3.X/test-applications/selenium-testing-lib/src/main/resources/jbossqa-extensions.js 2009-10-26
18:32:06 UTC (rev 15759)
@@ -183,4 +183,30 @@
}
return getText(element).trim();
};
+
+/**
+ * Add required script to page.
+ *
+ * Use a id of script (ideally based on hash of script) to avoid adding the same script
twice.
+ *
+ * (This code should refuse adding script to the page duplicitly due to usage
+ * of hash-id, so you can add it thought you are not sure script is already
+ * added or not - this is usefull for adding libraries directly to the
+ * page).
+ *
+ * Script will be appended to end of the body tag within the script
+ * tag.
+ *
+ * @param script what should be added to the page
+ */
+Selenium.prototype.addScriptLocally = function(id, script) {
+ var inDocument = this.browserbot.getCurrentWindow().document;
+ if (inDocument.getElementById(id)) {
+ return;
+ }
+ var scriptTag = inDocument.createElement('script');
+ scriptTag.id = id;
+ scriptTag.innerHTML = script;
+ inDocument.body.appendChild(scriptTag);
+};
\ No newline at end of file