Selenium – Logging with Log4j

download Selenium – Logging with Log4j

of 10

Transcript of Selenium – Logging with Log4j

  • HomeAboutSelenium

    Selenium Logging with Log4jby seetaram on May 23, 2011

    If we are executing Test Suite with hundreds of automated test cases then logging all the events might be useful.

    There are two APIs available for logging events.

    JUL Java Logging API

    Log4j Apache Software Foundation

    I am going to discuss Log4j as this is an open source logging library as well as it is very much flexible and extensible compared toJUL. Log4j is built as a subproject of Logging Services Project by the Apache Software Foundation.

    Log4j is built with three main concepts: loggers, appenders, and layouts. Logger is the main engine which sends the loggingrequests to appender. Appender might be a console, a log file, printer, etc. Layout is the formatting of the log output. I think thisis sufficient for this post and for any further information on Log4j you can certainly visit Google and search for Log4j. Oninternet, you can get tons of information available for Log4j.

    In the following sections configurations of Log4j and also using the logging inside a Selenium test is explained.

    Download Log4j from the Apaches download link:

    http:/www.apache.org/dyn/closer.cgi/logging/log4j/1.2.16/apache-log4j-1.2.16.zip

    Step 1: Add the Log4j JAR file to the Java Build Path (log4j-1.2.16.jar)

    Step 2: Create a new class Log4jXmlTest

    Step 3: Copy the following code into the newly created class

    package com.selftechy.junit4;

    import com.thoughtworks.selenium.*;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.apache.log4j.Logger;import org.apache.log4j.xml.DOMConfigurator;import org.apache.log4j.*;import org.apache.log4j.FileAppender;import org.apache.log4j.RollingFileAppender;

    /** * Author: Seetaram Hegde

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    1 de 10 09/08/2012 18:11

  • */public class Log4jXmlTest extends SeleneseTestCase {

    private static Logger Log = Logger.getLogger(Log4jXmlTest.class.getName());//

    @Beforepublic void setUp() throws Exception {

    DOMConfigurator.configure("log4j.xml"); Log.info("______________________________________________________________"); Log.info("Initializing Selenium...");

    selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.google.co.in/");selenium.start();Log.info("Selenium instance started");

    }

    @Testpublic void testAdvancedSearch() throws Exception {

    Log.info("Opening Google Website");selenium.open("http://www.google.com/");Log.info("Clicking on advanced search link");selenium.click("link=Advanced search");selenium.waitForPageToLoad("30000");Log.info("Entering search terms");selenium.type("as_q", "selenium,selftechy");Log.info("Clicking on Advanced Search button");selenium.click("//input[@value='Advanced Search']");selenium.waitForPageToLoad("30000");

    }

    @Afterpublic void tearDown() throws Exception {

    Log.info("Stopping Selenium...");Log.info("______________________________________________________________");selenium.stop();

    }

    }

    Step 4: Create a new XML file log4j.xml

    Step 5: Copy the following code into it and save (make sure that file is saved as xml file NOT as .txt file)

    Step 7: Place this log4j.xml file into Project root folder (to find out the path -> right click on the project -> click on properties. Location shows the projects root directory).

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    2 de 10 09/08/2012 18:11

  • Path to Project's root folder

    Step 8: Go to Run -> Run As -> JUnit Test

    Step 9: It should create a output log file in the projects root folder with name logfile.log

    Step 10: Open the log file, following lines should be there in the log file.

    2011-05-23 15:30:18,931 INFO [Log4jXmlTest] ______________________________________________________________

    2011-05-23 15:30:18,931 INFO [Log4jXmlTest] Initializing Selenium

    2011-05-23 15:30:24,773 INFO [Log4jXmlTest] Selenium instance started

    2011-05-23 15:30:24,773 INFO [Log4jXmlTest] Opening Google Website

    2011-05-23 15:30:26,850 INFO [Log4jXmlTest] Clicking on advanced search link

    2011-05-23 15:30:27,460 INFO [Log4jXmlTest] Entering search terms

    2011-05-23 15:30:27,491 INFO [Log4jXmlTest] Clicking on Advanced Search button

    2011-05-23 15:30:27,819 INFO [Log4jXmlTest] Stopping Selenium

    2011-05-23 15:30:27,819 INFO [Log4jXmlTest] ______________________________________________________________

    Tagged as: Automation Testing, JUnit 4, log4j

    You might also like:

    Selenium Creating Object Repository

    Parameterization of Tests in Selenium IDE

    JUnit 4 Executing multiple Test Suites

    Selenium Understanding Object Identification

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    3 de 10 09/08/2012 18:11

  • { 12 comments read them below or add one }

    Irfan August 1, 2011 at 1:01 pm

    I am getting following exception:

    log4j:WARN Fatal parsing error 1 and column 15log4j:WARN Invalid byte 1 of 1-byte UTF-8 sequence.org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.at org.apache.xerces.impl.io.UTF8Reader.invalidByte(Unknown Source)at org.apache.xerces.impl.io.UTF8Reader.read(Unknown Source)at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)at org.apache.xerces.impl.XMLEntityScanner.skipDeclSpaces(Unknown Source)at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:891)at com.selftechy.junit4.Log4jXmlTest.setUp(Log4jXmlTest.java:20)at junit.framework.TestCase.runBare(TestCase.java:132)at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)at junit.framework.TestResult$1.protect(TestResult.java:110)at junit.framework.TestResult.runProtected(TestResult.java:128)at junit.framework.TestResult.run(TestResult.java:113)at junit.framework.TestCase.run(TestCase.java:124)at junit.framework.TestSuite.runTest(TestSuite.java:232)at junit.framework.TestSuite.run(TestSuite.java:227)at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)log4j:ERROR Could not parse file [log4j.xml].org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:891)at com.selftechy.junit4.Log4jXmlTest.setUp(Log4jXmlTest.java:20)at junit.framework.TestCase.runBare(TestCase.java:132)at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)at junit.framework.TestResult$1.protect(TestResult.java:110)at junit.framework.TestResult.runProtected(TestResult.java:128)at junit.framework.TestResult.run(TestResult.java:113)at junit.framework.TestCase.run(TestCase.java:124)at junit.framework.TestSuite.runTest(TestSuite.java:232)at junit.framework.TestSuite.run(TestSuite.java:227)at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)log4j:WARN No appenders could be found for logger (com.selftechy.junit4.Log4jXmlTest).log4j:WARN Please initialize the log4j system properly.

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    4 de 10 09/08/2012 18:11

  • log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.log4j:WARN Fatal parsing error 1 and column 15log4j:WARN Invalid byte 1 of 1-byte UTF-8 sequence.org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.at org.apache.xerces.impl.io.UTF8Reader.invalidByte(Unknown Source)at org.apache.xerces.impl.io.UTF8Reader.read(Unknown Source)at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)at org.apache.xerces.impl.XMLEntityScanner.skipDeclSpaces(Unknown Source)at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:891)at com.selftechy.junit4.Log4jXmlTest.testAdvancedSearch(Log4jXmlTest.java:31)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at junit.framework.TestCase.runTest(TestCase.java:168)at junit.framework.TestCase.runBare(TestCase.java:134)at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)at junit.framework.TestResult$1.protect(TestResult.java:110)at junit.framework.TestResult.runProtected(TestResult.java:128)at junit.framework.TestResult.run(TestResult.java:113)at junit.framework.TestCase.run(TestCase.java:124)at junit.framework.TestSuite.runTest(TestSuite.java:232)at junit.framework.TestSuite.run(TestSuite.java:227)at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)log4j:ERROR Could not parse file [log4j.xml].org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:866)at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:891)at com.selftechy.junit4.Log4jXmlTest.testAdvancedSearch(Log4jXmlTest.java:31)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at junit.framework.TestCase.runTest(TestCase.java:168)at junit.framework.TestCase.runBare(TestCase.java:134)at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:212)at junit.framework.TestResult$1.protect(TestResult.java:110)at junit.framework.TestResult.runProtected(TestResult.java:128)at junit.framework.TestResult.run(TestResult.java:113)at junit.framework.TestCase.run(TestCase.java:124)at junit.framework.TestSuite.runTest(TestSuite.java:232)at junit.framework.TestSuite.run(TestSuite.java:227)at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    5 de 10 09/08/2012 18:11

  • at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    Reply

    seetaram August 4, 2011 at 8:37 am

    I think this is because, you directly copied the XML for log4j configuration from the blog. There you need to makesome changes such as, first change the double quotes (i.e. you need to again key in the because the one youcopied will be in special character format). Second, change the path of the log file as required. ( sometimes after 1.0after 1.0 one ? character will be inserted when u copy from the blog that needs to be removed).

    Reply

    Madhu September 20, 2011 at 12:08 pm

    I am not getting logfile.log file after sucessful execution of log4j.xml file.

    Reply

    seetaram September 21, 2011 at 6:35 am

    I think the log4j.xml file is not properly configured..

    Reply

    Sneha December 6, 2011 at 10:49 am

    Hi,

    Can you help me with storing logs in an excel file?I wanted to know how to mention the cell numbers while logging.

    Thanks,Sneha

    Reply

    seetaram December 7, 2011 at 9:52 am

    No. I dont think that you can store logs in Excel file.. having said this you can create logs in excel sheet provided you arenot using log4j but jxl or poi packages

    I am not sure whether you can create logs in Excel file using log4j

    Reply

    Subhash April 4, 2012 at 12:00 pm

    Hi,

    Can we insert IP address in log ? I am using selenium grid with multiple RCs . In this case I unable to know which one loggeneratd from which one RC.

    Thanks,Subhash

    Reply

    seetaram April 16, 2012 at 12:44 pm

    Use the below code to get the IP address of the specific system and then insert into the specific log filesInetAddress SysIP=InetAddress.getLocalHost();

    SysIP.getHostAddress()

    Reply

    Harish May 7, 2012 at 8:49 pm

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    6 de 10 09/08/2012 18:11

  • Hi,

    Im using SeleniumRC with TestNG and running my scripts through ant. My requirement is to incorporate the selenium-serverlogs in to my application logs (file set in log4j.properties).

    Any help here would be very much appreciated.

    Here is the code for starting selenium server in build.xml file. I added an arg line forlogging but it doesnt work.

    Here is the code in log4j properties file

    #Application Logslog4j.logger.devpinoyLogger=DEBUG, dest1log4j.appender.dest1=org.apache.log4j.RollingFileAppenderlog4j.appender.dest1.maxFileSize=5000KBlog4j.appender.dest1.maxBackupIndex=3log4j.appender.dest1.layout=org.apache.log4j.PatternLayoutlog4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss}%c %m%nlog4j.appender.dest1.File=(path to the file)\\Application.log#do not append the old file. Create a new log file everytimelog4j.appender.dest1.Append=false

    Reply

    Harish May 7, 2012 at 8:50 pm

    Hi,

    Im using SeleniumRC with TestNG and running my scripts through ant. My requirement is to incorporate the selenium-serverlogs in to my application logs (file set in log4j.properties).

    Any help here would be very much appreciated.

    Here is the code for starting selenium server in build.xml file. I added an arg line forlogging but it doesnt work.

    Here is the code in log4j properties file

    #Application Logslog4j.logger.devpinoyLogger=DEBUG, dest1log4j.appender.dest1=org.apache.log4j.RollingFileAppenderlog4j.appender.dest1.maxFileSize=5000KBlog4j.appender.dest1.maxBackupIndex=3log4j.appender.dest1.layout=org.apache.log4j.PatternLayoutlog4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss}%c %m%nlog4j.appender.dest1.File=(path to the file)\\Application.log#do not append the old file. Create a new log file everytimelog4j.appender.dest1.Append=false

    Reply

    Siva May 15, 2012 at 4:29 am

    Thanks for the above explanation , Can you please provide the datailed steps to create log4j.properties file

    Reply

    Denis Ballant July 3, 2012 at 9:37 am

    Harish,

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    7 de 10 09/08/2012 18:11

  • Heres the solution to your problem:

    java.util.logging.Logger.getLogger(org.openqa.selenium).setLevel(Level.WARNING);

    I had the same problem and couldnt find the solution on the net.In fact Selenium is using the Java Logging facility.So you just need to disable it using the corresponding API, hence the line of code above.

    Cheers,

    Denis

    Reply

    Leave a Comment

    Name *

    E-mail *

    Website

    Previous post: JUnit4 @Before vs @BeforeClass / @After vs @AfterClass

    Next post: Setting up Selenium with Eclipse

    Subscribe via

    RSS Feed

    Subscribe via

    E-Mail

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    8 de 10 09/08/2012 18:11

  • Popular Posts

    Parameterization of Selenium Tests with Microsoft ExcelSelenium 2.0 WebDriver A Test Automation exampleSelenium Use Ant to Generate HTML ReportsSetting up Selenium with EclipseExecute Selenium Test Using Eclipse

    Recent Posts

    Android A Revolutionary Mobile OSSome Thoughts on Learning Test AutomationTestNG Test Automation with SeleniumTestNG (Next Generation Testing Framework) Understanding AnnotationsSetting up TestNG with Eclipse

    Archives

    June 2012April 2012January 2012December 2011November 2011October 2011September 2011August 2011July 2011June 2011May 2011April 2011March 2011February 2011

    Categories

    AntAutomation TestingJavaMobile Apps TestingQuick Test ProfessionalSeleniumSelenium 2.0UncategorizedVBScriptWebDriver

    Tags

    Annotations Ant arithmetic operators Arrays Assertions Automation Testing Build Process Chromedriver Classes configuration CSV Data Types eclipse for loopfunctions if if else Inheritance Java JUnit JUnit 4 log4j Methods Microsoft Excel Object identification Object Repository Objects operators Parameterization

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    9 de 10 09/08/2012 18:11

  • properties QTP QTP Fundamentals Quick Test Professional Selenium Selenium 2.0 Selenium IDE TestAutomation Test Automation Framework TestNG Variables VBScript WebDriver WebDriver APIs XML XPath

    Do you want to get updates on the recent articles written? Please subscribe toRSS feed or Email

    2010-2011 Selftechy.com - All rights reserved.

    No content on this site may be reused in any fashion without written permission from Selftechy.com

    Selenium Logging with Log4j http://selftechy.com/2011/05/23/selenium-logging-with-log4j

    10 de 10 09/08/2012 18:11