[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");

       }

   }

转载于:https://www.cnblogs.com/feifeidxl/p/4450530.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值