Tuesday, April 17, 2012

How to handle logging in Selenium RC


Main thing while automating your application is Logging and Report Generation.
Below is the process how to implement logging with log4j.jar

Follow the steps
1. Go to apache site http://logging.apache.org/log4j/1.2/download.html and download log4j-1.2.16.jar file
2. Under Eclipse create a java class with name Log4jXmlTest
and paste the following code

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;


public class Log4jXmlTest extends SeleneseTestCase {
SeleniumServer seleniumserver;


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

   @Before
public void setUp() throws Exception {
    try{
    seleniumserver = new SeleniumServer();
    seleniumserver.start();
    int i = seleniumserver.getPort();
    System.out.println("Selenium Running on Port:"+i);
   
    }
    catch(Exception e){
    e.printStackTrace();
    }
   
    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();
}

}


3. Again Create .xml file with name log4j.xml and paste the following


<?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>


Note:  While doing copy paste, be careful that double quotes will be in some other format. So after pasting the code verify once that it should not throw any error.

4. Place the xml file created above in the project root folder.
    To place the xml file in root folder do the following
     Go to your main project name in eclipse and right click and select --> Properties
     You will see the Location under Resources link from the Properties window
     Go to that folder and paste the xml file
4. Run the Log4jXmlTest java file
5. After successful running of java class, xml file create logfile.log file which will have the logs.



It will create logfile.log  text file under root directory of the project if you manually go to the specified directory. Here is the log file which if you follow the above steps



2012-04-17 19:54:43,627 INFO  [Log4jXmlTest] ______________________________________________________________
2012-04-17 19:54:43,627 INFO  [Log4jXmlTest] Initializing Selenium...
2012-04-17 19:54:43,752 INFO  [Credential] Checking Resource aliases
2012-04-17 19:54:43,768 INFO  [SeleniumDriverResourceHandler] Command request: getNewBrowserSession[*chrome, http://www.google.co.in/, ] on session null
2012-04-17 19:54:43,783 INFO  [BrowserSessionFactory] creating new remote session
2012-04-17 19:54:43,971 INFO  [BrowserSessionFactory] Allocated session cc6ea8df996c412e8aca55f3d9d56080 for http://www.google.co.in/, launching...
2012-04-17 19:54:44,221 INFO  [FirefoxChromeLauncher] Preparing Firefox profile...
2012-04-17 19:54:47,377 INFO  [FirefoxChromeLauncher] Launching Firefox...
2012-04-17 19:54:52,299 INFO  [SeleniumDriverResourceHandler] Got result: OK,cc6ea8df996c412e8aca55f3d9d56080 on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:52,299 INFO  [Log4jXmlTest] Selenium instance started
2012-04-17 19:54:52,299 INFO  [Log4jXmlTest] Opening Google Website
2012-04-17 19:54:52,299 INFO  [SeleniumDriverResourceHandler] Command request: open[http://www.google.com/, ] on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:52,299 WARN  [SeleniumDriverResourceHandler] you appear to be changing domains from http://www.google.co.in/ to http://www.google.com/
this may lead to a 'Permission denied' from the browser (unless it is running as *iehta or *chrome,
or alternatively the selenium server is running in proxy injection mode)
2012-04-17 19:54:57,298 INFO  [SeleniumDriverResourceHandler] Got result: OK on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:57,298 INFO  [Log4jXmlTest] Clicking on advanced search link
2012-04-17 19:54:57,298 INFO  [SeleniumDriverResourceHandler] Command request: click[link=Advanced search, ] on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:57,361 INFO  [SeleniumDriverResourceHandler] Got result: OK on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:57,361 INFO  [SeleniumDriverResourceHandler] Command request: waitForPageToLoad[30000, ] on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:59,298 INFO  [SeleniumDriverResourceHandler] Got result: OK on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:59,298 INFO  [Log4jXmlTest] Entering search terms
2012-04-17 19:54:59,298 INFO  [SeleniumDriverResourceHandler] Command request: type[as_q, selenium,selftechy] on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:59,470 INFO  [SeleniumDriverResourceHandler] Got result: OK on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:59,470 INFO  [Log4jXmlTest] Clicking on Advanced Search button
2012-04-17 19:54:59,470 INFO  [SeleniumDriverResourceHandler] Command request: click[//input[@value='Advanced Search'], ] on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:59,580 INFO  [SeleniumDriverResourceHandler] Got result: OK on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:54:59,580 INFO  [SeleniumDriverResourceHandler] Command request: waitForPageToLoad[30000, ] on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:55:02,486 INFO  [SeleniumDriverResourceHandler] Got result: OK on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:55:02,486 INFO  [Log4jXmlTest] Stopping Selenium...
2012-04-17 19:55:02,486 INFO  [Log4jXmlTest] ______________________________________________________________
2012-04-17 19:55:02,486 INFO  [SeleniumDriverResourceHandler] Command request: testComplete[, ] on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:55:02,486 INFO  [FirefoxChromeLauncher] Killing Firefox...
2012-04-17 19:55:02,861 INFO  [SeleniumDriverResourceHandler] Got result: OK on session cc6ea8df996c412e8aca55f3d9d56080
2012-04-17 19:55:02,876 INFO  [SeleniumServer] Shutting down...
2012-04-17 19:55:02,876 INFO  [ThreadedServer] Stopping Acceptor ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=4444]
2012-04-17 19:55:02,986 INFO  [SocketListener] Stopped SocketListener on 0.0.0.0:4444
2012-04-17 19:55:03,033 INFO  [Container] Stopped HttpContext[/selenium-server/driver,/selenium-server/driver]
2012-04-17 19:55:03,095 INFO  [Container] Stopped HttpContext[/selenium-server,/selenium-server]
2012-04-17 19:55:03,142 INFO  [Container] Stopped HttpContext[/,/]
2012-04-17 19:55:03,142 INFO  [Container] Stopped org.openqa.jetty.jetty.servlet.ServletHandler@1befab0
2012-04-17 19:55:03,204 INFO  [Container] Stopped HttpContext[/wd,/wd]
2012-04-17 19:55:03,204 INFO  [Container] Stopped org.openqa.jetty.jetty.Server@1f20eeb

1 comment:

  1. can u explain step by step how to create a customized report in rc for say ---if u enter irctc without username and password an alert comes so here u need to add it to report- how it is possible?

    ReplyDelete