I’ve previously put up code that can be used in RFT to wait for data to appear in a table. What follows is a similar method, but instead of waiting for data, it waits for an exact number of rows to be present in a table. It’s useful if you are expecting a row to disappear from a table after processing the row, or something like that…
To use the method, paste it into whatever class deals with tables (a class called something like “TableManager”), and then call the method statically passing in the table and the expected number of rows as parameters.
E.g.
TableManager.waitForParticularNumberOfRows(tblAccounts(), 4);
That line of code will make the test wait for the default waiting time (same as is used for waitForExistence), polling the tblAccounts table to see if it has 4 rows yet. If it does, the script continues. If by the end of the waiting period the row count still isn’t 4, an exception is thrown (again, just like waitForExistence).
/*
* Copyright (c) 2006, Nathaniel Ritmeyer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* – Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
* – Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
* – Neither the name of natontesting.com nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* www.natontesting.com
*/
public static void waitForParticularNumberOfRows(GuiSubitemTestObject tblTargetTable, int iNumberOfRows) throws Exception
{
//get the waitForExistence values
Double dMaxWaitTime = (Double)getOption(IOptionName.MAXIMUM_WAIT_FOR_EXISTENCE);
Double dTimeBtwnRetry = (Double)getOption(IOptionName.WAIT_FOR_EXISTENCE_DELAY_BETWEEN_RETRIES);
int iMaxWaitForExistenceTime = dMaxWaitTime.intValue();
int iTimeToWaitBetweenRetries = dTimeBtwnRetry.intValue();
//work out how many times we’re going to check
int iNumberOfTimesToCheck = iMaxWaitForExistenceTime / iTimeToWaitBetweenRetries;
//loop iNumberOfTimesToCheck number of times checking for a particular
//number of rows
for(int iCheckCounter = 0; iCheckCounter < iNumberOfTimesToCheck; iCheckCounter++)
{
//get the contents of the table
ITestDataTable itdt = (ITestDataTable)tblTargetTable.getTestData(“contents”);
//if the number of rows doesn’t match what you want…
if(itdt.getRowCount() != iNumberOfRows)
{
//…sleep…
sleep(iTimeToWaitBetweenRetries);
}
else
{
//If we get here it’s because the table contains the particular
//number of rows that you’re after…
//It’s time to escape!
return;
}
}
throw new Exception(“Timed out waiting for correct number of rows”);
}
As always, any improvements are welcome!