FenWangNZ / blog

I love learning. This is my wiki.
0 stars 0 forks source link

Add logging for tests #12

Open FenWangNZ opened 3 years ago

FenWangNZ commented 3 years ago
  1. Install Nlog nuget packages: Nlog and Nlog Config
  2. Entre each class file. Create a Log property and instantiate it.

The LogManager will get the logger of the current class from the Nlog package, which means it will use the current page for logging and output it to the message;

TIps1: Each class must be written like this at the beginning of the class, and it will have the appropriate information for this class. Remember to add" Using Nlog".

Tips2: Logger cannot penetrate into your functional test cases, and cannot affect functional testing. It can only be placed in PageObject.

We can add logger into our method:

internal void Open()
        {
            Driver.Navigate().GoToUrl(URL.LoginUrl);
            _logger.Info($"Opened url=>{URL.LoginUrl}");
        }

or

internal void NoInput()
        {
            Actions actionsObj = new Actions(Driver);
            //Move the cursor From the user account to password field
            actionsObj.Click(EmailAddress).Perform();
            _logger.Info($"Inputed no mail address=>{EmailAddress}");
            actionsObj.Click(PassWord).Perform();
            _logger.Info($"Inputed no password=>{PassWord}");
            actionsObj.Click(EmailAddress).Perform();
            _logger.Info($"moved mouse back to login field");
        }

Below is the NLog.config file in my project:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="/Users/wangfen/Downloads/temp/nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="appName" value="CNZBATests"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
 <targets async="true">
    <target xsi:type="File"
            name="default"
            layout="${longdate} - ${threadid} - ${callsite:className=true:fileName=false:includeSourcePath=false:methodName=true} - ${level:uppercase=true}: ${message} ${onexception:${newline}EXCEPTION\: ${exception:format=ToString}}"
            fileName="c:\temp\${appName}\Debug.log"
            keepFileOpen="false"
            archiveFileName="/Users/wangfen/Downloads/temp/${appName}/Debug_${shortdate}.{##}.log"
            archiveNumbering="Sequence"
            archiveEvery="Month"
            maxArchiveFiles="30"
            />

    <target xsi:type="EventLog"
            name="eventlog"
            source="${appName}"
            layout="${message}${newline}${exception:format=ToString}"/>
    <target name="bugLogger"
              xsi:type="File"
              fileName="/Users/wangfen/Downloads/temp/${appName}/BugLogger_${shortdate}.txt"
              layout="${message}"
              />
  </targets>
  <rules>
    <logger name="*" writeTo="default" minlevel="Debug" />
    <logger name="*" writeTo="eventlog" minlevel="Error" />
    <logger name="*" writeTo="debugger" minlevel="Info" />
    <logger name="*" writeTo="bugLogger" minlevel="Info"/>
  </rules>
</nlog>

Test result log for login successfully.

Screen Shot 2020-11-02 at 9 34 39 PM

Screen Shot 2020-11-02 at 9 34 24 PM

Screen Shot 2020-11-02 at 9 34 48 PM