2
votes

I'm trying to log information when I'm running my Selenium tests.

Here is some very simple sample code I am working with:

using System.Threading;
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

class EntryPoint
{

    static void Main(string[] args)
    {
        string url = "http://testing.todorvachev.com/selectors/css-path/";
        string cssPath = "#search-2 > form > label > input";
        string xPath = "//*[@id=\"search-2\"]/form/label/input";

        IWebDriver driver = new ChromeDriver();

        driver.Navigate().GoToUrl(url);

        IWebElement cssPathElement = driver.FindElement(By.CssSelector(cssPath));
        IWebElement xPathElement = driver.FindElement(By.XPath(xPath));

        if (cssPathElement.Displayed)
        {
            Console.WriteLine("I can see the CSS path element");
        }
        else
        {
            Console.WriteLine("I can't see it");
        }

        driver.Quit();
    }
}

I understand that Selenium has various logging levels (https://raw.githubusercontent.com/wiki/SeleniumHQ/selenium/DesiredCapabilities.md) and I'd just like to learn how to implement them from the command line.

I have been trying different variations of the following, with no luck:

C:\Users\sandra\Desktop\WSD\SeleniumPractice\SetupEnvironment\SetupEnvironment\bin\Debug>SetupEnvironment.exe -log C:\temp\log

The command runs fine, and gives this output:

Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 58605
Only local connections are allowed.
I can see the CSS path element

Nothing gets written to the file I specified in the command.

1
any error messages? - TrevorBrooks
No error messages. The test runs fine - the browser opens, does its thing, closes again. I'll update the post with the command line output. - sandramedina
Have you considered log4net? - so cal cheesehead
@socalcheesehead I haven't. Could you provide a little detail? - sandramedina
if what you're looking to accomplish is simply log to a file or the console log4net is a pretty nice utility. Instead of doing Console.WriteLine you simply write Log.Debug("blah") or whatever level you like - so cal cheesehead

1 Answers

0
votes

As an alternative to using Selenium's logging facility you could use log4net. Simply install the log4net package in your solution then define the property and initialize in your setup method

private ILog Log;

[OneTimeSetUp]    
public void OneTimeSetUp() 
{
    Log = LogManager.GetLogger(GetType()); 
}

[Test]
public void LogTest()
{
    Log.Info("test test test test");
}

Here's a sample log4net.config file you can use to get you started which will log to both the console and a log file:

<log4net>
  <root>
    <level value="ALL" />
    <appender-ref ref="console" />
    <appender-ref ref="file" />
  </root>
  <appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level %logger - %message%newline" />
    </layout>
  </appender>
  <appender name="file" type="log4net.Appender.RollingFileAppender">
    <file value="TestFramework.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
    </layout>
  </appender>
</log4net>

Again this is just an alternative to selenium logging should you want to give it a try.