0

Why dosnt my method catch my 'Timeout Exception' and print to console?

    public void clickDrivingExperienceButton() throws Exception {
    boolean test = this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled();
    try {
        if (test == true) {
            link_DrivingExperiences.click();
        }
        System.out.println("Successfully clicked on the driving experience button, using locator:  " + "<" + link_DrivingExperiences.toString() + ">");
    }catch (TimeoutException e) {
        System.out.println("WHY DONT I PRINT ANYTHING??????" + e.getMessage());
    }catch (Exception e) {
        System.out.println("Unable to click on the Driving Experience Button, Exception: " + e.getMessage());
    } finally {
        // final code here
    }
}

enter image description here

Gbru
  • 1,065
  • 3
  • 24
  • 56

3 Answers3

0

Most likely timeout exception throws out of this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled();, and your try-catch block doesn't enclose that line

Dmytro Grynets
  • 923
  • 11
  • 29
0

Put this.wait.until inside try block.

Exception message already tells that the exception occurred when it was waiting for the element to be clickable.

public void clickDrivingExperienceButton() throws Exception {

    try {
 boolean test = this.wait.until(ExpectedConditions.elementToBeClickable(link_DrivingExperiences)).isEnabled();
        if (test == true) {
            link_DrivingExperiences.click();
        }
        System.out.println("Successfully clicked on the driving experience button, using locator:  " + "<" + link_DrivingExperiences.toString() + ">");
    }catch (TimeoutException e) {
        System.out.println("WHY DONT I PRINT ANYTHING??????" + e.getMessage());
    }catch (Exception e) {
        System.out.println("Unable to click on the Driving Experience Button, Exception: " + e.getMessage());
    } finally {
        // final code here
    }
} 
Sumit Kumar
  • 375
  • 1
  • 11
  • if i do this for some reason the set time lets say 10seconds dosnt work – Gbru Feb 09 '17 at 16:51
  • Cross check your code . Putting statement inside try should not cause any issue . Now the exception will be caught and printed. – Sumit Kumar Feb 09 '17 at 17:05
0

Your try-catch isn't catching the exception because the exception comes from the 2nd line (wait.until) and that's not inside the try-catch.

You've got many of the same issues that I addressed in your other question, https://stackoverflow.com/a/42120129/2386774, that I would suggest that you fix up in this code also.


It should basically be the below

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

You shouldn't really need to log as much as you are logging. If the element is successfully clicked, the script will progress. Logging that is just going to clog up your logs, IMO. There's also no point in logging an exception because it's going to be dumped to the logs anyway. Generally, you want your script to stop when an exception is thrown unless proceeding won't be affected by it. That largely depends on your scenario so you will have to make the final decision on whether to proceed or not which will determine how you handle thrown exceptions.

Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • thanks again, in the following method you have listed, how would i alter the method, enabling it to proceed even when clicking on the method was not successfull? – Gbru Feb 10 '17 at 09:26
  • Throw a `try-catch` around the `wait.until()` and catch `TimeoutException`. – JeffC Feb 10 '17 at 14:16