Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://selftechy.com/2011/05/23/selenium-logging-with-log4j
1 de 10
09/08/2012 18:11
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
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] ______________________________________________________________
3 de 10
09/08/2012 18:11
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
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
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
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
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
8 de 10
09/08/2012 18:11
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
4 log4j Methods Microsoft Excel Object identification Object Repository Objects operators Parameterization
9 de 10
09/08/2012 18:11
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