2

I am developing web test scripts with Selenium WebDriver + Java, following the POM design pattern, and using Log4j2 for logging purposes.

Currently, my page methods are set up like so:

public void clickButton(){
   Log.debug("Clicking button...");
   btn.click();
   Log.info("Clicked button");
}

with a log line generated at the debug level before the action is performed, and an entry logged at the info level afterwards.

However, after thinking about it, I am not sure that there is much point to the debug log beforehand, because if an action were to fail, I would immediately be able to pinpoint the issue by the info logs even without going to the debug level. Am I gaining much by including it or am I safe to remove it?

3 Answers 3

5

Two things can go wrong in this scenario:

  1. Button is not found (and thus you would have NullPointerException on method click()) so it will break before with NoSuchElementException.
  2. Button is not clickable (for some reasons) - and then you will have ElementNotClickableException.

I'm not logging each step. I'm logging Exceptions on Framework level + doing screenshots on Exception or failing assertion).

So you are safe with removing it and not gaining much (you wouldn't analyze 10000 lines of logs to check if something was logged).

1

This approach might help you if something will hang in btn.click(). You will at least be aware of what step you had passed the latter before you went into issue.

Another thing is that when even the exception is thrown within btn.click() it is usually logged something like locator query that is not always very user friendly. So logging a friendly button identifier or any other valuable information before you start the action will help you to locate the proper place in your log file (especially if run tests in parallel)

1

How about bundling all logger & error handling inside action function itself??

This will reduce the code greatly and will centralize the logger and error handling.

public void clickButton(){
   actions.click(btnObject,'buttonName')
}

//actions helper class
  public void click(UIObject,objectName){
     //Logger info with objectName
     UIObject.click
     //error handling with object details
    }

And use composition to relate actions class to page classes instead of inheritance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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