Expect for .NET 2.0.0-beta released

Beta by Simon Cunningham license CC BY 2.0

Beta by Simon Cunningham license CC BY 2.0 (http://www.lendingmemo.com)

I’m happy to announce that beta release of Expect for .NET 2.0.0 was published. It can be downloaded using NuGet and from github. As it is beta release there may be introduced some API changes prior to final release.

The released version was tested with both C# and VB.NET applications. Example applications can be found in github repository.

The new version has changed API. Below are some code snippets presenting how to use it:


using Expect

class Program
    static void Main(string[] args)
            ISession spawn = Expect.Expect.Spawn(new ProcessSpawnable("cmd.exe"));
            spawn.Expect(">", s => Console.WriteLine("got: " + s));
            spawn.Send("dir c:\\\n");
            spawn.Expect("Program", (s) => Console.WriteLine("found: " + s));
        catch (Exception e)


Imports Expect

Module Module1

    Sub Main()
        Dim session As ISession = Expect.Expect.Spawn(New ProcessSpawnable("cmd.exe"))
            session.Expect(">", Sub(s) Console.WriteLine("Prompt --> " + s))
            session.Send("dir c:\" + Environment.NewLine)
            session.Expect("Program Files", Sub(s) Console.WriteLine(s))
            session.Expect(">", Sub() session.Send("ping" + Environment.NewLine))
        Catch ex As System.TimeoutException
        End Try
        session.Expect("Lost = 0", Sub() session.Send("ping" + Environment.NewLine))
        session.Expect("Lost = 0", Sub(s) Console.WriteLine(s))
    End Sub

End Module

I would be happy to hear some feedback from you.

3 thoughts on “Expect for .NET 2.0.0-beta released

  1. Wojtek,

    Nice work. I have been looking for something like Expect.NET for quite a while. I wanted to let you know of a few modifications that I made to Session and ISession in case you are interested in incorporating them.

    1. Added a transcript of all output processed by Expect, regardless of whether Expect() finds a matching string, so that at the end of a session the transcript can be dumped into a log file. Also examining the transcript allows one to easily debug a failed Expect() match .
    2. Added public string Expect(string query) and public async Task ExpectAsync(string query) methods to the Expect class. Both methods simply return _output. This streamlined some code that I wrote for controlling Cisco routers.

    I didn’t add test cases for either modification as I am not familiar with Moq yet. Also I am curious why you chose to access _timeout via GetTimeout() and SetTimeout(int timeout) instead of using get/set accessors. Is this a personal preference or are you following a programming style? Finally, I haven’t decided yet if a third modification would be useful – a modification that grants access to _output when Expect fails to find a match. My transcript mod is good for human debugging, but it might also be useful to have the output available for programmatically determining if recovery from an error condition is possible. I haven’t really thought this through though.


    • Matt,

      Thanks for the comment. That’s great that you want to share your changes. Please create pull request on github.

      Regarding change 1. I think that it would be better to have this feature optional and disabled by default as it may consume a lot of memory for long running Session.

      Regarding timeout setting – I blindly followed ‘set timeout’ expression from original Tcl expect, but you are right that using get/set is better for .NET. I will change that.

      Regarding third change… I have in future feature list variant of Expect method which accepts multiple pairs. It will be better approach for error handling and recovery procedures.


  2. Pingback: Expect for .NET 2.0.0-rc released | Wojtek Iwanek Blog

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.