public static string GenerateScriptMethodTest(DeviceConfig deviceConfigAndroid, DeviceConfig deviceConfigIOS, TpLocatorEstrategy locatorEstrategy, XPathSelector.XPathType individualExpressionType, bool staticMethod, string appiumDriverName, out string functionCall, out string appiumConfig)
        {
            functionCall = "";
            appiumConfig = "";

            #region validation
            string msg = "";

            if (!Support.MyNode.CheckDeviceConfigCompatibility(deviceConfigAndroid, deviceConfigIOS, out msg))
            {
                return(msg);
            }

            #endregion


            StringBuilder scriptCallFunction = new StringBuilder();
            List <string> functionList       = new List <string>();
            List <string> scriptList         = new List <string>();
            StringBuilder appiumConfigList   = new StringBuilder();

            string elementName = "e";

            int qtdEvents = deviceConfigAndroid.Events.Count;
            for (int i = 0; i < qtdEvents; i++)
            {
                MyNode nAndroid = deviceConfigAndroid.Events[i];
                MyNode nIOS     = deviceConfigIOS.Events[i];


                string scriptFunction = GenerateScriptMethodTest(nAndroid, nIOS, locatorEstrategy, individualExpressionType, staticMethod, elementName, appiumDriverName, out functionCall);

                if (!functionList.Contains(functionCall))
                {
                    functionList.Add(functionCall);
                    scriptList.Add(scriptFunction);
                    scriptCallFunction.AppendLine(_tab3 + functionCall);
                }
            }


            #region appium config


            appiumConfigList.AppendLine();

            if (!EnabledEvaluation)
            {
                appiumConfigList.AppendLine(_tab3 + "_locator = new LocatorStrategy(_driver, null);");
                appiumConfigList.AppendLine();
            }
            else
            {
                #region Evaluation

                string strategy = locatorEstrategy.ToString();
                if (locatorEstrategy == TpLocatorEstrategy.IndividualExpression)
                {
                    strategy += individualExpressionType.ToString();
                }

                appiumConfigList.AppendLine(_tab3 + "Exec.Create(\"" + deviceConfigAndroid.AppName + "\", ProjectConfig.OutputDeviceID, \"" + deviceConfigAndroid.TestCaseName + "\", " + deviceConfigAndroid.Events.Count + ",\"" + strategy + "\", ProjectConfig.OutputPath);");
                appiumConfigList.AppendLine(_tab3 + "Exec.Instance.Start();");
                #endregion
                appiumConfigList.AppendLine();
                appiumConfigList.AppendLine(_tab3 + "_locator = new LocatorStrategy(_driver, Exec.Instance);");
                appiumConfigList.AppendLine();
            }

            #endregion

            scriptCallFunction.AppendLine();
            scriptCallFunction.AppendLine(_tab3 + "Exec.Instance.EndSuccefull();");


            functionCall = scriptCallFunction.ToString();
            appiumConfig = appiumConfigList.ToString();

            return(string.Join("\n", scriptList.ToArray()));
        }
        public static string GenerateScriptMethodTest(MyNode nodeDeviceAndroid, MyNode nodeDeviceIOS, TpLocatorEstrategy locatorEstrategy, XPathSelector.XPathType individualExpressionType, bool staticMethod, string elementName, string appiumDriverName, out string functionCall)
        {
            List <XPathSelector> selectors = MyNode.ExtractSelectors(nodeDeviceAndroid, nodeDeviceIOS);

            #region Contingency Selector
            string contingencySelectorForAndroid = selectors.Where(s => s.Type == XPathSelector.XPathType.AbsolutePath).Single().XPathForAndroid;
            string contingencySelectorForIOS     = selectors.Where(s => s.Type == XPathSelector.XPathType.AbsolutePath).Single().XPathForIOS;
            #endregion

            if (locatorEstrategy == TpLocatorEstrategy.IndividualExpression)
            {
                //Only a XPath
                selectors = selectors.Where(s => s.Type == individualExpressionType).ToList();
            }
            else if (locatorEstrategy == TpLocatorEstrategy.CombinedExpressionsInOrder)
            {
                selectors = MyNode.OrderBySelectorInOrder(selectors);
            }
            else if (locatorEstrategy == TpLocatorEstrategy.CombinedExpressionsMultiLocator)
            {
                //nothing
            }



            StringBuilder scriptFunction     = new StringBuilder();
            StringBuilder scriptFunctionBody = new StringBuilder();

            string tpStatic = " ";

            if (staticMethod)
            {
                tpStatic = " static ";
            }

            string functionName = nodeDeviceAndroid.EventName.Replace(" ", string.Empty);

            if (nodeDeviceAndroid.SendClick)
            {
                functionName += "SendClick_Test";
            }
            else if (nodeDeviceAndroid.SendKeys)
            {
                functionName += "SendKeys_Test";
            }

            if (nodeDeviceAndroid.WaitElementBySecond > 0)
            {
                scriptFunctionBody.AppendLine(_tab3 + "System.Threading.Thread.Sleep(" + (nodeDeviceAndroid.WaitElementBySecond * 1000) + ");");
            }


            List <string> selectorsAndroid = new List <string>();
            List <string> selectorsIOS     = new List <string>();
            List <string> selectorsType    = new List <string>();

            foreach (var s in selectors)
            {
                selectorsAndroid.Add("@\"" + s.XPathForAndroid + "\"");
                selectorsIOS.Add("@\"" + s.XPathForIOS + "\"");
                selectorsType.Add("@\"" + s.Type.ToString() + "\"");
            }


            scriptFunctionBody.AppendLine(_tab3 + "ForceUpdateScreen();");

            #region
            if (EnabledEvaluation)
            {
                scriptFunctionBody.AppendLine(_tab3 + "Exec.Instance.AddEvent(\"" + nodeDeviceAndroid.EventName + "\");");
                scriptFunctionBody.AppendLine();
            }
            #endregion

            scriptFunctionBody.AppendLine(_tab3 + "string[] selectors = new string[0];");

            if (_enabledEvaluation)
            {
                scriptFunctionBody.AppendLine(_tab3 + "string contingencyXPathSelector = \"\";");
            }


            scriptFunctionBody.AppendLine();
            scriptFunctionBody.AppendLine(_tab3 + "if (ProjectConfig.PlataformName == \"Android\")");
            scriptFunctionBody.AppendLine(_tab3 + "{");
            scriptFunctionBody.AppendLine(_tab4 + "selectors = new string[] {" + string.Join(", ", selectorsAndroid) + "};");

            if (_enabledEvaluation)
            {
                scriptFunctionBody.AppendLine(_tab4 + "contingencyXPathSelector = \"" + contingencySelectorForAndroid + "\";");
            }

            scriptFunctionBody.AppendLine(_tab3 + "}");
            scriptFunctionBody.AppendLine(_tab3 + "else if (ProjectConfig.PlataformName == \"iOS\")");
            scriptFunctionBody.AppendLine(_tab3 + "{");
            scriptFunctionBody.AppendLine(_tab4 + "selectors = new string[] {" + string.Join(", ", selectorsIOS) + "};");

            if (_enabledEvaluation)
            {
                scriptFunctionBody.AppendLine(_tab4 + "contingencyXPathSelector = \"" + contingencySelectorForIOS + "\";");
            }

            scriptFunctionBody.AppendLine(_tab3 + "}");

            scriptFunctionBody.AppendLine();
            scriptFunctionBody.AppendLine(_tab3 + "string[] selectorsType = new string[] {" + string.Join(", ", selectorsType) + "};");

            scriptFunctionBody.AppendLine();

            if (locatorEstrategy == TpLocatorEstrategy.IndividualExpression)
            {
                scriptFunctionBody.AppendLine(_tab3 + "IWebElement " + elementName + " = _locator.FindElementByXPath(selectors[0], selectorsType[0]);");
            }
            else if (locatorEstrategy == TpLocatorEstrategy.CombinedExpressionsInOrder)
            {
                scriptFunctionBody.AppendLine(_tab3 + "IWebElement " + elementName + " = _locator.FindElementByXPathInOrder(selectors, selectorsType);");
            }
            else if (locatorEstrategy == TpLocatorEstrategy.CombinedExpressionsMultiLocator)
            {
                scriptFunctionBody.AppendLine(_tab3 + "IWebElement " + elementName + " = _locator.FindElementByXPathMultiLocator(selectors, selectorsType);");
            }

            scriptFunctionBody.AppendLine();

            if (_enabledEvaluation)
            {
                scriptFunctionBody.AppendLine(_tab3 + "if (e == null)");
                scriptFunctionBody.AppendLine(_tab3 + "{");
                scriptFunctionBody.AppendLine(_tab4 + "e = _locator.FindElementByContingencyXPath(contingencyXPathSelector);");
                scriptFunctionBody.AppendLine(_tab4 + "if (e != null)");
                scriptFunctionBody.AppendLine(_tab5 + "Exec.Instance.CurrentEvent.UsedContingencyXPathSelector = true;");
                scriptFunctionBody.AppendLine(_tab3 + "}");
                scriptFunctionBody.AppendLine();
            }


            if (nodeDeviceAndroid.SendClick)
            {
                scriptFunctionBody.Append(_tab3 + elementName + ".Click();");
            }
            else if (nodeDeviceAndroid.SendKeys)
            {
                scriptFunctionBody.AppendLine(_tab3 + elementName + ".Click();");
                scriptFunctionBody.AppendLine(_tab3 + elementName + ".Clear();");
                scriptFunctionBody.AppendLine(_tab3 + elementName + ".SendKeys(\"" + nodeDeviceAndroid.SendKeysText + "\");");
                scriptFunctionBody.AppendLine(_tab3 + "try {");
                scriptFunctionBody.AppendLine(_tab4 + "if (ProjectConfig.PlataformName == \"Android\")");
                scriptFunctionBody.Append(_tab5 + appiumDriverName + ".HideKeyboard();");
                scriptFunctionBody.AppendLine();
                scriptFunctionBody.AppendLine(_tab4 + "else if (ProjectConfig.PlataformName == \"iOS\")");
                scriptFunctionBody.Append(_tab5 + appiumDriverName + ".FindElementByXPath(\"//*[@name='Hide keyboard']\").Click();");
                scriptFunctionBody.AppendLine();
                scriptFunctionBody.Append(_tab3 + "} catch {}");
            }
            scriptFunctionBody.AppendLine();
            scriptFunctionBody.AppendLine();
            scriptFunctionBody.AppendLine(_tab3 + "/*Insert your assert here*/");


            #region
            if (EnabledEvaluation)
            {
                scriptFunctionBody.AppendLine();
                scriptFunctionBody.AppendLine(_tab3 + "Exec.Instance.CurrentEvent.EndSucessfull();");
            }
            #endregion

            scriptFunction.AppendLine(_tab2 + "public" + tpStatic + "void " + functionName + "()");
            scriptFunction.AppendLine(_tab2 + "{");
            scriptFunction.AppendLine(scriptFunctionBody.ToString());
            scriptFunction.AppendLine(_tab2 + "}");
            scriptFunction.AppendLine("");

            functionCall = functionName + "(); //" + nodeDeviceAndroid.EventName;

            return(scriptFunction.ToString());
        }