【问题标题】:Doing some standard validation to web element before doing the actual action在执行实际操作之前对 Web 元素进行一些标准验证
【发布时间】:2019-10-25 18:47:36
【问题描述】:

正如标题所示,在实际操作之前,我正在对 Web 元素进行一些标准检查。检查元素是否显示和启用。我想将这两项检查分开,因为我想要一个失败的具体原因。感觉下面的代码太长了。

任何建议都将不胜感激。

Boolean isActionSuccess = false; 
        if (currentObject.isDisplayed()) {
            if (currentObject.isEnabled()) {

                // move to the object before clicking
                CommonFunctions.silentWait(1);
                actionToE.moveToElement(currentObject).perform();

                if (!actionPar.isEmpty()) {
                    // do something else
                } else {
                    currentObject.sendKeys(Keys.ARROW_UP);
                    isActionSuccess = true;
                }

            } else {
                System.out.println("Web Element is disabled!");
            }

        } else {
            System.out.println("Web Element is not displayed!");
        }

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    您最好的做法是将它们分成各自的小函数并返回布尔值。 喜欢

    Boolean isElementDisplayed(WebElement element){
        if (element.isDisplayed())
            return true;
        System.out.println(element + " is not displayed!");
        return false;
    }
    
    Boolean isElementEnabled(WebElement element){
        if (element.isEnabled())
            return true;
        System.out.println(element + " is not enabled!");
        return false;
    }
    

    但我也建议在执行 moveToElement 之后调用 isElementDisplayed,因为某些浏览器对“显示”的含义有不同的看法。

    您还可以使用 try catch 记录每个函数的异常。

    【讨论】:

    • 感谢@Naveen,您的评论让我的代码缩短了一点!
    【解决方案2】:
            Boolean isActionSuccess = false; 
            CommonFunctions.silentWait(1);
            actionToE.moveToElement(currentObject).perform();
    
            if (CommonFunctions.isElementDisplayed(currentObject)) {
                if (CommonFunctions.isElementEnabled(currentObject)) {
                    if (!actionPar.isEmpty()) {
                        // do something
                        }
                    } else {
                        currentObject.sendKeys(Keys.ARROW_LEFT);
                        isActionSuccess = true;
                    }
    
                }
            }
    

    【讨论】:

      【解决方案3】:

      在使用Selenium 执行Automated Tests 时,您不需要在实际操作之前需要对网络元素进行任何类型的额外标准检查。为了记录,每一行额外的代码都会产生额外的指令和指令周期。相反,您需要 optimize 您的代码/程序。

      如果您的用例是调用click()sendKeys(),则无需单独调用isDisplayed()isEnabled() 进行检查。相反,您需要使用WebDriverWaitExpectedConditions 结合使用来等待预定义的时间段(根据测试规范)。

      例子:

      • presenceOfElementLocated() 是检查页面 DOM 上是否存在元素的期望。这并不一定意味着该元素是可见的。

        new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("button.nsg-button")));
        
      • visibilityOfElementLocated() 期望检查一个元素是否存在于页面的 DOM 中并且可见。可见性是指元素不仅显示出来,而且高度和宽度都大于0。

        new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button.nsg-button")));
        
      • elementToBeClickable() 是检查元素是否可见并启用以便您可以单击它的期望。

        new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='nsg-button']"))).click();
        

      【讨论】:

      • 嗨@DebanjanB,我了解您来自哪里,但我正在创建自己的框架(关键字和数据驱动相结合),您的建议不适用。我只是想要一个建议来缩短执行相同操作的代码。
      • @Luvs1015 我同意您自己的框架(关键字和数据驱动相结合,我在回答中建议的是最佳实践,同时保持代码优化,这可能对未来的读者有所帮助.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 2010-12-26
      • 2016-12-13
      相关资源
      最近更新 更多