001: // Copyright © 2006-2007 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.plugins.exceltest;
003:
004: import com.canoo.webtest.engine.StepFailedException;
005: import org.apache.poi.hssf.util.CellReference;
006: import org.apache.poi.hssf.usermodel.HSSFSheet;
007: import org.apache.poi.hssf.usermodel.HSSFCell;
008:
009: /**
010: * @author Rob Nielsen
011: * @webtest.step category="Excel"
012: * name="excelFindRow"
013: * alias="findRow"
014: * description="This step allows you to find a specific row on a sheet. This is useful for using with repeat when you don't know how many rows there will be on a table. It starts from a specified cell and searches down for a row matching the given text in an Excel spreadsheet and saves the resulting row as a property."
015: */
016: public class ExcelFindRow extends AbstractExcelSheetStep {
017: private String fText;
018: private String fPropertyName;
019: private String fPropertyType;
020: private String fStartRow = "1";
021: private String fCol;
022:
023: /**
024: * @param index The row to start from
025: * @webtest.parameter required="no"
026: * default="1"
027: * description="The row to start searching from"
028: */
029: public void setStartRow(final String index) {
030: fStartRow = index;
031: }
032:
033: public String getStartRow() {
034: return fStartRow;
035: }
036:
037: /**
038: * @param index The index of the sheet to select
039: * @webtest.parameter required="no"
040: * description="The column reference (eg. 'B' or '2') to search in."
041: */
042: public void setCol(final String index) {
043: fCol = index;
044: }
045:
046: public String getCol() {
047: return fCol;
048: }
049:
050: /**
051: * @param name The Property Name
052: * @webtest.parameter required="yes"
053: * description="The name of the property in which to store the value."
054: */
055: public void setProperty(final String name) {
056: fPropertyName = name;
057: }
058:
059: public String getProperty() {
060: return fPropertyName;
061: }
062:
063: /**
064: * Sets the Type of the Property.<p>
065: *
066: * @param type The Property type
067: * @webtest.parameter required="no"
068: * description="The type of the property in which to store the value. Either \"ant\" or \"dynamic\"."
069: * default="the \"defaultPropertyType\" as specified in the \"config\" element is used."
070: */
071: public void setPropertyType(final String type) {
072: fPropertyType = type;
073: }
074:
075: public String getPropertyType() {
076: return fPropertyType;
077: }
078:
079: public String getText() {
080: return fText;
081: }
082:
083: /**
084: * @param text
085: * @webtest.parameter
086: * required="yes"
087: * description="The text value to search for. Will be treated as a regex if surrounded with '/'s (eg '/test\d+/')"
088: */
089: public void setText(final String text) {
090: fText = text;
091: }
092:
093: protected boolean verifyText(final String actualValue) {
094: return verifyStrings(getText(), actualValue);
095: }
096:
097: protected void verifyParameters() {
098: super .verifyParameters();
099: nullParamCheck(getText(), "text");
100: nullParamCheck(getProperty(), "property");
101: nullParamCheck(getCol(), "col");
102: optionalIntegerParamCheck(getStartRow(), "startRow", true);
103: }
104:
105: public void doExecute() {
106: final CellReference cellReference = ExcelCellUtils
107: .getCellReference(this , null, getStartRow(), getCol());
108: final HSSFSheet excelSheet = getExcelSheet();
109: int row = cellReference.getRow();
110: while (row <= excelSheet.getLastRowNum()) {
111: final HSSFCell excelCellAt = ExcelCellUtils.getExcelCellAt(
112: this , row, cellReference.getCol());
113: if (verifyText(ExcelCellUtils.getCellValueAt(excelCellAt))) {
114: setWebtestProperty(getProperty(), String
115: .valueOf(row + 1), getPropertyType());
116: return;
117: }
118: row++;
119: }
120: throw new StepFailedException("No cells were found matching '"
121: + getText() + "' starting from " + cellReference, this);
122: }
123: }
|