How can I check if a non-input text element is clickable in Selenium? (Java)

Pari :

I'm trying to check the clickability of a non-input text element (which can only be viewed but not edited). I have a test that where I want to assert that the view only text element (Ex: First Name) displayed on page can not be clicked.

I have tried using the isEnabled() method to check if the view only text element is enabled or not but the assertion is not happening correctly.

This is Bobcat Selenium code

Step definition code:

@Then("^I should verify that the First Name is not clickable$")
    public void iShouldVerifyThatTheFirstNameIsNotClickable() {
        assertEquals("Error: First Name is clickable", true, 
fullName.verifyClick());
}

Page Object code:

public boolean verifyClick() {
        if (firstName.isEnabled()) {
            return true;
        }
        else {
                return false;
        }
}

Expected result: Since firstName is a view only element, the result of the verifyClick() method should be false so my @Then("^I should verify that the First Name is not clickable$") result should fail since the assertion is failing.

Actual result: @Then("^I should verify that the First Name is not clickable$") result is success.

C. Peck :

There is a part of the selenium Java bindings that could be useful to you here. In ExpectedConditions you'll find a function called elementToBeClickable(). This returns a boolean that's false whenever the element is not clickable for any reason, and true when it can receive a click. So you just want to wait and see if that function returns true. Selenium handles that as well with the WebDriverWait class.

So you'll need to import both of those, and then you can do something like this:

//setting the timeout for our wait to be 20 seconds (you can use whatever you want)
WebDriverWait myWaitVar = new WebDriverWait(driver,20); 
try {
    WebElement myElement = myWaitVar.until(ExpectedConditions.elementToBeClickable(firstName)));
    //assert test failed!
}
catch(timeoutException timeout) {
    //whatever you want to do when the element is not clickable
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=158669&siteId=1