1

I have a method that uses the Actions class to access a sub menu link. After hovering the main menu link I need to pause before clicking on the sub menu link. So I added a thread.sleep(). At this point the IDE throws an exception with 2 quick fixes as a suggestion:

"Add throws exception" or "surround with try catch block".

Which one should I use?

Here is the method:

public void goToAddNewPost() {
    Actions actions = new Actions(driver);      
    WebElement menuHoverLink = driver.findElement(posts);   
    actions.moveToElement(menuHoverLink).build().perform();
    Thread.sleep(500);
    WebElement subLink = driver.findElement(addNew);        
    actions.moveToElement(subLink).click().build().perform();       
}
  • Use "surround with try catch block" as that is standard practice. – Helping Hands Nov 24 '15 at 12:46
1

I would suggest you ignore what the IDE recommended (which is likely aimed at Java as opposed to Selenium users). As a general rule, test suites don't want to handle exceptions. Each test should pass/fail on a single criteria. As such, if you think something might break, write a test for it. So, instead of handling exceptions, use an Assert to determine if the element(s) are present or not (with an appropriate failure message).

AsserTrue(driver.findElement(addNew), "addNew element not present");

This approach will prove invaluable when you run the tests as part of a suite. You want the test to fail, and give you its reason for doing so, without having to look at the log files to establish the cause. It will also help intergrate with CI tools like Jenkins or Bamboo

I would also recommend removing your Thread.Sleep() and instead replace it with an implicit wait

  • 1. If I remove Thread.Sleep() then the IDE will not prompt for exception handling. So then do I need exception handling at all? 2. I tried above code at it does't work. Is this correct? Assert.assertTrue(subLink.isDisplayed(), "addNew element not present"); – ilm Nov 25 '15 at 9:09
  • 1
    As a general rule, test suites don't want to handle exceptions. Each test should pass/fail. As such, if you think something might break, write a test for it. The syntax for asserts will vary depending on if you use TestNG or JUnit. Take my example more as guidance than a copy/paste into your code. – ECiurleo Nov 25 '15 at 9:15
  • 1
    implicit wait ++ – Ethel Evans Nov 25 '15 at 21:35
0

I would go by Helping Hands answer, and wrap this in between a try-catch block to catch an exception.

public void goToAddNewPost() {

try{

Actions actions = new Actions(driver);      
WebElement menuHoverLink = driver.findElement(posts);   
actions.moveToElement(menuHoverLink).build().perform();
Thread.sleep(500);
WebElement subLink = driver.findElement(addNew);        
actions.moveToElement(subLink).click().build().perform();

}catch(NoSucElementException e){
     System.out.println("No such element is found")
}catch(TimeoutException e1){
     System.out.println("Timed out but no element found")
}
}

This should be the probable code , which should give you an idea. Here is a good read.

Secondly, I would recommend you use a Wait condition in your tests, instead of the Thread.sleep( ), that you are currently using

  • Catching exceptions in tests is often bad practice. This often results in the test failing further away from the actual issue, which makes tracking the root cause difficult. Generally, the test should fail as fast as possible, so the tester doesn't have to read the logs to figure out why the assert one or more functions later failed; it should fail right here. – Ethel Evans Nov 25 '15 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.