You are on page 1of 7

Coded UI code Points

1. Open the Browser and insert a URL


System.Diagnostics.Process.Start("iexplore","http://www.google.lk");
2.

Data Source Binding

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "| DataDirectory|\\QAtestdata.csv", "QAtestdata#csv", DataAccessMethod.Sequential), DeploymentItem("TestProject5\\QAtestdata.csv"), TestMethod] public void CodedUITestMethod1() { this.UIMap.FillDatainGmailParams.UIFirstNameEditText= TestContext.DataRow["FName"].ToString(); this.UIMap.FillDatainGmailParams.UILastNameEditText = TestContext.DataRow["LName"].ToString(); this.UIMap.FillDatainGmailParams.UIEmailEditText = TestContext.DataRow["EMAIL"].ToString(); this.UIMap.LoadGmailCreate(); this.UIMap.FillDatainGmail(); } /// <summary> ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///</summary> public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } private TestContext testContextInstance;

public UIMap UIMap { get { if ((this.map == null)) { this.map = new UIMap(); } return this.map; } } If Insert data file(.csv) directly to the source code, you need to set the deployment settings. Test Edit Test Settings Local(local.testsettings) Deployment tick enable deployement Add File select your data file OR

3.

Set flag to run the test even something went wrong( Continue on Error)

Set flag to allow play back to continue if non-essential actions fail. (For example, if a mouse hover action fails.) Playback.PlaybackSettings.ContinueOnError = true; Reset flag to ensure that play back stops if there is an error. Playback.PlaybackSettings.ContinueOnError = false;

4. Match Exact hierarchy


The Record and Playback Engine uses a hierarchical structure to locate controls. The Yes button in the dialog above is identified as

To locate YesButton during playback, the engine will search for AutoCompletePasswordWindow, then for YesWindow in it, and finally YesButton in YesWindow. Sometimes application may change so that the hierarchy gets modified. Record and Playback engine attempts to locate the control, in such cases, by skipping the intermediate elements in the hierarchy. Thus in the example, if it is unable to locate YesWindow, it will search for YesButton directly under AutoCompletePasswordWindow and all its children. This behavior can be controlled by the Match Exact Hierarchy setting. This is done by the following code snippet. Playback.PlaybackSettings.MatchExactHierarchy = true; If the flag is set to true, every control is the hierarchy will be searched in order. Any failure will result in failure to locate the control. By default, this flag is set to false. Note that this may sometimes lead to false positives i.e an unintended control may be identified. In such cases, you will have to change this setting.

5. Search in minimized windows


The Record and Playback Engine searches for controls even inside minimized windows. The minimized window is restored and actions performed on the control inside it. You have the ability to turn this feature off i.e. prevent the engine from looking at minimized windows. This is done by the following code snippet. Playback.PlaybackSettings.SearchInMinimizedWindows = false;

Smart Match
The Record and Playback Engine identifies controls on the user interface by its search properties. e.g:The standard calculator window is identified as below.

SearchProperties[WinProperties.Window.ControlType] = ControlType.Window.Name; SearchProperties[WinProperties.Window.Name] = "Calculator"; SearchProperties[WinProperties.Window.ClassName] = "CalcFrame"; Some of the search properties may change over time. Record and Playback engine uses a Smart Match algorithm to identify controls if they cannot be located using the exact properties. The smart match algorithm uses heuristics and attempts to locate the control using variations on the search properties. You can control when Smart Match should be applied. By default Smart Match is applied for Top Level Windows and all controls. You can turn off Smart match using the following code snippet. Playback.PlaybackSettings.SmartMatchOptions = SmartMatchOptions.None;

The default settings (Smart match top level windows and controls) is optimal for a resilient coded UI test. But sometimes it may lead to false positives and then you will have to modify this setting.

5. Wait For Ready Level


The Record and Playback engine ensures that each UI control is ready to be acted upon before performing any action. The smart Wait For Ready algorithm significantly improves the resilience of your Coded UI Test. Now you dont need to add the annoying Sleep statements whenever a UI Control is busy and not ready to receive input. By default, the engine checks the UI Thread (foreground thread) to determine if a control is ready.

You can turn off Wait For Ready completely by the following code snippet. Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.Disabled;

