PageElement.cs :  » Web-Testing » IeUnit-web-testing-framework » QuickFocus » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Web Testing » IeUnit web testing framework 
IeUnit web testing framework » QuickFocus » PageElement.cs
/// <copyright from="2004" to="2006" company="VisuMap Technologies Inc.">
///   Copyright (C) VisuMap Technologies Inc.
/// 
///   Permission to use, copy, modify, distribute and sell this 
///   software and its documentation for any purpose is hereby 
///   granted without fee, provided that the above copyright notice 
///   appear in all copies and that both that copyright notice and 
///   this permission notice appear in supporting documentation. 
///   VisuMap Technologies Company makes no representations about the 
///   suitability of this software for any purpose. It is provided 
///   "as is" without explicit or implied warranty. 
/// </copyright>
using System;
using System.Collections;
using mshtml;

namespace QuickFocus{
  /// <summary>
  /// Summary description for PageElement.
  /// </summary>
    public class PageElement {
        protected IHTMLElement  e;
        protected ArrayList     snipList = new ArrayList();
        private  bool           inSubmitMode; // indicate whether the user is want submit block

        protected PageElement(IHTMLElement e) {
            this.inSubmitMode = false;
            this.e = e;
        }

        public bool InSubmitMode  {
            set { inSubmitMode = value; }
            get { return inSubmitMode; }
        }

        public void ClearSnipList() {
            snipList.Clear();
        }

        public virtual ArrayList ActionSnipList() {
            return snipList;
        }

        public virtual ArrayList AssertSnipList() {
            return snipList;
        }

        protected void AddSnip(string snip) {
            snipList.Add(snip + "\n");
        }

        protected void InsertSnip(string snip) {
            snipList.Insert(0, snip + "\n");
        }

        protected void AssertObjWithId() {
            if ( (e.id != null) && IsUnique(e.id) ) {
                if ( e.innerText != null ) { 
                    AddSnip("_.assertTextContains(_.findObjById(\"" + e.id 
                        + "\"), \""  + EscapeString(MakeKeyString(e.innerText)) + "\");");
                } else {
                    AddSnip("_.assertNotNull(_.findObjById(\"" + e.id + "\"));");
                }
            }

            string name = ElementName;
            if (  (name != null) && IsUnique(name) ) {
                AddSnip("_.assertNotNull(_.findObjById(\"" + name + "\"));");
            }
        }

        protected void ClickObjWithId() {
            if ( (e.id != null) && IsUnique(e.id) ) {
                AddSnip("_.clickObjById(\"" + e.id + "\");");
            }

            string name = ElementName;
            if (  (name != null) && IsUnique(name) ) {
                AddSnip("_.clickObjById(\"" + name + "\");");
            }
        }

        //
        // DHTML modles doesn't grant that the name or id is uniqueu.
        // In many case we just issue id or name related statement 
        // if the id or name is unique since IeUnit doesn't support the no-unique
        // name or id.
        //
        protected bool IsUnique(string idOrName) {
            if ( idOrName == null ) {
                return false;
            }
            object obj = MainForm.SelectedDoc.all.item(idOrName,1);
            return (obj == null) ? true : false;
        }

        protected void AssertTagHasText(string keyText) {
            // If nothing else is available we just check the page as a whole.
            string tag = e.tagName;
            IHTMLElementCollection eList = MainForm.SelectedDoc.all.tags(tag) as IHTMLElementCollection;
            IEnumerator it = eList.GetEnumerator();
            int idx = -1;
            while ( it.MoveNext() ) {
                if ( it.Current != null ) {
                    idx++;
                }
                if ( this.e.Equals(it.Current) ) {
                    break;
                }
            }
            if (idx >= 0) {
                AddSnip("_.assertTagHasText(\"" + tag + "\"," + idx + ",\"" + keyText + "\");");
            } else {
                AddSnip("_.assertPageHasText(\"" + keyText + "\");");
            }
        }

        public static PageElement ToPageElement(IHTMLElement e) {
            PageElement pe;

            if ( e == null ) {
                return null;
            }

            pe = Button.AsButton(e);            if ( pe != null ) { return pe; }
            pe = InputText.AsInputText(e);      if ( pe != null ) { return pe;}
            pe = ImageElement.AsImageElement(e);if ( pe != null ) { return pe; }
            pe = AnchorLink.AsAnchorLink(e);    if ( pe != null ) { return pe; }
            pe = RadioButton.AsRadioButton(e);  if ( pe != null ) { return pe; }
            pe = CheckBox.AsCheckBox(e);        if ( pe != null ) { return pe; }
            pe = SelectOption.AsSelectOption(e);if ( pe != null ) { return pe; }
            pe = SelectOption.AsSelectOption(e);if ( pe != null ) { return pe; }
            pe = TableElement.AsTableElement(e);if ( pe != null ) { return pe; }
            pe = MapArea.AsMapArea(e);          if ( pe != null ) { return pe; }
            pe = TextArea.AsTextArea(e);        if ( pe != null ) { return pe; }
            pe = ForeignFrame.AsForeignFrame(e);if ( pe != null ) { return pe; }

            // TextBlock will catch all element with non-null innerText property.
            pe = TextBlock.AsTextBlock(e);      if ( pe != null ) { return pe; }
            
            // For any other element we just return a generic PageElement object
            return new PageElement(e);
        }

        /// <summary>
        /// Find an element in the parent chain with given tag name upto give level.
        /// </summary>
        /// <param name="e"></param>
        /// <param name="tagName"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        protected static IHTMLElement FindParentOfTag(IHTMLElement e, string tagName, int level) {
            if ( e == null ) { 
                return null;
            }

            for (int i=0; i<level; i++) {
                if ( e.tagName == tagName ) {
                    break;
                }
                e = e.parentElement;
                if ( e == null ){
                    break;
                }
            }

            if ( e==null) {
                return null;
            }

            if (e.tagName == tagName) {
                return e;
            } else {
                return null;
            }
        }

        
        /// <summary>
        /// Find the closest parent that has an id up
        /// to given level.
        /// </summary>
        /// <param name="e"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        public IHTMLElement FindParentWithId(IHTMLElement e, int level) {
            if ( e == null ) {
                return null;
            }

            for(int i=0; i<level; i++) {
                if ( e.id != null ) {
                    return e;
                } 
                e = e.parentElement;
                if ( e == null ) {
                    return null;
                }
            }
            return null;
        }

        /// <summary>
        /// Extract characteristics key string from a raw text string.
        /// </summary>
        /// <param name="rawString"></param>
        /// <returns></returns>
        protected string MakeKeyString(string rawStr) {            
            if ( rawStr == null ) {
                return null;
            }

            rawStr = rawStr.Trim(' ', '\t', '\n','\r');
            return rawStr.Substring(0, 
                Math.Min(Root.appConfig.maxKeyTextLength, rawStr.Length));            
        }

        internal static string EscapeString(string str) {
            if ( str == null ) {
                return null;
            }
            string retStr = str.Replace("\"", "\\\"");
            retStr = retStr.Replace("\r", "\\r");
            retStr = retStr.Replace("\n", "\\n");
            return retStr;
        }

        protected string ElementName {
            get {
                if (e is HTMLInputElementClass ) {
                    return (e as HTMLInputElementClass).name;
                } else if ( e is HTMLSelectElementClass ) {
                    return (e as HTMLSelectElementClass).name;
                } else {
                    return null;
                }
            } 
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.