Anda di halaman 1dari 10

Selenium Logging with Log4j

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

Home About Selenium

Selenium Logging with Log4j


by 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 to JUL. 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 logging requests to appender. Appender might be a console, a log file, printer, etc. Layout is the formatting of the log output. I think this is sufficient for this post and for any further information on Log4j you can certainly visit Google and search for Log4j. On internet, 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 import import import import import import import import com.thoughtworks.selenium.*; org.junit.After; org.junit.Before; org.junit.Test; org.apache.log4j.Logger; org.apache.log4j.xml.DOMConfigurator; org.apache.log4j.*; org.apache.log4j.FileAppender; org.apache.log4j.RollingFileAppender;

/** * Author: Seetaram Hegde

1 de 10

09/08/2012 18:11

Selenium Logging with Log4j

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

*/ public class Log4jXmlTest extends SeleneseTestCase { private static Logger Log = Logger.getLogger(Log4jXmlTest.class.getName());// @Before public 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"); } @Test public 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"); } @After public 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) <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE log4j:configuration SYSTEM log4j.dtd> <log4j:configuration xmlns:log4j=http://jakarta.apache.org/log4j/ debug=false> <appender name=fileAppender class=org.apache.log4j.FileAppender> <param name=Threshold value=INFO /> <param name=File value=logfile.log/> <layout class=org.apache.log4j.PatternLayout> <param name=ConversionPattern value=%d %-5p [%c{1}] %m %n /> </layout> </appender> <root> <level value=INFO/> <appender-ref ref=fileAppender/> </root> </log4j:configuration> 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).

2 de 10

09/08/2012 18:11

Selenium Logging with Log4j

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

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

3 de 10

09/08/2012 18:11

Selenium Logging with Log4j

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

{ 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 15 log4j: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.

4 de 10

09/08/2012 18:11

Selenium Logging with Log4j

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

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. log4j:WARN Fatal parsing error 1 and column 15 log4j: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)

5 de 10

09/08/2012 18:11

Selenium Logging with Log4j

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

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 make some changes such as, first change the double quotes (i.e. you need to again key in the because the one you copied will be in special character format). Second, change the path of the log file as required. ( sometimes after 1.0 after 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 are not 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 log generatd 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 files InetAddress SysIP=InetAddress.getLocalHost(); SysIP.getHostAddress() Reply Harish May 7, 2012 at 8:49 pm

6 de 10

09/08/2012 18:11

Selenium Logging with Log4j

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

Hi, Im using SeleniumRC with TestNG and running my scripts through ant. My requirement is to incorporate the seleniumserver logs 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 for logging but it doesnt work. Here is the code in log4j properties file #Application Logs log4j.logger.devpinoyLogger=DEBUG, dest1 log4j.appender.dest1=org.apache.log4j.RollingFileAppender log4j.appender.dest1.maxFileSize=5000KB log4j.appender.dest1.maxBackupIndex=3 log4j.appender.dest1.layout=org.apache.log4j.PatternLayout log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n log4j.appender.dest1.File=(path to the file)\\Application.log #do not append the old file. Create a new log file everytime log4j.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 seleniumserver logs 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 for logging but it doesnt work. Here is the code in log4j properties file #Application Logs log4j.logger.devpinoyLogger=DEBUG, dest1 log4j.appender.dest1=org.apache.log4j.RollingFileAppender log4j.appender.dest1.maxFileSize=5000KB log4j.appender.dest1.maxBackupIndex=3 log4j.appender.dest1.layout=org.apache.log4j.PatternLayout log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n log4j.appender.dest1.File=(path to the file)\\Application.log #do not append the old file. Create a new log file everytime log4j.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,

7 de 10

09/08/2012 18:11

Selenium Logging with Log4j

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

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

8 de 10

09/08/2012 18:11

Selenium Logging with Log4j

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

Popular Posts
Parameterization of Selenium Tests with Microsoft Excel Selenium 2.0 WebDriver A Test Automation example Selenium Use Ant to Generate HTML Reports Setting up Selenium with Eclipse Execute Selenium Test Using Eclipse

Recent Posts
Android A Revolutionary Mobile OS Some Thoughts on Learning Test Automation TestNG Test Automation with Selenium TestNG (Next Generation Testing Framework) Understanding Annotations Setting up TestNG with Eclipse

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

Categories
Ant Automation Testing Java Mobile Apps Testing Quick Test Professional Selenium Selenium 2.0 Uncategorized VBScript WebDriver

Tags
Annotations

Ant

arithmetic operators

Arrays Assertions Automation Testing Build Process Chromedriver Classes configuration CSV Data Types eclipse for loop

functions if if else Inheritance Java JUnit JUnit

4 log4j Methods Microsoft Excel Object identification Object Repository Objects operators Parameterization

9 de 10

09/08/2012 18:11

Selenium Logging with Log4j

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

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

QTP

Do you want to get updates on the recent articles written? Please subscribe to RSS 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

10 de 10

09/08/2012 18:11

Anda mungkin juga menyukai