How to generate logs with CodedUI?

Why logs? Well, it helps to debug the problem in our script. Usually logging and capturing the screenshots are not enable by default with CodedUI and to make it, we need to make some set up.

Follow the below steps to enable logging and capturing the screenshots-

1: Go to C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE location and search for ‘QTAgent32_40.exe.config’ and ‘QTAgent32.exe.config’. Make sure you have edit permission for these config files.

2: Modify the value for ‘EqtTraceLevel‘:

Log

3: Add App.config file in your project and add the following code under the configuration node:

Log1

4: Add following code in CodedUITest1.cs file-

Log2

5: Now run the test.

6: Open Test Explorer and click on Output hyperlink-

Log3

7: Click on UITestActionLog.html link.

Log4

8: Now check your script logs-

Log5

Log6

Logging with Log4J

Log4j is widely used API for generating the logs and there are few steps to configure it with Selenium Webdriver.

  • Download the latest log4j api from here.
  • Include the log4j-1.2.XX.jar file in your project build path.
  • Right click on your project > New > Other…>XML>XML FIle> and create a XML file with the log4j.xml name.
  • Copy and paste the below xml contents in newly created XML file-

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>
<log4j:configuration xmlns:log4j=”http://jakarta.apache.org/log4j/&#8221; debug=”false”>
<appender name=”fileAppender” class=”org.apache.log4j.FileAppender”>
<param name=”Threshold” value=”INFO” />
<param name=”File” value=”logfile.log“/>
<param name=”append” value=”true”/>
<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>

  • Create new class file without main method and paste below code-

public class TestCase_Logging {
public static WebDriver driver=null;
private static Logger Log = Logger.getLogger(TestCase_POF.class.getName());
public static void main(String[] args) {
DOMConfigurator.configure(“log4j.xml”);
System.setProperty(“webdriver.ie.driver”, “D:/IE_Driver/IEDriverServer.exe”);
driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Log.info(“Start the application.”);
driver.get(“https://google.com&#8221;);

}
}

Note: Highlighted codes are important to generate the log, so don’t miss them.

  • Now run the program and go to Project root folder. System has been generated logfile.log file.