Skip to content

Being able to test an element exists without relying on exceptions #22

Open
@vincent-fuchs

Description

@vincent-fuchs

Hi,

Regularly in my application, I need to click on one element if it exists, or another if it doesn't. The problem I have is that there's no method returning a boolean doing that. So I'm writing code like this which throws a quite generic AssertionError (for which I don't know if it failed because it doesn't exist or because element was not clickable), forcing me to write code like this :

private WorkFlowPage clickWorkFlowTab() {

    try{
        find(By.id("someElement")).click();
    }catch(AssertionError e)
    {
        find(By.id("someOtherElement")).click();
    }
    return this;
}

Since it returns a LazyDomElement, find method doesn't perform the search immediately, as mentioned in documentation. Is there a way to force the find to happen, so that I can test if DomElement is null or not, then proceed. Something similar to findEagerly below :

private WorkFlowPage clickWorkFlowTab() {

    DomElement workflowTab=findEagerly(By.id("someElement"));

    if(workflowTab!=null){
        workflowTab.click();
    }
    else {
        find(By.id("someOtherElement")).click();
    }

    return this;
}

One alternative could be to throw a specific exception if the element is not found, to identify clearly these cases, and leave the AssertionError for real assertion issues based on should().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions