[seam-commits] Seam SVN: r11426 - in branches/community/Seam_2_2/src/test/ftest: src/main/org/jboss/seam/example/common/test/selenium and 1 other directory.
seam-commits at lists.jboss.org
seam-commits at lists.jboss.org
Tue Aug 25 16:00:55 EDT 2009
Author: kpiwko at redhat.com
Date: 2009-08-25 16:00:55 -0400 (Tue, 25 Aug 2009)
New Revision: 11426
Added:
branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/selenium/SeleniumDateSelector.java
Modified:
branches/community/Seam_2_2/src/test/ftest/seamgen/src/main/org/jboss/seam/test/functional/seamgen/GenerateEntitiesTest.java
branches/community/Seam_2_2/src/test/ftest/seamgen/src/main/org/jboss/seam/test/functional/seamgen/PersonTest.java
branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/selenium/SeamSelenium.java
Log:
JBQA-2276 selectDate() ftest method for Rich&IceFaces
Modified: branches/community/Seam_2_2/src/test/ftest/seamgen/src/main/org/jboss/seam/test/functional/seamgen/GenerateEntitiesTest.java
===================================================================
--- branches/community/Seam_2_2/src/test/ftest/seamgen/src/main/org/jboss/seam/test/functional/seamgen/GenerateEntitiesTest.java 2009-08-25 15:22:31 UTC (rev 11425)
+++ branches/community/Seam_2_2/src/test/ftest/seamgen/src/main/org/jboss/seam/test/functional/seamgen/GenerateEntitiesTest.java 2009-08-25 20:00:55 UTC (rev 11426)
@@ -24,10 +24,9 @@
import java.io.InputStream;
import java.util.Date;
+import org.jboss.seam.example.common.test.selenium.SeleniumDateSelector;
import org.testng.annotations.BeforeGroups;
-import com.thoughtworks.selenium.Wait;
-
/**
* This class and its subclasses test seam-gen's "generate-entities" feature.
* Every test method should be part of "generate-entitiesTest" in order to get
@@ -116,33 +115,28 @@
}
/**
- * Select a date using icefaces or richfaces calendar component. Selecting
- * hardcoded values is only implemented yet.
+ * Selects a date using icefaces or richfaces calendar component.
+ * @param date Date to be selected
*/
public void selectDate(Date date)
{
- // TODO
- final String richFaces = "id=person:birthdateField:birthdateDayCell24";
- final String iceFaces = "xpath=id('person:birthdateField')//table/tbody/tr[4]/td[4]/a";
- final String icefacesCalendarButton = "id=person:birthdateField:birthdate_cb";
- if (browser.isElementPresent(richFaces))
+ final String richFacesButton = "id=person:birthdateField:birthdatePopupButton";
+ final String richFacesCalendar = "xpath=//div[@class='rich-calendar-tool-btn' and text()='Today']";
+ final String iceFacesButton = "id=person:birthdateField:birthdate_cb";
+ final String iceFacesCalendar = "id=person:birthdateField:birthdate_ct";
+
+ if (browser.isElementPresent(richFacesButton))
{
- browser.click(richFaces);
+ browser.click(richFacesButton);
+ browser.waitForElement(richFacesCalendar);
+ SeleniumDateSelector.RICHFACES.setDate(browser, date);
}
- else if (browser.isElementPresent(icefacesCalendarButton))
+ else if (browser.isElementPresent(iceFacesButton))
{
- browser.click(icefacesCalendarButton);
- new Wait()
- {
-
- @Override
- public boolean until()
- {
- return browser.isElementPresent(iceFaces);
- }
- }.wait("Calendar did not appear.", Long.valueOf(SELENIUM_TIMEOUT));
- browser.click(iceFaces);
+ browser.click(iceFacesButton);
+ browser.waitForElement(iceFacesCalendar);
+ SeleniumDateSelector.ICEFACES.setDate(browser, date);
}
else
{
Modified: branches/community/Seam_2_2/src/test/ftest/seamgen/src/main/org/jboss/seam/test/functional/seamgen/PersonTest.java
===================================================================
--- branches/community/Seam_2_2/src/test/ftest/seamgen/src/main/org/jboss/seam/test/functional/seamgen/PersonTest.java 2009-08-25 15:22:31 UTC (rev 11425)
+++ branches/community/Seam_2_2/src/test/ftest/seamgen/src/main/org/jboss/seam/test/functional/seamgen/PersonTest.java 2009-08-25 20:00:55 UTC (rev 11426)
@@ -21,13 +21,13 @@
*/
package org.jboss.seam.test.functional.seamgen;
-import java.util.Date;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import java.util.Calendar;
+
import org.testng.annotations.Test;
-import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.assertEquals;
-
/**
* This test verifies CRUD functionality on the Person table.
* @author Jozef Hartinger
@@ -51,7 +51,9 @@
String username = "tester";
String address = "test address";
String name = "John Doe";
- createNewPerson(username, address, new Date(), name);
+ Calendar cal = Calendar.getInstance();
+ cal.set(1991, 10, 20);
+ createNewPerson(username, address, cal.getTime(), name);
assertTrue(browser.isElementPresent(MESSAGES), "Confirmation message expected.");
assertEquals(browser.getText(MESSAGES), "Successfully created", "Unexpected confirmation message");
@@ -77,7 +79,9 @@
String editButton = String.format(PERSON_LIST_EDIT_BUTTON_BY_NAME, username);
browser.clickAndWait(editButton);
// update the entity
- fillPersonEditPage(username, address, new Date(), name);
+ Calendar cal = Calendar.getInstance();
+ cal.set(1984, 02, 29);
+ fillPersonEditPage(username, address, cal.getTime(), name);
browser.clickAndWait(PERSON_UPDATE);
// verify
assertTrue(browser.isElementPresent(MESSAGES), "Confirmation message expected.");
Modified: branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/selenium/SeamSelenium.java
===================================================================
--- branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/selenium/SeamSelenium.java 2009-08-25 15:22:31 UTC (rev 11425)
+++ branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/selenium/SeamSelenium.java 2009-08-25 20:00:55 UTC (rev 11426)
@@ -113,6 +113,35 @@
}
/**
+ * Waits until element is asynchronously loaded on page. Uses global Selenium
+ * timeout
+ *
+ * @param locator Locator of element
+ */
+ public void waitForElement(final String locator)
+ {
+ waitForElement(locator, Long.valueOf(timeout));
+ }
+
+ /**
+ * Waits until element is asynchronously loaded on page.
+ *
+ * @param timeout Timeout in milliseconds
+ * @param locator Locator of element
+ */
+ public void waitForElement(final String locator, long timeout)
+ {
+ new Wait()
+ {
+ @Override
+ public boolean until()
+ {
+ return isElementPresent(locator);
+ }
+ }.wait("Timeout while waiting for asynchronous update of " + locator, timeout);
+ }
+
+ /**
* Returns true if icefaces detection is turned on
*/
public boolean isIcefacesDetection()
Added: branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/selenium/SeleniumDateSelector.java
===================================================================
--- branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/selenium/SeleniumDateSelector.java (rev 0)
+++ branches/community/Seam_2_2/src/test/ftest/src/main/org/jboss/seam/example/common/test/selenium/SeleniumDateSelector.java 2009-08-25 20:00:55 UTC (rev 11426)
@@ -0,0 +1,261 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, 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.seam.example.common.test.selenium;
+
+import java.text.DateFormatSymbols;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+import java.util.StringTokenizer;
+
+/**
+ * Selects date using Selenium in calendar provided by framework. Each framework
+ * is responsible for managing movement in calendar grid
+ *
+ * @author kpiwko
+ *
+ */
+public enum SeleniumDateSelector
+{
+ RICHFACES
+ {
+ @Override
+ public void setDay(SeamSelenium browser, int day)
+ {
+ String locator = "xpath=//td[contains(@class, 'rich-calendar-cell') and not(contains(@class,'rich-calendar-boundary-dates')) and text() ='" + Integer.toString(day) + "']";
+ browser.waitForElement(locator);
+ browser.click(locator);
+ }
+
+ @Override
+ public void setMonth(SeamSelenium browser, int month)
+ {
+ String monthLocator = "xpath=//div[@class='rich-calendar-tool-btn' and contains(.,',')]";
+ StringTokenizer stk = new StringTokenizer(browser.getText(monthLocator), ",");
+ String calMonth = stk.nextToken().trim();
+ int steps = monthStepsCount(calMonth, month);
+
+ movement(browser, "xpath=//div[@class='rich-calendar-tool-btn' and text()='<']", "xpath=//div[@class='rich-calendar-tool-btn' and text()='>']", steps);
+ }
+
+ @Override
+ public void setYear(SeamSelenium browser, int year)
+ {
+ String yearLocator = "xpath=//div[@class='rich-calendar-tool-btn' and contains(.,',')]";
+ StringTokenizer stk = new StringTokenizer(browser.getText(yearLocator), ",");
+ // omit first token
+ stk.nextToken();
+ String calYear = stk.nextToken().trim();
+ int steps = yearStepsCount(calYear, year);
+
+ movement(browser, "xpath=//div[@class='rich-calendar-tool-btn' and text()='<<']", "xpath=//div[@class='rich-calendar-tool-btn' and text()='>>'", steps);
+ }
+
+ },
+ ICEFACES
+ {
+ @Override
+ public void setDay(SeamSelenium browser, int day)
+ {
+ String locator = "xpath=//td[@class='iceSelInpDateDay']/a[./span/text()='" + Integer.toString(day) + "']";
+ browser.waitForElement(locator);
+ browser.click(locator);
+ }
+
+ @Override
+ public void setMonth(SeamSelenium browser, int month)
+ {
+ String monthLocator = "xpath=//td[@class='iceSelInpDateMonthYear'][2]";
+ String calMonth = browser.getText(monthLocator).trim();
+ int steps = monthStepsCount(calMonth, month);
+
+ movement(browser, "xpath=//td[@class='iceSelInpDateMonthYear'][1]/a", "xpath=//td[@class='iceSelInpDateMonthYear'][3]/a", steps);
+ }
+
+ @Override
+ public void setYear(SeamSelenium browser, int year)
+ {
+ String yearLocator = "xpath=//td[@class='iceSelInpDateMonthYear'][6]";
+ String calYear = browser.getText(yearLocator).trim();
+ int steps = yearStepsCount(calYear, year);
+
+ movement(browser, "xpath=//td[@class='iceSelInpDateMonthYear'][5]/a", "xpath=//td[@class='iceSelInpDateMonthYear'][7]/a", steps);
+ }
+
+ /**
+ * IceFaces forces partial submit, so we must wait for page reload
+ */
+ @Override
+ protected void click(SeamSelenium browser, String locator)
+ {
+ browser.clickAndWait(locator);
+ }
+
+ };
+
+ /**
+ * Selects date using Selenium browser
+ *
+ * @param browser
+ * Selenium browser instance
+ * @param date
+ * Date to be selected
+ */
+ public void setDate(SeamSelenium browser, Date date)
+ {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(date);
+
+ setYear(browser, cal.get(Calendar.YEAR));
+ setMonth(browser, cal.get(Calendar.MONTH));
+ setDay(browser, cal.get(Calendar.DAY_OF_MONTH));
+ }
+
+ /**
+ * Selects day in calendar
+ *
+ * @param browser
+ * Selenium browser instance
+ * @param day
+ * Integer containing the day to be selected, indexing from 1
+ */
+ protected abstract void setDay(SeamSelenium browser, int day);
+
+ /**
+ * Selects month in calendar
+ *
+ * @param browser
+ * Selenium browser instance
+ * @param month
+ * Integer containing the month to be selected, indexing from 1
+ */
+ protected abstract void setMonth(SeamSelenium browser, int month);
+
+ /**
+ * Selects year in calendar
+ *
+ * @param browser
+ * Selenium browser instance
+ * @param year
+ * Integer containing the year to be selected
+ */
+ protected abstract void setYear(SeamSelenium browser, int year);
+
+ /**
+ * Determines direction of month in calendar interface
+ *
+ * @param calMonth
+ * Which month is currently shown on calendar
+ * @param month
+ * Which month is desired to be set, indexing from 1
+ * @return Number of steps which must be done in either of direction, where
+ * sign has meaning:
+ * <ul>
+ * <li>- goes to past</li>
+ * <li>+ goes to future</li>
+ * </ul>
+ */
+ protected int monthStepsCount(String calMonth, int month)
+ {
+ final List<String> months = Arrays.asList(DateFormatSymbols.getInstance().getMonths());
+ int mindex = months.indexOf(calMonth);
+ if (mindex == -1)
+ throw new IllegalArgumentException("Unknown month: " + calMonth + " for locale: " + Locale.getDefault());
+
+ return month - (mindex + 1);
+ }
+
+ /**
+ * Determines direction of year in calendar interface
+ *
+ * @param calYear
+ * Which year is currently shown on calendar
+ * @param year
+ * Which month is desired to be set, indexing from 1
+ * @return Number of steps which must be done in either of direction, where
+ * sign has meaning:
+ * <ul>
+ * <li>- goes to past</li>
+ * <li>+ goes to future</li>
+ * </ul>
+ */
+ protected final int yearStepsCount(String calYear, int year)
+ {
+ int yindex;
+ try
+ {
+ yindex = Integer.valueOf(calYear);
+ }
+ catch (NumberFormatException nfe)
+ {
+ throw new IllegalArgumentException("Invalid year: " + calYear, nfe);
+ }
+
+ return year - yindex;
+ }
+
+ /**
+ * Moves in either backward or forward direction according to step count.
+ * Uses locator of element for both directions.
+ *
+ * @param browser
+ * Selenium browser instance
+ * @param backLocator
+ * Element which moves calendar to past
+ * @param forwardLocator
+ * Element which moves calendar to future
+ * @param steps
+ * Number of steps to be done, determined by monthStepsCount() or
+ * yearStepsCount() function
+ * @see SeleniumDateSelector#monthStepsCount(String, int)
+ * @see SeleniumDateSelector#yearStepsCount(String, int)
+ */
+ protected void movement(SeamSelenium browser, String backLocator, String forwardLocator, int steps)
+ {
+ // going to past
+ if (steps < 0)
+ {
+ for (int i = 0; i > steps; i--)
+ click(browser, backLocator);
+ }
+ // going to future
+ else
+ {
+ for (int i = 0; i < steps; i++)
+ click(browser, forwardLocator);
+ }
+ }
+
+ /**
+ * Clicks on element. Allow differentiate action according to framework, such
+ * as wait for page to load for IceFaces
+ *
+ * @param browser Selenium browser
+ * @param locator Locator of element to be clicked on
+ */
+ protected void click(SeamSelenium browser, String locator)
+ {
+ browser.click(locator);
+ }
+}
More information about the seam-commits
mailing list