0

As the title suggest, I'm doing some standard checking of an web element before the actual action. Check like if the element is displayed and enabled. I want to separate the the two checking because I want a specific reason why it fail. I feel the code below is too long.

Any suggestion would be appreciated.

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!");
        }
Luvs1015
  • 9
  • 6

3 Answers3

0

Your best course of action here will be to separate them into their own little functions and return boolean. Like

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;
}

But I will also suggest to call isElementDisplayed after moveToElement is performed because some browsers consider differently what "displayed" means.

You can also use try catch to log the exceptions per function.

Naveen
  • 770
  • 10
  • 22
0
        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;
                }

            }
        }
Luvs1015
  • 9
  • 6
0

While using Selenium to execute the Automated Tests you don't need any sort of additional standard checking of an web element before the actual action. For the record, each extra line of code will induce extra instructions and instruction cycles. Instead you need to optimize your code/program.

If your usecase is to invoke click() or sendKeys() you don't need to invoke isDisplayed() or isEnabled() separately to check. Instead you need to wait for a predefined time period (as per the Test Specification) using WebDriverWait inconjunction with ExpectedConditions.

Example:

  • presenceOfElementLocated() is an expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.

    new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("button.nsg-button")));
    
  • visibilityOfElementLocated() is an expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.

    new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button.nsg-button")));
    
  • elementToBeClickable() is an expectation for checking an element is visible and enabled such that you can click it.

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='nsg-button']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hi @DebanjanB,I understand where you are coming from but I'm creating my own framework(keyword and data driven combined) and your suggestion doesn't apply. I just want a suggestion to shorten my code that will perform the same action. – Luvs1015 Oct 25 '19 at 08:37
  • @Luvs1015 Where as I agree to your own framework(keyword and data driven combined, what I suggested in my answer are the best practices at the same time maintaining code optimization which may be helpful to the future readers. – undetected Selenium Oct 25 '19 at 10:45