示例#1
0
        protected virtual string DetermineName(PageObjectDefinition pageObject, SeleniumGeneratorContext context)
        {
            // if selector is set, just read it and don't add data context prefixes
            //var shouldAddDataContextPrefixes = uniqueName == null;
            var shouldAddDataContextPrefixes = true;

            // if not found, use the name properties to determine the name

            string uniqueName = null;

            foreach (var nameProperty in NameProperties)
            {
                uniqueName = SelectorStringHelper.TryGetNameFromProperty(context.Control, nameProperty);
                if (uniqueName != null)
                {
                    uniqueName = SelectorStringHelper.NormalizeUniqueName(uniqueName);
                    break;
                }
            }

            // if not found, try to use the content of the control to determine the name
            if (uniqueName == null && CanUseControlContentForName)
            {
                uniqueName = SelectorStringHelper.GetTextFromContent(context.Control.Content);
            }

            // check if control is userControl and assign control's name as unique name
            if (uniqueName == null && context.Control.DothtmlNode is DothtmlElementNode htmlNode)
            {
                uniqueName = htmlNode.TagName;

                // not add DataContext when generating page object for user control
                shouldAddDataContextPrefixes = false;
            }

            // if not found, use control name
            if (uniqueName == null)
            {
                uniqueName = typeof(TControl).Name;
            }

            if (shouldAddDataContextPrefixes)
            {
                uniqueName = SelectorStringHelper.AddDataContextPrefixesToName(pageObject.DataContextPrefixes, uniqueName);
            }

            return(uniqueName);
        }
        public void SeleniumGenerator_TestAddDataContextPrefixesToName()
        {
            // initialize
            var dataContextPrefixes = new List <string> {
                "MyPage", "FirstTab", "Address"
            };
            var uniqueName = "City";

            // do
            var uniqueNameWithDataContextPrefixes = SelectorStringHelper.AddDataContextPrefixesToName(dataContextPrefixes, uniqueName);

            // assert
            var expectedResult = "MyPage_FirstTab_Address_City";

            Assert.AreEqual(expectedResult, uniqueNameWithDataContextPrefixes);
        }