public static WebDriverWait createWait(WebDriver driver) { return new WebDriverWait(driver, Environment.getTimeOutInSeconds(),Environment.getSleepInMillis()); } -------------------------------------------------------------------------------- // <1> Wait for element present in Dom, may not visible /** * Wait for element present in DOM, may not visible * @param driver * @param locator * @param errorMessage * @return */ 1). public static WebElement waitForElementPresent(WebDriver driver,final By locator, String errorMessage) { Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() { @Override public WebElement apply(WebDriver driver) { return driver.findElement(locator); } }; WebDriverWait wait = createWait(driver); if (errorMessage != "") { wait.withMessage(errorMessage); // withMessage() 为 WebDriverWait 类的方法 } else { wait.withMessage("Cannot find Element: " + locator.toString()); } return wait.until(waitFn); } 2). public static WebElement waitForElementPresent(WebDriver driver, final By locator) { return waitForElementPresent(driver, locator, ""); } -------------------------------------------------------------------------------------- <2> Wait for all elements present in Dom, may not visisble /** * Wait for elements present in DOM, may not visible * @param driver * @param locator * @param errorMessage * @return */ 1). public static List<WebElement> waitForAllElementsPresent(WebDriver driver,final By locator, String errorMessage{ Function<WebDriver, List<WebElement>> waitFn = new Function<WebDriver, List<WebElement>>() { @Override public List<WebElement> apply(WebDriver driver) { return driver.findElements(locator); } }; WebDriverWait wait = createWait(driver); if (errorMessage != "") { wait.withMessage(errorMessage); } else { wait.withMessage("Cannot find Elements: " + locator.toString()); } return wait.until(waitFn); } 2). public static List<WebElement> waitForAllElementsPresent(WebDriver driver, final By locator) { return waitForAllElementsPresent(driver, locator, ""); } -------------------------------------------------------------------------------------- <3> Wait for context element present in DOM, may not visible /** * Finding element from the context, then wait for element present in DOM, may not visible * @param driver * @param locator * @param context * @param errorMessage * @return */ 1). public static WebElement waitForElementPresent(WebDriver driver,final By locator, final WebElement context, String errorMessage) { Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() { @Override public WebElement apply(WebDriver driver) { return context.findElement(locator); } }; WebDriverWait wait = createWait(driver); if (errorMessage != "") { wait.withMessage(errorMessage); } else { wait.withMessage("Cannot find Element: " + locator.toString()); } return wait.until(waitFn); } 2). public static WebElement waitForElementPresent(WebDriver driver,final By locator, final WebElement context) { return waitForElementPresent(driver, locator, context, ""); } ------------------------------------------------------------------------- <4> Wait for all the target context elements present in DOM, may not visible /** * Finding elements from the context, then wait for all the elements present in DOM, may not visible * @param driver * @param locator * @param context * @param errorMessage * @return */ 1). public static List<WebElement> waitForAllElementsPresent(WebDriver driver, final By locator, final WebElement context, String errorMessage) { Function<WebDriver, List<WebElement>> waitFn = new Function<WebDriver, List<WebElement>>() { @Override public List<WebElement> apply(WebDriver driver) { try { List<WebElement> els = context.findElements(locator); if (els.size() > 0) { return els; } return null; } catch (Exception e) { } return null; } }; WebDriverWait wait = createWait(driver); if (errorMessage != "") { wait.withMessage(errorMessage); } else { wait.withMessage("Cannot find Elements: " + locator.toString()); } return wait.until(waitFn); } 2). public static List<WebElement> waitForAllElementsPresent(WebDriver driver, final By locator, final WebElement context) { return waitForAllElementsPresent(driver, locator, context, ""); } -------------------------------------------------------------------------------- <5> Wait for element visible /** * Wait for element visible * * @param driver * @param locator * @param errorMessage * @return */ 1). public static WebElement waitForElementVisible(WebDriver driver, final By locator, String errorMessage) { Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() { @Override public WebElement apply(WebDriver driver) { try { WebElement el = driver.findElement(locator); if (el.isDisplayed()) { return el; } } catch (Exception e) { } return null; } }; WebDriverWait wait = createWait(driver); if (errorMessage != "") { wait.withMessage(errorMessage); } else { wait.withMessage("Cannot find Element: " + locator.toString()); } return wait.until(waitFn); } 2). public static WebElement waitForElementVisible(WebDriver driver,final By locator) { return waitForElementVisible(driver, locator, ""); } -------------------------------------------------------------------------------- <6> Wait for context element visible /** * Finding element from the context, wait for element visible * @param driver * @param locator * @param context * @param errorMessage * @return */ 1). public static WebElement waitForElementVisible(WebDriver driver, final By locator, final WebElement context, String errorMessage) { Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() { @Override public WebElement apply(WebDriver driver) { try { WebElement el = context.findElement(locator); if (el.isDisplayed()) { return el; } } catch (Exception e) { } return null; } }; WebDriverWait wait = createWait(driver); if (errorMessage != "") { wait.withMessage(errorMessage); } else { wait.withMessage("Cannot find Element: " + locator.toString()); } return wait.until(waitFn); } 2). public static WebElement waitForElementVisible(WebDriver driver, final By locator, final WebElement context) { return waitForElementVisible(driver, locator, context, ""); } --------------------------------------------------------------------------------------------------------- <7> Wait for all the elements visible /** * Wait for all the elements visible * * @param driver * @param locator * @param errorMessage * @return */ 1). public static List<WebElement> waitForAllElementsVisible(WebDriver driver, final By locator,String errorMessage{ Function<WebDriver, List<WebElement>> waitFn = new Function<WebDriver, List<WebElement>>() { @Override public List<WebElement> apply(WebDriver driver) { try { List<WebElement> els = driver.findElements(locator); int count = 0; for (WebElement el : els) { if (el.isDisplayed()) { count++; } } if (count == els.size() && els.size() > 0) { return els; } } catch (Exception e) { } return null; } }; WebDriverWait wait = createWait(driver); if (errorMessage != "") { wait.withMessage(errorMessage); } else { wait.withMessage("Cannot find Elements: " + locator.toString()); } return wait.until(waitFn); } 2). public static List<WebElement> waitForAllElementsVisible(WebDriver driver, final By locator) { return waitForAllElementsVisible(driver,locator,""); } ----------------------------------------------------------------------------------- <8> Wait for all the target context elements visible /** * Finding elements from the context, then wait for all the elements present visible * @param driver * @param locator * @param context * @param errorMessage * @return */ 1). public static List<WebElement> waitForAllElementsVisible(WebDriver driver, final By locator, final WebElement context,String errorMessage) { Function<WebDriver, List<WebElement>> waitFn = new Function<WebDriver, List<WebElement>>() { @Override public List<WebElement> apply(WebDriver driver) { try { List<WebElement> els = context.findElements(locator); int count = 0; for (WebElement el : els) { if (el.isDisplayed()) { count++; } } if (count == els.size() && els.size() > 0) { return els; } } catch (Exception e) { } return null; } }; WebDriverWait wait = createWait(driver); if (errorMessage != "") { wait.withMessage(errorMessage); } else { wait.withMessage("Cannot find Elements: " + locator.toString()); } return wait.until(waitFn); } 2). public static List<WebElement> waitForAllElementsVisible(WebDriver driver, final By locator, final WebElement context){ return waitForAllElementsVisible(driver,locator,context,""); } ---------------------------------------------------------------------------- 1). public static WebElement waitForElementVisible(WebDriver driver, final By locator, final WebElement context, int seconds, String errorMessage) { Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() { @Override public WebElement apply(WebDriver driver) { try { WebElement el = context.findElement(locator); if (el.isDisplayed()) { return el; } } catch (Exception e) { } return null; } }; WebDriverWait wait = createWait(driver); if (errorMessage != "") { wait.withMessage(errorMessage); } else { wait.withMessage("Cannot find Element: " + locator.toString()); } return wait.until(waitFn); } 2). public static WebElement waitForElementVisible(WebDriver driver,final By locator, final WebElement context, int seconds) { return waitForElementVisible(driver, locator, context, seconds, ""); } --------------------------------------------------------------------------------- 1)./** * Check if element exists in DOM, maybe not visible * @param driver * @param locator * @return */ public static Boolean isElementPresent(WebDriver driver, By locator) { try { driver.findElement(locator); return true; } catch (Exception e) { return false; } } 2)./** * Check if element exists from context node, maybe not visible * @param context * @param locator * @return */ public static Boolean isElementPresent(WebElement context, By locator) { try { context.findElement(locator); return true; } catch (Exception e) { return false; } } 3). /** * Check if element is visible in DOM * @param driver * @param locator * @return */ public static Boolean isElementVisible(WebDriver driver, By locator) { try { WebElement el = driver.findElement(locator); if(el.isDisplayed()){ return true; } else{ return false; } } catch (Exception e) { return false; } } 4). /** * Check if element is visible from context node * @param context * @param locator * @return */ public static Boolean isElementVisible(WebElement context, By locator) { try { WebElement el = context.findElement(locator); if(el.isDisplayed()){ return true; } else{ return false; } } catch (Exception e) { return false; } }