一路查找最终的实现的代码乳腺
/**
* Searches for a {@code View} with the given regex string and returns {@code true} if the* searched {@code View} is found a given number of times.
*
* @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 timeout the amount of time in milliseconds to wait //设置搜索的等待时间,当时间结束后停止搜索
* @param scroll whether scrolling should be performed //滑动滚动条查找
* @param onlyVisible {@code true} if only texts visible on the screen should be searched //是否只查找页面显示的关键字
*
* @return {@code true} if a view of the specified class with the given text is found a given number of times.
* {@code false} if it is not found.
*/
public <T extends TextView> T searchFor(final Class<T> viewClass, final String regex, int expectedMinimumNumberOfMatches, final long timeout, final boolean scroll, final boolean onlyVisible) {
//默认搜索个数为1
if(expectedMinimumNumberOfMatches < 1) {expectedMinimumNumberOfMatches = 1;
}
//Callable为定义的接口
final Callable<Collection<T>> viewFetcherCallback = new Callable<Collection<T>>() {
@SuppressWarnings("unchecked")
public Collection<T> call() throws Exception {
sleeper.sleep();
/**
* isAssignableFrom 是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的超类或接口。
*通常调用格式是
*Class1.isAssignableFrom(Class2)
*/
ArrayList<T> viewsToReturn = viewFetcher.getCurrentViews(viewClass);
//OnlyVisible为true时移除搜索的InvisibleView对象
if(onlyVisible){
viewsToReturn = RobotiumUtils.removeInvisibleViews(viewsToReturn);
}
//返回结果
if(viewClass.isAssignableFrom(TextView.class)) {
viewsToReturn.addAll((Collection<? extends T>) webUtils.getTextViewsFromWebView());
}
return viewsToReturn;
}
};
try {
return searchFor(viewFetcherCallback, regex, expectedMinimumNumberOfMatches, timeout, scroll);
} catch (Exception e) {
throw new RuntimeException(e);
}
}