玩过QTP的人都知道Object.Exist方法,而在selenium2里面我们如何实现类似的判断功能呢?下面简单提供几个方法,注意,相关的被引用的对象的声明请参见http://blog.youkuaiyun.com/fudax/article/details/7879910和http://blog.youkuaiyun.com/fudax/article/details/7879915这两个完整的工具类。
第一组就是对网页弹出的提示信息的判断,也就是Dialog,对于等待时间的加载,这里也可以用org.openqa.selenium.support.ui.WebDriverWait去实现,不过我这里还是给出简单的自定义的循环查找。
/**
* judge if the alert is existing.
*
* @throws RuntimeException
*/
protected boolean alertExists() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException ne) {
warn("no alert is present now...");
} catch (Exception e) {
LOG.error(e);
throw new RuntimeException(e.getMessage());
}
return false;
}
/**
* judge if the alert is present in specified seconds.
*
* @param seconds timeout in seconds
* @throws RuntimeException
*/
protected boolean alertExists(int seconds) {
long start = System.currentTimeMillis();
while ((System.currentTimeMillis() - start) < seconds * 1000) {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException ne) {
pause(sleepUnit);
} catch (Exception e) {
LOG.error(e);
throw new RuntimeException(e.getMessage());
}
}
return false;
}
第二组是对指定页面对象的判断,方法极为简单。这里由于WebDriver.findElements是没有抛出Exception的,所以没有再去单独处理:
/**
* judge if the element is existing.
*
* @param by the element locator By
*/
protected boolean elementExists(By by) {
return (driver.findElements(by).size() > 0) ? true : false;
}
/**
* judge if the element is present in specified seconds.
*
* @param by the element locator By
* @param seconds timeout in seconds
*/
protected boolean elementExists(final By by, int seconds) {
long start = System.currentTimeMillis();
boolean exists = false;
while (!exists && ((System.currentTimeMillis() - start) < seconds * 1000)) {
exists = elementExists(by);
}
return exists;
}
第三组是对网页窗口是否存在的判断,分两种:一种是指定窗口标题去判断的方法,另一种是针对于那种没有标题的窗口进行判断的方法:
/**
* judge if the browser is existing, using part of the page title.
*
* @param browserTitle part of the title to see if browser exists
* @throws RuntimeException
*/
protected boolean browserExists(String browserTitle) {
String defaultHandler = driver.getWindowHandle();
try {
Set<String> windowHandles = driver.getWindowHandles();
for (String handler : windowHandles) {
driver.switchTo().window(handler);
String currentTitle = driver.getTitle();
if (currentTitle.contains(browserTitle)){
return true;
}
}
} catch (WebDriverException e) {
LOG.error(e);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
driver.switchTo().window(defaultHandler);
}
return false;
}
/**
* judge if the browser is present by title reg pattern in specified seconds.
*
* @param browserTitle part of the title to see if browser exists
* @param seconds timeout in seconds
* @throws RuntimeException
*/
protected boolean browserExists(String browserTitle, int seconds) {
long start = System.currentTimeMillis();
boolean isExist = false;
while (!isExist && (System.currentTimeMillis() - start) < seconds * 1000) {
isExist = browserExists(browserTitle);
}
return isExist;
}
/**
* wait for new window which has no title in few seconds.
*
* @param browserCount windows count before new window appears.
* @param seconds time unit in seconds.
*/
protected boolean isNewWindowExits(int browserCount, int seconds) {
boolean isExist = false;
long begins = System.currentTimeMillis();
while ((System.currentTimeMillis() - begins < seconds * 1000) && !isExist) {
isExist = (driver.getWindowHandles().size() > browserCount) ? true : false;
}
return isExist;
}
/**
* wait for new window which has no title in few seconds.
*
* @param oldHandlers windows handler Set before new window appears.
* @param seconds time unit in seconds.
*/
protected boolean isNewWindowExits(Set<String> oldHandlers, int seconds) {
boolean isExist = false;
long begins = System.currentTimeMillis();
while ((System.currentTimeMillis() - begins < seconds * 1000) && !isExist) {
isExist = (driver.getWindowHandles().size() > oldHandlers.size()) ? true : false;
}
return isExist;
}