Showing posts with label Coded UI. Show all posts
Showing posts with label Coded UI. Show all posts

Wednesday, February 13, 2013

Descriptive Programming in CODEDUI Visual Studio Ultimate 2010

Descriptive Programming in CODEDUI Visual Studio Ultimate 2010

Code to press button "3" from the Calculator UI of Windows XP.


1. Open Visual Studio 2010 > Select New project > Select "Test Project" from Visual C# ,Test
   You should be able to see a Blank workspace created ,
2. On your right side in properties you will have Unittest1  Created .Select that file from the tree right click and delete.

3. Click on New Template >Select Coded UI>Ok

4.Delete any pre-exsisting code from the workspace and Copy the below and paste in the workspace.

Lets discuss the code :

//calculator should already be open
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using System.Threading;

///////////////////////// MAIN CLASS
[CodedUITest]
public class CodedUITest1
{
    private UICalculatorWindow mUICalculatorWindow;//Object Declaration ---1
    public CodedUITest1()//Constructor-----2
    {
    }
    [TestMethod]
    public void CodedUITestMethod1() //Similar to Main ()
    {
        Thread.Sleep(1000); /*//Declared simply ,can be commented ([using System.Threading;])*/
        this.UICalculatorWindow.UIItem3Window.UIItem3Button.SearchProperties[WinButton.PropertyNames.Name] = "3";
  

        Mouse.Click(this.UICalculatorWindow.UIItem3Window.UIItem3Button, new Point(18, 18));
    }
    public UICalculatorWindow UICalculatorWindow //Setting Property for Main Window -----4 
    {
        get
        {
            mUICalculatorWindow = new UICalculatorWindow();
            return mUICalculatorWindow;
        }
    }
}
 ///////////////////////////////////////////////////Class 2

public class UICalculatorWindow : WinWindow //Parent Window
{
    private UIItem3Window mUIItem3Window;/*
Child Window Object*/
    public UICalculatorWindow()  //Constructor for Main Window  
    {
        this.SearchProperties[WinWindow.PropertyNames.Name] = "Calculator";
        this.SearchProperties[WinWindow.PropertyNames.ClassName] = "SciCalc";
    }
    public UIItem3Window UIItem3Window //Property for Child Window
    {
        get
        {
            mUIItem3Window = new UIItem3Window(this);
            return mUIItem3Window;
        }
    }
}
 ////////////////////////////////////////////////////////////////////Class 3

public class UIItem3Window : WinWindow  /*Child Window Class ,inhereting WinWindow properties */
{
    private WinButton mUIItem3Button;//Object Declaration
    public UIItem3Window(UITestControl searchLimitContainer) //Constructor
    {
        this.SearchProperties[WinWindow.PropertyNames.ControlId] = "127";// child window
    }
    public WinButton UIItem3Button  //Property For Button
    {
        get
        {
            mUIItem3Button = new WinButton(this); 
            return mUIItem3Button;
        }
    }
}
 /////////////////////////////////////////////////////////////////////////////////////////////////////END

The above code "press button 3" on the Windows XP Calculator using technique similar to descriptive programming in QTP.

1) The button 3 is actually inside 2 Windows .
  1. "UICalculatorWindow"  is the parent window
  2. "UIItem3Window"  is the child window
  3. UIItem3Button  is button "3" is present inside the child Window .
2) A seperate class has been created for parent window object and child window object whose properties has been initialised .

3)For the button object  the properties has been initialsed directly in the Main function which is ( public void CodedUITestMethod1() )
Remeber (Main Function in coded UI is "public void CodedUITestMethod1() " meaning the execution starts from there and also all the operation is written here).

4)The Logic in CUI as far as my understanding goes is something like this :
Ex : Lets say "W" is a Window and "B" is a button inside the Window "W" which we need to Click.
Therefore B is a child of W.

5) We need to create 2 Classes :-
   (1) is the Main Class (i.e.,  public void CodedUITestMethod1() ) and (2) is the Object Definition for the parent Class in our case "W" .

(1)  which is the Main Class contains
  • Declaration of Parent Window Object "W".
  • Constructor for the class
  • Initialzation of property for the button "B" and to click the button.
  • Assigning Property value to the Parent Window Object "W".
(2) is the Parent Window Object Class  contains .
  • Declaration of Parent Window Object "B".
  • Constructor for the class
  • Initialzation of "W" properties.
  • Assigning Property value to the Button Object "B".

Remember :Each Parent Class will have its child object declared inside it and Property call  for the child!!