1. 程式人生 > >【開源自動化測試疑難FAQ】【WebDriver】幾種型別的元件的判斷

【開源自動化測試疑難FAQ】【WebDriver】幾種型別的元件的判斷

第一組就是對網頁彈出的提示資訊的判斷,也就是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;
	}