Pages

HOME

HOME

Tuesday, 30 October 2012

How to increase the test result file storage limit in CodedUI

In CodedUI the Test Result folder is created automatically. The "Test Result" is in a folder in which the solution file is located(by default).

The default file storage limit is 25 files. The files are stored in .trx format which is nothing but an XML file.

We can increase the limit as described below in 5 steps:
1. Click on "Tools". Select "Options" menu item.



 

2. "Options" pop up will be shown. Click on "Test Tools" menu item from the list.
3.  Click on "Test execution" sub menu-item.




4. Enter the integer value i.e. the number of files you want to save in Test result folder.



5. If you want to see the detail reason (if script fails) on double click on test result we need to check the checkbox.



That's it.

Enjoy Coding!!!

Saturday, 27 October 2012

How to add Timeout test settings in CodedUI

Today we will see how to add Timeout settings in CodedUI test project. It's simple just 5 steps procedure.

1. Right click on "Solution Items" folder. Select "Add" >> New Item.



2. Click on "Test Settings" templates.And click on Add button.



3. The Test settings template will be loaded. In this click on "Test Timeouts". 


4. In Test Timeouts settings we can set the timeouts for:
 a) Abort test if it exceeds the given time limit.
 b) Mark test as Failed if it exceeds the given time limit.

Enter the timeout values.



5. Click on Apply button and then Click on Close button.

The settings will be applied to the all test cases. In this way we can add Timeout settings in Test project.
# If you want to edit the timeout again then double click on "*---.testsettings" file available in "Solution Items" folder and follow the steps 3,4,5.



Enjoy Coding!!!








Tuesday, 23 October 2012

How to check Visibility of a Control (UITestControl) in CodedUI


Today we will see how to check if control is Visible or not on the User Interface. In CodedUI the UITestControl does not provide "Visible" property directly but it has some very useful methods.By using these methods we can determine/find the existence of the control on the User Interface.This method we have write in UIMap.cs file.
This is explained as below:

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Input;
    using System.CodeDom.Compiler;
    using System.Text.RegularExpressions;
    using Microsoft.VisualStudio.TestTools.UITest.Extension;
    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
    using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse;
    using MouseButtons = System.Windows.Forms.MouseButtons;
    using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
    
    
public partial class UIMap
  {
        //To check if control is visible or not
        public void VerifyControlVisible()
        {
            //Namespace need to include :
            //using Microsoft.VisualStudio.TestTools.UITesting;
            //using Microsoft.VisualStudio.TestTools.UITest.Extension;          

            WinText ctrlToTest = this.UICalculatorWindow.UIItem0Window.UIItem0Text;
            
//UITestControl.Exists-- METHOD CHECKS IF THE CONTROL EXIST OR NOT on User Interface.
          
          if (!ctrlToTest.Exists)
            {
                //If control is not visible or does not exist then throw exeception
                throw new AssertInconclusiveException("The given control is not visible or does not exist.");
            }

            //****UITestControl.TryFind()-- METHOD CHECKS IF THE CONTROL 
            //EXIST OR NOT on User Interface.
            //This method is MORE RELIABLE as compared to ctrlToTest.Exists . 
            //So we can get more reliable result from TryFind method 
           
           if(!ctrlToTest.TryFind())
            {
                //If control is not visible or does not exist then TryFind() will return false 
                //hence we can throw exeception
                throw new AssertInconclusiveException("The given control is not visible or does not exist.");
            }

            //Another alternative approach is using UITestControl.TryGetClickablePoint
            //This method finds the control on the User Interface and get the Clickable
            //point(i.e. position or the co-ordinates of the control)
            //Hence TryGetClickablePoint method also more reliable than the  
           //UITestControl.Exists method.
            
            Point tmpPoint;
            
            if(!ctrlToTest.TryGetClickablePoint(out tmpPoint))
            {
                //If control is not visible or does not exist then TryGetClickablePoint()
               //will return false hence we can throw exeception
                throw new AssertInconclusiveException("The given control is not visible or does not exist.");
            }

        }
    }

These are very helpful methods to find the control existence.


Enjoy Coding!!!




Monday, 15 October 2012

Creating Ordered Test in codedUI (How to create Ordered test)

Today we will see how to execute the automated test scripts in Batch. In CodedUI  we can create and execute the batch of Unit tests using Ordered test.

To create ordered test please follow the below steps:

1. Open CodedUI TestProject.
2. Click on Test tab >> New test.





3. Select Ordered Test 

-- We can give name to ordered test in Test name text box and click on OK button.

4.Then we need to select the unit test from list of available tests in the project.



5. In the Left side pane we can see the available tests.Either by double click on individual test or by selecting test and then click on ">" button will add the test to the ordered test list.To remove the test from "selected tests"- select the test from "selected tests"  list click on "<".



