Beispiel #1
0
        public static void GenerateCode(string pageObjectName, string resultDir, SplitDomElements sde)
        {
            var template = Template.Parse(File.ReadAllText("./Templates/CSharpCode.liquid"));
            var hash     = Hash.FromAnonymousObject(new { root = sde, name = pageObjectName });
            var rendered = template.Render(hash);

            Directory.CreateDirectory("./Results");
            File.WriteAllText($"./Results/{pageObjectName}.cs", rendered);
            var fullSource = new FileInfo($"./Results/{pageObjectName}.cs").FullName;
            var fullDest   = new FileInfo(resultDir + "\\" + $"{pageObjectName}.cs").FullName;

            File.Copy(fullSource, fullDest, true);
        }
        private static SplitDomElements DivvyElementsByType(List <IElement> elements)
        {
            var inputElements    = new List <InputElement>();
            var buttonElements   = new List <ButtonElement>();
            var selectElements   = new List <SelectElement>();
            var checkboxElements = new List <InputCheckboxElement>();
            var linkElements     = new List <LinkElement>();
            var radioElements    = new List <RadioElement>();
            var textAreaElements = new List <TextAreaElement>();

            var filteredElements = elements.Where(x => !string.IsNullOrEmpty(x.Id));

            foreach (var el in filteredElements)
            {
                var tagName      = el.TagName.ToLower();
                var idValues     = el.Id;
                var classValues  = el.ClassName;
                var variableName = ProcessIdToVariable(el);

                switch (tagName)
                {
                case "input" when el.Attributes["type"].Value != "hidden":
                {
                    if (el.Attributes["type"].Value == "checkbox")
                    {
                        var name = el.Attributes["name"]?.Value;
                        checkboxElements.Add(new InputCheckboxElement(idValues, classValues,
                                                                      name,
                                                                      name == "checked",
                                                                      variableName));
                    }
                    else
                    {
                        var name = el.Attributes["name"]?.Value;
                        var type = el.Attributes["type"]?.Value;
                        inputElements.Add(new InputElement(idValues, classValues, name,
                                                           type, el.InnerHtml, variableName));
                    }

                    break;
                }

                case "button":
                    buttonElements.Add(new ButtonElement(idValues, classValues, variableName));
                    break;

                case "select":
                {
                    var optionsElements = new List <OptionsElement>();
                    var options         = el.QuerySelectorAll("option");

                    optionsElements.AddRange(
                        options.Select(x =>
                        {
                            var @class = x.Attributes["class"]?.Value;
                            var value  = x.Attributes["value"]?.Value;
                            var optionsVariableName = ProcessIdToVariable(x);

                            return(new OptionsElement(x.Id, @class, x.InnerHtml, value, optionsVariableName));
                        }));

                    selectElements.Add(new SelectElement(idValues, classValues, optionsElements, variableName));
                    break;
                }

                case "a":
                    var href = el.Attributes["href"]?.Value;
                    linkElements.Add(new LinkElement(idValues, classValues, href, el.InnerHtml, variableName));
                    break;

                case "textarea":
                    textAreaElements.Add(new TextAreaElement(idValues, classValues, variableName));
                    break;
                }
            }

            var result = new SplitDomElements(
                inputElements,
                selectElements,
                buttonElements,
                checkboxElements,
                linkElements,
                radioElements,
                textAreaElements);

            return(result);
        }