Gbru
Gbru

Reputation: 1087

A Robust Try Catch Method to use in WebDriver?

A Robust Try Catch Method to use in WebDriver?

  1. Can someone advice from there experiece whether the following method looks correct in the likely scenario where searching for an element gets timed out or the incorrect locator has been used?

  2. The timeout Exception dosnt seem to be printing my System.out.println after i set the wait to 2seconds and change the locator with the wrong xpath

My Code:

    public void clickSupercarsLink() throws Exception {
    try {
        this.wait.until(ExpectedConditions.elementToBeClickable(link_Supercars)).click();
    } catch (TimeoutException e) {
        System.out.println("UNABLE TO FIND ELEMENT : Timeout");
    } catch (Exception e) {
        System.out.println("UNABLE TO FIND ELEMENT : Exception");
        throw (e);
    }
}

New Code:

public void clickSupercarsLink() throws Exception {
    try {
        this.wait.until(ExpectedConditions.elementToBeClickable(link_Supercars)).click();
    } catch (TimeoutException e) {
        System.out.println("Timed out attempting to click on element: <" + link_Supercars.toString() + ">");
    } catch (Exception e) {
        System.out.println("Unable to click on element: " + "<" + link_Supercars.toString() + ">");
    }
}

Upvotes: 0

Views: 1086

Answers (1)

Mayur Shah
Mayur Shah

Reputation: 528

@Phil I would want you to throw that exception and handle it at high level. In current scenario, if there is a critical exception, your test will method calling your method clickSupercarsLink will not know that there was an exception.

Any way you are throwing exception, why do you have to catch it and do nothing with it then just printing!! This is not why you throw exception right?

public void clickSupercarsLink() throws Exception {
this.wait.until(ExpectedConditions.elementToBeClickable(link_Supercars)).click();
}

Upvotes: 1

Related Questions