6.Now the very important point is to check the "continue after failure" checkbox.This ensures that if any test get failed during the ordered test execution, then the next test will be executed from the selected list.e.g In given example if "checkAddition.." test fails then the "CheckMultiplication.." will be executed. 



7. Now click on Save icon to Save the ordered test.


8. Now click on Run test and then results will be generated.
To Execute ordered test click on Test >>Windows>>Test List Editor



9. select the ordered test from the list and click on Run ordered test icon. OR by right click on the selected ordered test and select "Run checked tests"




10. The result will be shown as below:




That's it!!! .

Saturday, 11 August 2012

How/When to :Use UITestControl.WaitFor..() method in CodedUI


Today we will see how to use the different Playback.Wait() methods provided in CodedUI.While automating the scripts you may come across scenario like some controls are visible after some actions performed on other controls.viz. On click of search button the the respective gridview is shown(or visible on page). In such cases the control is availability depends on action performed on some other controls; you may face issue like "ControlNotFound()" exception. To overcome or handle such scenario CodedUI provides some Playback.Wait() or UITestControl.WaitForControl....() methods.Let's see one by one the different methods and their purpose.

1.WaitForControlReady :This method Waits for the control to be ready to accept mouse and keyboard input. The engine implicitly calls this API for all actions to wait for the control to be ready before doing any action on it. However, some time in some cases, you may have to call it explicitly.
UITestControl UiTestCtrl = new UITestControl();
                if (UiTestCtrl.WaitForControlReady())
                {
                    Mouse.Click(UiTestCtrl);
                }
Also this method has 1 overload method to give the timeout as input in milliseconds. Default timeout is 60000 i.e. 1 minute
UITestControl UiTestCtrl = new UITestControl();
//Here the timeout is set to 5 seconds
                if (UiTestCtrl.WaitForControlReady(5000))
                {
                    Mouse.Click(UiTestCtrl);
                }


2.WaitForControlEnabledWaits for the control to be enabled. For example, when you want to wait for control to be enabled which is depend on server response then we can use this method to wait. 
UITestControl UiTestCtrl = new UITestControl();
                if (UiTestCtrl. WaitForControlEnabled())
                {
                    Mouse.Click(UiTestCtrl);
                }
Also this method has 1 overload method to give the timeout as input in milliseconds. Default timeout is 60000 i.e. 1 minute
UITestControl UiTestCtrl = new UITestControl();
//Here the timeout is set to 5 seconds
                if (UiTestCtrl. WaitForControlEnabled(5000))
                {
                    Mouse.Click(UiTestCtrl);
                }


3.WaitForControlExists:Waits for the control to appear on User Interface. e.g. you are expecting an error message after you have done the validation of the parameters. The time taken for validation is not fixed. We can use this method to wait for the error message dialog box.
UITestControl UiTestCtrl = new UITestControl();
                if (UiTestCtrl. WaitForControlExist())
                {
                    Mouse.Click(UiTestCtrl);
                }
Also this method has 1 overload method to give the timeout as input in milliseconds. Default timeout is 60000 i.e. 1 minute
UITestControl UiTestCtrl = new UITestControl();
//Here the timeout is set to 5 seconds
                if (UiTestCtrl. WaitForControlExist(5000))
                {
                    Mouse.Click(UiTestCtrl);
                }


4.WaitForControlNotExists :Waits for the control to disappear from the User Interface(Form/web page). As shown above similarly we can validate for WaitForControlNotExists.
5.WaitForControlPropertyEqual(string,Object):This method waits for the specified property of the control to have the given value. For example if we have windows calculator and we want to check 1+2= 3 is true or false.
[TestMethod]
        public void CheckAdditionFunctionality()
        {
            try
            {
                //add using Microsoft.VisualStudio.TestTools.UITesting.WinControls; namespace
                LaunchCalculator();
                //Here playback.wait() method added just for understanding purpose for first time user..
                //to understand the actions automated in slow
                WinButton btn1 = this.UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button;
                WinButton btn2 = this.UIMap.UICalculatorWindow.UIItemWindow1.UIItem2Button;
                WinButton btnAdd = this.UIMap.UICalculatorWindow.UIItemWindow2.UIAddButton;
                WinButton btnEquals = this.UIMap.UICalculatorWindow.UIItemWindow3.UIEqualsButton;
                WinText txtResult = this.UIMap.UICalculatorWindow.UIItem0Window.UIItem0Text;
                Mouse.Click(btn1);
               // Playback.Wait(10000);
                Mouse.Click(btnAdd);
                //Playback.Wait(10000);
                Mouse.Click(btn2);
                Mouse.Click(btnAdd);
              bool isTrue=txtResult.WaitForControlPropertyEqual("DisplayText", "3");
                       //we can explicitly add wait in milliseconds
// txtResult.WaitForControlPropertyEqual("DisplayText", "3",5000);
                    if (txtResult)
                    {
                        //True.
                    }
                    else{
                        //false.
                    }
              }
            catch(Exception e) {
                throw new AssertInconclusiveException(e.Message.ToString());
            }

        }

 6.WaitForControlPropertyNotEqual(string,Object) :Waits for the specified property of the control to have the opposite of a specified value. For example, you wait for the edit box to be not read-only, that is, editable.
 //Here it is given for illustration only
      bool isTrue=txtResult.WaitForControlPropertyNotEqual("ReadOnly", " true ");
                       //we can explicitly add wait in milliseconds
