QaTestCase.cs :  » Web-Testing » IeUnit-web-testing-framework » QuickFocus » QA » 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 » QA » QaTestCase.cs
#if QA_TEST
using System;
using VisuMap.AppTest;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using mshtml;
using System.IO;

using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Collections;
using FormCheckBoxSystem.Windows.Forms.CheckBox;

namespace QuickFocus.QA{
  /// <summary>
  /// Base TestCase to hold commonly used methods.
  /// </summary>
  public class QaTestCase : TestCase
  {
        protected string homeDir = @"C:\dev\IeUnit\";
        private FormChecker appChecker;

        public QaTestCase() {
            appChecker = new FormChecker(Root.mainForm);
        }

        #region Find Methods
        protected Control FindControl(string ctrlName) {
            return AppChecker.FindControl(ctrlName);
        }

        protected Control FindControlByTag(string tag) {
            return AppChecker.FindControlByTag(tag);
        }

        protected IHTMLElement2 FindElementById(string id) {
            return CurrentDoc.all.item(id, 0) as IHTMLElement2;
        }

        protected ArrayList FindElementsByTag(string tagName) {
            ArrayList retList = new ArrayList();
            IHTMLElementCollection elements = CurrentDoc.all.tags(tagName) as IHTMLElementCollection;
            foreach(object obj in elements) {
                retList.Add(obj);
            }
            return retList;
        }

        protected string AppStatus {
            get {
                return (FindControl("statusBar") as StatusBar).Text;
            }
        }

        #endregion


        #region Assert methods

        protected void AssertIsHelloPage() {
            AxSHDocVw.AxWebBrowser browser = FindControlByTag("browserMain") as AxSHDocVw.AxWebBrowser;
            IHTMLDocument2 doc = browser.Document as IHTMLDocument2;
            AssertEquals( "Hello World!", doc.body.innerText);
            AssertEquals("Hello World", doc.title);
        }

        protected void AssertPageTitle(string title) {
            AxSHDocVw.AxWebBrowser browser = FindControlByTag("browserMain") as AxSHDocVw.AxWebBrowser;
            IHTMLDocument2 doc = browser.Document as IHTMLDocument2;
            AssertEquals(title, doc.title);
        }

        protected void AssertPageHasText(string txt) {
            string pageText = CurrentDoc.body.innerText;
            if ( pageText.IndexOf ( txt ) < 0 ) {
                AssertFail("The page doesn't contain " + txt );
            }
        }

        protected void AssertSnipHas(string txt){
            string snip = SnippetText;
            if ( snip.IndexOf(txt) < 0) {
                AssertFail("The snip code \"" + snip + "\" doesn't contain " + txt );
            }
        }

        protected void AssertSnipStartsWith(string txt){
            string snip = SnippetText;
            if (! snip.StartsWith(txt) ) {
                AssertFail("The snip code \"" + snip + "\" doesn't start with \"" + txt + "\"");
            }
        }

        protected void AssertStatusHas(string txt) {
            string status = AppStatus;
            if ( status.IndexOf(txt) < 0) {
                AssertFail("The status text \"" + status + "\" doesn't contain " + txt );
            }
        }

        protected void AssertWaitStatusHas(string txt) {
            WaitForStatus(txt);
            AssertStatusHas(txt);
        }

        #endregion

        protected void SetFlags(bool assertFlag, bool actionFlag, bool altFlag,
            bool commentAltFlag, bool autoClearFlag, bool simulationFlag) 
        {
            Invoke( delegate() {
                (FindControl("cbAssertSnip") as FormCheckBox).Checked = assertFlag;
                (FindControl("cbActionSnip") as FormCheckBox).Checked = actionFlag;
                (FindControl("cbShowAlternatives") as FormCheckBox).Checked = altFlag;
                Root.appConfig.autoClear = autoClearFlag;
                Root.appConfig.simulationMode = simulationFlag;
                Root.appConfig.commentOutAlt = commentAltFlag;
            });
        }

        protected void SetDefaultFlags() {
            SetFlags(true, true, true, true, false, false);
        }
        protected FormChecker AppChecker {
            get { return appChecker; }
        }

        protected void WaitForStatus(string msg) {
            for(int i=0; i<150; i++) {
                if (AppStatus.IndexOf(msg) >= 0 ) {
                    break;
                }
                Wait(200);
            }
        }

        protected void ClearSnipWindow() {
            Invoke(delegate() {
                Root.mainForm.ClearSnipWin();
                WaitMin();
            });
        }

        protected void OpenUrl(string url) {
            ComboBox cBox = FindControl("cmbbUrl") as ComboBox;
            AppChecker.SetText(cBox, url);
            AppChecker.ClickButtonByName("btnGo");
            Application.DoEvents();
            WaitMin();
        }

