Search code examples
c#selenium-webdriverroslynfluentautomation

Retrieve Timings from Fluent


I'm building up a Fluentautomation script with a pathway to step through my application with testing. Is it possible to record timings between actions, rather than just getting the overall timing at the end? i.e.

    var TestChrome = Require<F14N>()
        .Init<FluentAutomation.SeleniumWebDriver>()
        .Bootstrap("Chrome")
        .Config(settings => {
            // Easy access to FluentAutomation.Settings values
            settings.DefaultWaitUntilTimeout = TimeSpan.FromSeconds(1);
        });


    TestChrome.Run("Hello Google", I => {
        I.Open("http://master.neutrino.com");
        I.Enter("myUserName").In("#txtUsername");
        I.Enter("myPassword").In("#txtPassword");
        I.Click("#btnLogin");
    // want to log timing here 
        I.Enter("Fred Bloggs\r\n").In("#inputGlobalSearch");
       I.Wait(1);
    //log timing here also
    ...etc
    });

Solution

  • Since you're not chaining the methods on I, you can simply inject stopwatch code:

    var TestChrome = Require<F14N>()
        .Init<FluentAutomation.SeleniumWebDriver>()
        .Bootstrap("Chrome")
        .Config(settings => {
            // Easy access to FluentAutomation.Settings values
            settings.DefaultWaitUntilTimeout = TimeSpan.FromSeconds(1);
        });
    
    
    TestChrome.Run("Hello Google", I => {
        I.Open("http://master.neutrino.com");
        I.Enter("myUserName").In("#txtUsername");
        I.Enter("myPassword").In("#txtPassword");
        I.Click("#btnLogin");
    
        StopWatch sw = new StopWatch()
        sw.Start();
    
        I.Enter("Fred Bloggs\r\n").In("#inputGlobalSearch");
    
        sw.Stop();
        Debug.Write(sw.ElapsedMilliseconds);
    
        I.Wait(1);
    });
    

    Creating an extension method for I that starts and stops a timer should be doable as well.

    using FluentAutomation.Interfaces;
    
    public static class IExtension
    {
        public static StopWatch sw = new StopWatch();
    
        public static IActionSyntaxProvider StartTimer(this IActionSyntaxProvider) { sw.Reset(); sw.Start();  }
        public static IActionSyntaxProvider StopTimer(this IActionSyntaxProvider) { sw.Stop(); Trace.Write(sw.ElapsedMilliseconds); }
    
    }
    

    So it becomes:

    TestChrome.Run("Hello Google", I => {
        I.Open("http://master.neutrino.com");
        I.Enter("myUserName").In("#txtUsername");
        I.Enter("myPassword").In("#txtPassword");
        I.Click("#btnLogin");
        IExtension.StartTimer(I);
        I.Enter("Fred Bloggs\r\n").In("#inputGlobalSearch");
        IExtension.StopTimer(I);
    
        I.Wait(1);
    });
    

    Or fluently when Roslyn starts to support it:

    TestChrome.Run("Hello Google", I => {
        I
           .Open("http://master.neutrino.com");
           .Enter("myUserName").In("#txtUsername");
           .Enter("myPassword").In("#txtPassword");
           .Click("#btnLogin");
           .StartTimer();
           .Enter("Fred Bloggs\r\n").In("#inputGlobalSearch");
           .StopTimer();
           .Wait(1);
    });