// txtResult.WaitForControlPropertyEqual("ReadOnly ", "true",5000);
                    if (txtResult)
                    {
                        //True.
                    }
                    else{
                        //false.
                    }
  

7.WaitForControlCondition(Predicate<UITestControl>):Waits for the specified predicate returns to be true. This can be used for complex wait operation (like OR conditions) on a given control. For example, you can wait until the status text is or as shown in the following code:

// Define the method to evaluate the condition

private static bool IsOperationSucceed(UITestControl control)
{
        return txtResult.DisplayText == "3" || txtResult.DisplayText == "2";
}

// In test method, wait till the method evaluates to true
txtResult.WaitForControlCondition(IsOperationSucceed);




8.WaitForCondition<T>(T,Predicate<T>): All the above methods are instance methods of UITestControl. While this method is a static method. This method also waits for the specified predicate to be true. e.g. We can wait until the Result text is or until an error/exception message displayed.

// Define the method to evaluate the condition
private static bool IsOperationSucceedOrError(UITestControl[] controls)
{
    
    WinWindow errorDialog = controls[1] as WinWindow;
    return txtResult.DisplayText == "2" || errorDialog.Exists;
}

// In test method, wait till the method evaluates to true
UITestControl.WaitForCondition<UITestControl[]>(newUITestControl[] { txtResult, errorDialog }, IsOperationSucceedOrError);

Other links:
1.How to create CodedUITest
2.How to use Playback.wait()



Friday, 27 July 2012

How/When :To use Playback.Wait() method in CodedUI

The CodedUI provides one useful method when we want to wait for sometime till we get some operation(e.g. action - like onclick of submit button if some sql query get fired and then success message we show on some label at that time we have to wait for sometime ) completed  then we can call the Playback.Wait(int thinkTimeMilliseconds) explicitly.The Playback.Wait() internally calls Thread.Sleep().


This method is under :
Namespace:  Microsoft.VisualStudio.TestTools.UITesting
Assembly:  Microsoft.VisualStudio.TestTools.UITesting (in Microsoft.VisualStudio.TestTools.UITesting.dll)

The properties supported by Playback class are :
Name
Description
IsInitialized
This property indicates whether playback has been initialized.
IsSessionStarted
This property indicates whether the playback session has started.
PlaybackSettings
This property gets the current playback settings.


The methods supported by Playback class are:
Method Name
Description
Initialize
This method initializes playback for the UITest.
Wait
This method makes the playback pause for a specified period of time.
Cancel
This method cancels the playback of the current operation.
Cleanup
This method performs clean up after the UITest playback.
StartSession
This method starts a new session for playback.
StopSession
This method stops the current playback session.
EncryptText
This method ncrypts a given text for passing to playback as password property.
Example:


Please ref How to Create CodedUI test for the background for the below method.

   [TestMethod]

   public void CheckAdditionFunctionality()

   {

       try

         {

                //add using Microsoft.VisualStudio.TestTools.UITesting.WinControls; namespace

                LaunchCalculator();

                //Here playback.wait() method added just for understanding purpose for first time user..

                //to understand the actions automated in slow

         WinButton btn1 = this.UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button;

         WinButton btn2 = this.UIMap.UICalculatorWindow.UIItemWindow1.UIItem2Button;

         WinButton btnAdd = this.UIMap.UICalculatorWindow.UIItemWindow2.UIAddButton;    
WinButton btnEquals = this.UIMap.UICalculatorWindow.UIItemWindow3.UIEqualsButton;
WinText txtResult = this.UIMap.UICalculatorWindow.UIItem0Window.UIItem0Text;

                Mouse.Click(btn1);
                //Playback.Wait(5000);

                Mouse.Click(btnAdd);
                //Playback.Wait(5000);

                Mouse.Click(btn2);
                // Playback.Wait(5000);

                Mouse.Click(btnAdd);

                //playback will wait for 10000 thinkTimeMilliseconds

                Playback.Initialize();
                Playback.Wait(10000);

                if (txtResult.DisplayText.Equals("3"))

                {

                    MessageBox.Show("Passed");

                }

                else {

                    MessageBox.Show("Failed");

                }

            }

                           catch(Exception e) {

                throw new AssertInconclusiveException(e.Message.ToString());

            }

           finally{
                 Playback.Cleanup();
            }
}