1

I have a basic script that parses through HTML and creates a basic POM based on Inputs, Selects etc. It does a decent job but I was curious if there is any 'professional' or popularly used pieces that offers similar functionality?

This is for C# with Selenium 2 using the PageFactory.

  • 2
    Why generate the POM? Are you really going to use all elements in your tests? Sounds a bit like overkill, think YAGNI! :) en.wikipedia.org/wiki/You_aren%27t_gonna_need_it – Niels van Reijmersdal Apr 29 '16 at 15:13
  • Forms with 50+ required elements are a drain to write out a POM with by hand. – Paul Muir Apr 29 '16 at 15:23
  • Damn, lot of fields! :) Not sure I would want to be a user of a system like that though. Goodluck in finding your solution, but I do not think it exists, because generating paths or selectors doesn't sound very maintainable and will need a lot of manual work anyways. Better is to give to form fields logical selector/ID's so it is easy to code when making selenium tests. – Niels van Reijmersdal Apr 29 '16 at 15:40
  • They all have IDs, for the most part at least. My current solution gets me 90% of the way so far. – Paul Muir Apr 29 '16 at 15:42
1

I haven't tried it but there's a Chrome Extension called Selenium Page Object Generator that looks like it automates PO creation, and it supports C#.

SWD Page Recorder also automates PO creation, and supports C#.

  • The Chrome extension just sits there trying to process everything. I will see if I can limit down the elements some soon. The SWD seems decent overall but I seem to have to manually select and name all of the elements I want. Will be useful when having people assist me with it. – Paul Muir May 2 '16 at 19:57
1

My current Solution. Parses through the HTML file (C:\QA\Test.html), finds all input tags and Select tags, if they have an ID it will convert them into a POM based format. It then prints out to C:\QA\Pom.txt the basic object for it.

Overall, it does 90% of the work that I need, with minor editing.

using System;
using System.Linq;
using System.IO;
using CsQuery;
class Solution
{
    static void Main(string[] args)
    {
        var doc = CQ.CreateDocumentFromFile(@"C:\QA\Test.html");
        var inputFields = doc["input"]; 
        var inputPairs = inputFields.Select(node => new Tuple<string, string>(node.Id, node.Value));
        string pom = String.Empty;
        foreach (var pair in inputPairs)
        {
            var split = pair.Item1.Split('_');
            if (!String.IsNullOrEmpty(pair.Item1.Trim()))
            {
                pom += String.Format("[FindsBy(How = How.Id, Using = \"{0}\")]\r\n", pair.Item1);
                pom += String.Format("public IWebElement {0};\r\n\r\n", split[split.Length - 1]);
            }
        }

        var selectFields = doc["select"]; 
        var selectPairs = selectFields.Select(node => new Tuple<string, string>(node.Id, node.Value));
        foreach (var pair in selectPairs)
        {
            var split = pair.Item1.Split('_');
            var name = split[split.Length - 1];
            if (!String.IsNullOrEmpty(pair.Item1.Trim()))
            {
                pom += String.Format("[FindsBy(How = How.Id, Using = \"{0}\")]\r\n", pair.Item1);
                pom += String.Format(@"public IWebElement {0}Element;
                                        public SelectElement {0}
                                        {{
                                            get {{ return new SelectElement({0}Element); }}
                                        }} {1}{1}{1}", name,Environment.NewLine);
            }
        }
        File.WriteAllText(@"C:\QA\Pom.txt", pom);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.