Log the 'Test Name' in the log4j

453 views
Skip to first unread message

SeleniumMember

unread,
Oct 5, 2015, 5:07:01 PM10/5/15
to Selenium Users
I am running Selenium WebDriver Junit tests. I am trying to generate the log files using log4j. I have the logs generated and want to make it more meaningful by logging the Test Name and some other helpful info. I have no clue what I need add to get the Test Name logged for Debug purpose in the event the test fails.

So, far this is what I have...

Thanks

#Root logger
log4j.rootLogger=DEBUG, INFO, ERROR, WARN, file

#Pattern to output the caller's file name and line number.
log4j.appender.file.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\src\\Selenium.logs
log4j.appender.file.maxFileSize=1MB

#Keep backup file
log4j.appender.file.maxBackupIndex=5

log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss} %-5p %c{1}:%L -%m%n
log4j.appender.file.layout.PatternLayout %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} \u2013 %msg%n
log4j.appender.file.Append=false

Krishnan Mahadevan

unread,
Oct 7, 2015, 12:33:41 AM10/7/15
to Selenium Users
Anjana,

This is more of a log4j query than a Selenium question. 

But I still don't know how does one extract the JUnit Test names. 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/6fd0dc92-f023-4dd7-b189-2898977e5686%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Collin

unread,
Oct 7, 2015, 5:24:09 AM10/7/15
to Selenium Users
Something like this?

@BeforeMethod
public void prepareTest(Method method) throws {
LOGGER.info("Starting test " + method.getName() + "... ");
}

SeleniumMember

unread,
Oct 8, 2015, 12:19:36 PM10/8/15
to Selenium Users
Thank you both for the reply. Reply didn't show up until this morning for some reason.

@Mark, As you suggested, I tried the following. I thought of adding the similar line to all the Tests. I hope this is okay to do.

@Before
    public void openBrowser() {
        log.info("Starting to execute test method TestCreateXYZ");
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://127.0.0.1/");

Mark Collin

unread,
Oct 9, 2015, 4:00:00 AM10/9/15
to Selenium Users
What I suggested was a TestNG way to get the name of the test that was running automatically.

If you are using jUnit you can use:

@Rule
public TestRule watcher = new TestWatcher() {
protected void starting(Description description) {
LOGGER.info("Starting test " + description.getMethodName() + "... ");
}
};
Reply all
Reply to author
Forward
0 new messages