Showing posts with label Action Name. Show all posts
Showing posts with label Action Name. Show all posts

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;
        }