[Selenium] common functions comparison

1.Wait for element  in default time or self defined time

When the element need some time to be present , be visible, be not present or be not visible, for example : loading icon, waiting time is very import to get the element.

*
*SeleniumUtil.waitForElementNotVisible(driver, By.cssSelector("div#window-waiting-box"),"Waiting box should disppear in default time which is configured as timeOutInSeconds in environment.xml");

 

*SeleniumUtil.waitForElementNotVisible(driver, By.cssSelector("div#window-waiting-box"), 120 ,"Waiting box should disappear in 120s");
 

2.Explicit wait

(1)  new WebDriverWait(driver, 10). until(ExpectedConditions.elementToBeClickable(locator));

 

(2)  new WebDriverWait(driver, 10). until(ExpectedConditions.visibilityOf(locator));

 

(3)  new WebDriverWait(driver, 10). until(ExpectedConditions.presenceOfElementLocated(locator);

 

(4)

       Function<WebDriver, WebElement> waitFn = new Function<WebDriver, WebElement>() {

            @Override

            public WebElement apply(WebDriver driver) {

                return el.findElement(By.cssSelector("div.rptstatus.rptcomplete"));

            }

        };

        //Detect every 2 seconds,  the maximum time  is 120 seconds

       WebDriverWait wait = new WebDriverWait(driver, 120, 2);

       wait.withMessage("A processing icon should display in the Status column in the row.”)

       wait.until(waitFn);

 

3.waitForElementVisible    VS  waitForElementPresent

*SeleniumUtil.waitForElementVisible(driver, By.cssSelector("input#btnClose"), "Close button exists but not visible")
*
*SeleniumUtil.waitForElementPresent(driver, By.cssSelector("input#btnClose"), "Close button doesn’t exist")
 
4.waitForElementPresent   VS   waitForAllElementsPresent
*WebElement element = SeleniumUtil.waitForElementPresent(driver, By.cssSelector("input[value='"+buttonName+"']"),"Cannot get button named: "+buttonName);
*
*List<WebElement> elementList = SeleniumUtil.waitForAllElementsPresent(driver, By.cssSelector("div.rtq-grid-row[rowid]"),"Cannot get folders list");
 

5.Some element might be Present/Visible, it also might not. But both conditions are correct.

For example : alert dialog

In this condition, we need to use try{ ...} catch()

 

 6.Find element under another element

 

permissionEl.findElement(By.cssSelector("input[value='true']"))

 

SeleniumUtil.waitForElementVisible(driver, By.cssSelector(".top-bottom-split"), workSpaceEl);

public void catchIfPopUpDialogAndClickClose(){

     try{

             SeleniumUtil.waitForElementVisible(driver, By.cssSelector("input#btnClose")).click();

               logger.info("Dialog pops up");

      }

      catch(Exception e)

      {

               logger.info("No dialog pops up");

       }

   }

posted @ 2015-04-23 14:48  *飞飞*  阅读(179)  评论(0编辑  收藏  举报