namjae / robotium

Automatically exported from code.google.com/p/robotium
0 stars 0 forks source link

Timeout parameter for the searchText() function #77

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I think the possibility of providing a timeout parameter for the searchText 
function could be pretty useful. Right now it is using a timeout constant of 5 
seconds. The code that needs to be added or modified would be:

In file Solo.java:

/**
     * Searches for a text string and returns {@code true} if at least one item
     * is found with the expected text. Will automatically scroll when needed. 
     *
     * @param text the text to search for. The parameter will be interpreted as a regular expression
         * @param timeout the time the function is going to wait when looking for the text expressed in miliseconds
     * @return {@code true} if the search string is found and {@code false} if it is not found
     *
     */

    public boolean searchText(String text, int timeout) {
        boolean found = searcher.searchWithTimeoutFor(TextView.class, text, 0, true, false, timeout);
        return found;
    }

In file Searcher.java:

    /**
     * Searches for a {@code View} with the given regex string and returns {@code true} if the
     * searched {@code Button} is found a given number of times. Will automatically scroll when needed.
     *
     * @param viewClass what kind of {@code View} to search for, e.g. {@code Button.class} or {@code TextView.class}
     * @param regex the text to search for. The parameter <strong>will</strong> be interpreted as a regular expression.
     * @param expectedMinimumNumberOfMatches the minimum number of matches expected to be found. {@code 0} matches means that one or more
     * matches are expected to be found
     * @param scroll whether scrolling should be performed
     * @param onlyVisible {@code true} if only texts visible on the screen should be searched
         * @param timeout the time the function is going to wait when looking for the text expressed in miliseconds
     * @return {@code true} if a {@code View} of the specified class with the given text is found a given number of
     * times, and {@code false} if it is not found
     *
     */

    public boolean searchWithTimeoutFor(Class<? extends TextView> viewClass, String regex, int expectedMinimumNumberOfMatches, boolean scroll, boolean onlyVisible, int timeout) {
        final long endTime = System.currentTimeMillis() + timeout;

        while (System.currentTimeMillis() < endTime) {
            sleeper.sleep();
            final boolean foundAnyMatchingView = searchFor(viewClass, regex, expectedMinimumNumberOfMatches, scroll, onlyVisible);
            if (foundAnyMatchingView){
                return true;
            }
        }

        return false;
    }

Original issue reported on code.google.com by jesuscas...@gmail.com on 25 Feb 2011 at 11:35

GoogleCodeExporter commented 9 years ago
Hi,

Please use waitForText() instead. Same behaviour as searchText() but with the 
possiblity of setting timeout. 

/Renas

Original comment by renasr...@gmail.com on 25 Feb 2011 at 11:43