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

           }

       }

  • 相关阅读:
    python 字典
    python set集合
    python封装和解构
    python 内置数据结构 切片
    CPU 上下文切换及案例分析
    怎么理解linux的平均负载及平均负载高后的排查工具
    Linux性能优化
    python 内置数据结构 字符串
    python内置数据结构
    python GC、分支、循环
  • 原文地址:https://www.cnblogs.com/feifeidxl/p/4450530.html
Copyright © 2020-2023  润新知