Tuesday, August 28, 2018

CodedUI - Generic method for identify control




using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITesting.HtmlControls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public HtmlEdit txtProgramCodeEdit
        {
            get
            {

                {
                    if ((this.mtxtProgramCodeEdit == null))
                    {
                        this.mtxtProgramCodeEdit = new HtmlEdit(this);
                    }

                    return mtxtProgramCodeEdit;
                }
            }
        }

        private BrowserWindow mParentWindow { get; set; }

        public enum PropertyType
        {
            Name,
            Id,
            ClassName,
            ControlType
        }



        public UITestControl ParentBrowserWindowInstance { get; private set; }

  public void EnterText<T>(PropertyType type, string propertyvalue, string text) where T : HtmlControl
        {
         //   HtmlEdit ht = new HtmlEdit(ParentWindow);

            HtmlControl genericControl = (T)Activator.CreateInstance(typeof(T), new object[] { ParentWindow });
            if (type == PropertyType.Name)
                genericControl.SearchProperties[HtmlControl.PropertyNames.Name] = propertyvalue;
            else if (type == PropertyType.Id)
                genericControl.SearchProperties[HtmlControl.PropertyNames.Id] = propertyvalue;
            //Type in the UI
            Keyboard.SendKeys(genericControl, text);
        }

        public void Click(object controlType, PropertyType type, string propertyvalue)
        {
            if (controlType is HtmlButton)
            {
                HtmlButton hbtn = new HtmlButton(ParentWindow);

                if (type == PropertyType.Name)
                {
                    hbtn.SearchProperties[HtmlButton.PropertyNames.Name] = propertyvalue;
                }
                else if (type == PropertyType.Id)
                {
                    hbtn.SearchProperties[HtmlButton.PropertyNames.Id] = propertyvalue;
                    hbtn.DrawHighlight();

                   
                }
                hbtn.DrawHighlight();
                Mouse.Click(hbtn);
            }
        }

        public BrowserWindow TopParentWindow()
        {
            BrowserWindow br = new BrowserWindow();
            //br.SearchProperties[UITestControl.PropertyNames.Name] = BrowserWindow.CurrentBrowser.ToString();
            br.SearchProperties[UITestControl.PropertyNames.Name] = "CUSTOMER CONTRACT MAINTENANCE";
            return br;
        }

        public BrowserWindow ParentWindow
        {
            get
            {
                if (this.mParentWindow == null)
                {
                    this.mParentWindow = TopParentWindow();
                }
                return this.mParentWindow;
            }
        }
=================
 cr.EnterText(PropertyType.Id, "txtCustomerNo", "12345");
                cr.Click(new HtmlButton(), PropertyType.Id, "btnSave110");

No comments:

Post a Comment