Alternately you may want to wait for all threads including background threads. This may be required if the application is making asynchronous calls that results in UI changes. You can do this by the following code snippet. Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads; NOTE that this setting will slow down the engine and should be carefully used.

6. Set Property Verification


After setting the property of any UI control, the record and playback engine performs a verification pass to ensure that the set succeeded and the UI control now has the value assigned to it.

e.g:- You type the text MSFT in the search edit box in Bing. After playing back this action, the engine reads the text property of the search edit box to ensure that this set has succeeded.

In certain applications, the set property may trigger off some business logic which then changes that property itself. In such cases, the verification fails. If your application exhibits such a behavior you can turn of the set property verification using the following code snippet. Playback.PlaybackSettings.SkipSetPropertyVerification = true;

7. Fail Fast
The Record and playback engines search algorithm has a fail fast logic. i.e. If the engine is reasonably confident that the search will not succeed, it will return with a failure. This ensures that you are not waiting for a non-existent control for 2 mins (the default search timeout).

Sometimes you may want to disable fail fast. Typically, you do this along with increasing your search timeout. You are confidant that the UI control will become available within a specified duration (which

is longer than the search timeout) and you want the engine to wait for that duration. You can do this by using the following code snippet. Playback.PlaybackSettings.ShouldSearchFailFast = false;

8. Misc
Other playback settings which can be modified include a. Delay Between Actions: How long should the engine wait before performing two successive actions? By default, 100 ms. b. Search timeout How long should the engine attempt to locate a control? By default, 2 mins. c. Wait for Ready Timeout How long should the engine continue waiting for a control to be ready? By Default, 60s d. Think Time multiplier For slow machines, assign a high multiple so that the recorded think time is scaled up to handle slower application responses. e. Send Keys As ScanCode - By default, the engine sends keys to the application under test as Unicode. But in certain cases, it may need to send keys as scancode. Sending Unicode to such applications will result in failure

NOTE: The Test runner tool in VSTT 2010 also uses the Record and Playback Engine. You can configure the playback settings described above in Test runner tool from a. mtlm.exe.config located in %VSINSTALLDIR%\Common7\IDE b. Microsoft Test & Lab Manager, navigate to Lab Center > Test Settings , open the applicable Test Settings, select Data and Diagnostics section and click on the Configure button next to Action Log and Action Recording. In the Advanced section of the dialog, some of the Playback settings described above may be configured.

Possible to add many UIMaps in one project and it will reduce the interdependency between tests
1. Create New folder Right Click Add New Item Select Coded UI Test Map from Add New Item wizard Enter the name and click Add Right click on the Added New UIMap Select Edit with Coded UI Test Builder Record the your test using the tool

2.
3. 4.

5.
6. 7.

Use BHRTestcase and TestRunner to run the code

using System; using System.Collections.Generic; using System.Linq;

using System.Text; namespace BHRTestProject { class TestRunner { public TestRunner() { }

private BHRTestProject.UIMaps.GeographicalInfoClasses.GeographicalInfo asanka = null; public BHRTestProject.UIMaps.GeographicalInfoClasses.GeographicalInfo asankaobject { get { if (asanka == null) { asanka = new UIMaps.GeographicalInfoClasses.GeographicalInfo(); } } } private BHRTestProject.UIMap newasanka = null; public BHRTestProject.UIMap newasankaobject { get { if (newasanka == null) { newasanka = new BHRTestProject.UIMap(); } return newasanka; } } } } return asanka;

using System;

using using using using using using using

System.Text; System.Collections.Generic; System.Linq; Microsoft.VisualStudio.TestTools.UnitTesting; Microsoft.VisualStudio.TestTools.UITesting; Microsoft.VisualStudio.TestTools.UITest.Extension; Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;

namespace BHRTestProject { [CodedUITest] public class BHRTestcase { [TestMethod] public void TestMethod1() { TestRunner testrunner = new TestRunner(); testrunner.asankaobject.RecordedMethod1(); testrunner.newasankaobject.LoadBrowser(); testrunner.newasankaobject.LoadApplication(); testrunner.newasankaobject.RegionSearch(); testrunner.newasankaobject.RegionSearchResultVerify(); } public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } private TestContext testContextInstance; } }