        protected void OpenSampleFile(string fileName) {
            string filePath = homeDir + @"tool\QuickFocus\samples\" + fileName;
            if ( File.Exists(filePath) ) {
                OpenUrl(filePath);
            } else {
                filePath = homeDir + @"samples\ApiTest\" + fileName;
                if ( File.Exists(filePath) ) {
                    OpenUrl(filePath);
                } else {
                    MsgBox.Alert("Sample file " + fileName + " not found");
                    return;
                }
            }

            // Wait till the page has loaded or attached.
            for(int i=0; i<300; i++) {
                if ( (Root.mainForm.TheState == AppState.StOpen) 
                    || (Root.mainForm.TheState == AppState.StAttached) ){
                    break;
                }
                Wait(100);
            }
        }

        protected void AttachSampleFile(string fileName) {
            OpenSampleFile(fileName);
            AttachPage();
        }


        protected void AttachPage() {
            for(int i=0; i<100; i++) {
                if ( Root.mainForm.TheState == AppState.StAttached ) {
                    return;
                }
                if ( Root.mainForm.TheState == AppState.StOpen ) {
                    AppChecker.ClickButtonByName("btnAttach");
                }
                WaitMin();
            }
        }

        protected void DetachPage() {
            for(int i=0; i<100; i++) {
                if ( Root.mainForm.TheState == AppState.StOpen ) {
                    return;
                }
                if ( Root.mainForm.TheState == AppState.StAttached ) {
                    AppChecker.ClickButtonByName("btnAttach");
                }
                Wait(100);
            }
        }

        public void Wait(int milliseconds) {
            Thread.Sleep(milliseconds);
        }

        public void WaitMin() {
            Thread.Sleep(FormChecker.MinWaitTimeMS);
        }

        private IHTMLDocument2 CurrentDoc {
            get { return MainForm.Browser.Document as IHTMLDocument2; }
        }

        protected void MoveMouseToElement(IHTMLElement2 e2) {
            IHTMLRect       rect    = e2.getBoundingClientRect();
            MoveToPoint( (rect.left + rect.right)/2, (rect.top + rect.bottom)/2 );
            WaitMin();
        }

        protected void ClickElement(IHTMLElement2 e) {
            MoveMouseToElement( e );
            UnsafeNativeMethods.ClickMouse(MouseButtons.Left);
            Application.DoEvents();
            WaitMin();
        }

        protected void MoveToPoint(int x, int y) {                
            Invoke( delegate() {
                Cursor.Position = MainForm.Browser.PointToScreen(new Point(x, y));                
            });
            Application.DoEvents();
        }

        protected void ClickPoint(int x, int y) {
            MoveToPoint(x, y);
            WaitMin();
            UnsafeNativeMethods.ClickMouse(MouseButtons.Left);
            Application.DoEvents();
            WaitMin();
        }

        protected void ClickAreaElement(IHTMLElement2 img, IHTMLElement2 area) {
            IHTMLRect rectImg = img.getBoundingClientRect();
            IHTMLRect rectArea = area.getBoundingClientRect();

            MoveToPoint(
                (rectArea.left + rectArea.right)/2 + img.clientLeft + rectImg.left,
                (rectArea.top + rectArea.bottom)/2 + img.clientTop + rectImg.top );
            WaitMin();
            UnsafeNativeMethods.ClickMouse(MouseButtons.Left);
            Application.DoEvents();
            WaitMin();
        }

        protected void ClickElementById(string id) {
            IHTMLElement2 e = FindElementById(id);
            ClickElement( e );
        }

        public void AppendSnip(string snip) {
            RichTextBox rtb = AppChecker.FindControl("rtbSnipWin") as RichTextBox;
            Invoke( delegate() { rtb.Text = rtb.Text + "\n" + snip; });
        }

        public string SnippetText {
            get {
                RichTextBox rtb = AppChecker.FindControl("rtbSnipWin") as RichTextBox;
                string retText = null;
                Invoke(delegate() { retText = rtb.Text; });
                return retText;
            }
            set {
                RichTextBox rtb = AppChecker.FindControl("rtbSnipWin") as RichTextBox;
                Invoke( delegate() {
                    rtb.Text = value;
                    rtb.SelectionStart = rtb.Text.Length;
                    rtb.SelectionLength = 0;
                });
            }
        }

        /// <summary>
        /// Shortcut to simplify Form.Invoke() call.
        /// </summary>
        public void Invoke(MethodInvokerVoid method) {
            AppChecker.Invoke(method);
        }
    }
}

#endif
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.