private static CodeExpression getFindByNameDetailCode(GuiComponent Component)
        {
            if (Component == null)
            {
                return(null);
            }
            try
            {
                Stack <SapCompInfo> comps = new Stack <SapCompInfo>();
                do
                {
                    SapCompInfo ci = new SapCompInfo();
                    ci.Id   = Component.Id;
                    ci.Name = Component.Name;
                    ci.Type = Component.GetDetailType();
                    comps.Push(ci);
                    Component = Component.Parent;
                }while ((Component is GuiSession) == false);

                SapCompInfo top = comps.Pop();
                CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeVariableReferenceExpression("SAPTestHelper.Current.SAPGuiSession")
                        , "FindById"
                        , new CodeTypeReference(top.Type))
                    , new CodePrimitiveExpression(top.Name));


                while (comps.Count > 0)
                {
                    SapCompInfo temp = comps.Pop();
                    expression = new CodeMethodInvokeExpression(
                        new CodeMethodReferenceExpression(
                            expression
                            , "FindByName"
                            , new CodeTypeReference(temp.Type))
                        , new CodePrimitiveExpression(temp.Name));
                }
                return(expression);
            }
            catch
            {
                return(null);
            }
        }
        void _sapGuiSession_Change(GuiSession Session, GuiComponent Component, object CommandArray)
        {
            if (_stepAction == null)
            {
                return;
            }
            SapCompInfo cpInfo = new SapCompInfo();

            cpInfo.Id   = Component.Id;
            cpInfo.Name = Component.Name;
            cpInfo.Type = Component.GetDetailType();

            if (Component is GuiVComponent)
            {
                var vc = Component as GuiVComponent;
                try
                {
                    cpInfo.Tip = vc.DefaultTooltip == "" ? vc.Tooltip : vc.DefaultTooltip;
                }
                catch
                {
                }
            }


            if (_isFindById)
            {
                cpInfo.FindMethod = Component.FindByIdCode();
            }
            else
            {
                cpInfo.FindMethod = Component.FindByNameCode();
            }

            RecordStep step = new RecordStep();

            step.CompInfo = cpInfo;

            object[] objs = CommandArray as object[];

            objs = objs[0] as object[];
            switch (objs[0].ToString().ToLower())
            {
            case "m":
                step.Action = BindingFlags.InvokeMethod;
                break;

            case "sp":
                step.Action = BindingFlags.SetProperty;
                break;
            }

            var action = objs[1].ToString();

            upperFirstChar(ref action);

            step.ActionName = action;

            var count = objs.Count();

            if (count > 2)
            {
                step.ActionParams = new List <SAPDataParameter>();

                if (step.Action == BindingFlags.InvokeMethod)
                {
                    MethodInfo method = GetSAPTypeInfo(step.CompInfo.Type).GetMethod(step.ActionName);
                    //MethodInfo method = SAPAutomationHelper.Current.GetSAPTypeInfo<MethodInfo>(step.CompInfo.Type, t => t.GetMethod(step.ActionName));
                    int index = 2;
                    foreach (var pInfo in method.GetParameters())
                    {
                        var para = new SAPDataParameter();
                        para.Type    = pInfo.ParameterType;
                        para.Comment = pInfo.Name;
                        para.Value   = objs[index];
                        para.Name    = pInfo.Name;
                        step.ActionParams.Add(para);
                        index++;
                    }
                }
                else
                {
                    for (int i = 2; i < count; i++)
                    {
                        var para = new SAPDataParameter();
                        para.Type    = objs[i].GetType();
                        para.Value   = objs[i];
                        para.Comment = step.CompInfo.Tip;

                        step.ActionParams.Add(para);
                    }
                }
            }

            _stepAction(step);
        }
        public static CodeExpression FindByNameCode(this SapCompInfo CompInfo)
        {
            GuiComponent comp = SAPAutomationHelper.Current.GetSAPComponentById <GuiComponent>(CompInfo.Id);

            return(getFindByNameCode(comp));
        }
        private static CodeExpression getFindByNameCode(GuiComponent Component)
        {
            if (Component == null)
                return null;
            try
            {
                Stack<SapCompInfo> comps = new Stack<SapCompInfo>();
                do
                {
                    SapCompInfo ci = new SapCompInfo();
                    ci.Id = Component.Id;
                    ci.Name = Component.Name;
                    ci.Type = Component.GetDetailType();
                    comps.Push(ci);
                    Component = Component.Parent;
                }
                while ((Component is GuiSession) == false);

                SapCompInfo top = comps.Pop();
                CodeMethodInvokeExpression expression = new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                        new CodeVariableReferenceExpression("SAPTestHelper.Current.SAPGuiSession")
                        , "FindById"
                        , new CodeTypeReference(top.Type))
                        , new CodePrimitiveExpression(top.Name));

                while (comps.Count > 0)
                {
                    SapCompInfo temp = comps.Pop();
                    expression = new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(
                          expression
                          , "FindByName"
                          , new CodeTypeReference(temp.Type))
                          , new CodePrimitiveExpression(temp.Name));

                }
                return expression;
            }
            catch
            {
                return null;
            }
        }