Wednesday, February 22, 2012

Using Reflection to track page object actions

Some quick code I thought I would share.  This will use .Net reflection to return the Page Object function currently being executed.  It's just a nice way to track or log what action failed without having to resort to interception or some other technical wizardry.


 public static string GetPageObjectFunctionName()
        {
            string functionName = "";
            System.Diagnostics.StackTrace callStack = new System.Diagnostics.StackTrace();

            System.Diagnostics.StackFrame[] frames = callStack.GetFrames();
            foreach (System.Diagnostics.StackFrame frame in frames)
            {
                if (frame.GetMethod().ToString().Contains("PageObject"))
                {

                    functionName = frame.GetMethod().ReflectedType.Name + "." +  frame.GetMethod().Name.ToString();

                }

            }
            return functionName;
        }

2 comments:

  1. Excellent Post. Also visit http://whiteboxqa.com/selenium.php

    ReplyDelete
  2. Really useful.Thanks.

    You may also visit for similar articles at http://testocean.blogspot.in

    ReplyDelete