【开源自动化测试疑难FAQ】【WebDriver】几种类型的组件的判断

本文介绍了使用Selenium进行元素、对话框及浏览器窗口存在性的判断方法,包括直接判断及在指定时间内等待出现的判断方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       玩过QTP的人都知道Object.Exist方法,而在selenium2里面我们如何实现类似的判断功能呢?下面简单提供几个方法,注意,相关的被引用的对象的声明请参见http://blog.youkuaiyun.com/fudax/article/details/7879910http://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;
	}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值