Sunday, 21 September 2014

Windows 8, Win+I key combination

In Windows 8, Win+I key combination is used to bring up the Settings Charms.

So in Visual Studio 11 Coded UI Test, we now use Ctrl+I to locate controls. 
NOTE: This shortcut key and its behavior is present only Coded UI Test Builder is in “Add Assertions” mode.
Also NOTE: The change is applicable for Visual Studio 11 on all Operating systems. (not just Win8)

You can modify this key combination now in Coded UI Test Builder.exe.config

(%ProgramFiles%\Microsoft Visual studio11.0\Common7\IDE\CodedUITestBuilder.exe.config)

In the appSettings section, add the following keys. (Replace with Modifier & Key that you want)
<add key="FetchModifierKey" value="Control"/>
<add key="FetchKey" value="I"/>

Data Driven Coded UI Tests

After you have created a coded UI test, you can use the steps in the following procedure to add your data source and parameters to your test. This example assumes that you have these code elements in your solution:
  • A coded UI test class named CodedUITest1.
  • A test method named CodedUITestMethod1().
The test method is for a simple calculator application that adds two numbers and verifies that they are added together correctly for this test to pass.
The data source is a .csv file that contains the following data:
Create the file and add it to your test project.
  1. After adding the data, the file should appear as the following:
    Populate the .CSV file with data
  2. It is important to save the .csv file using the correct encoding. On the FILE menu, choose Advanced Save Options and choose Unicode (UTF-8 without signature) – Codepage 65001 as the encoding.
  3. The .csv file, must be copied to the output directory, or the test can’t run. Use the Properties window to copy it.
    Deploy the .CSV file
    Now that we have the data set created, let’s bind the data to the test.

Create a Data-Driven Coded UI Test

To create a data-driven coded UI test

  1. To bind the data source, add a DataSource attribute within the existing [TestMethod] attribute that is immediately above the test method.

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod]  
    public void CodedUITestMethod1()  
        // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.  
  2. The data source is now available for you to use in this test method
  3. In Solution Explorer, open the CodedUITest1.cs file. Make the following changes to the CodedUITestMethod1() method:

  4. Add the following two lines of code before the call to the AddTwoNumbers method to provide values for the numbers to add.
    this.UIMap.AddTwoNumbersParams.TextInput1EditText = 
    this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
  5. Add the following line of code before the call to the AssertforAdd method to provide the value for the assert method.
    this.UIMap.AssertforAddExpectedValues.TextAnswerEditText = 
    This is how the coded UI test method should look with the parameters and the data source added to it:
            "|DataDirectory|\\DataDriven.csv", "DataDriven#csv", 
    public void CodedUITestMethod1()
        // To generate code for this test, select "Generate Code for 
        // Coded UI Test" from the shortcut menu and select one of 
        // the menu items.
        this.UIMap.AddTwoNumbersParams.TextInput1EditText = 
        this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
        this.UIMap.AssertforAddExpectedValues.TextAnswerEditText = 

  6. Save the changes to the CodedUITest1.cs source code file.
  7. To run your coded UI test, right-click the coded UI test in the Test View window and click Run Selection.
    After the tests have run, the overall test result for all iterations of the test displays in the Test Results window. To see the details of each iteration, double-click the test in the Test Results window.

Monday, 1 September 2014

Mouse hovers in CodedUI

Manually recording mouse hovers
Under some circumstances, a particular control that’s being used in a coded UI test might require you to use the keyboard to manually record mouse hover events. For example, when you test a Windows Form or a Windows Presentation Foundation (WPF) application, there might be custom code. Or, there might be special behavior defined for hovering over a control, such as a tree node expanding when a user hovers over it. To test circumstances like these, you have to manually notify the Coded UI Test Builder that you are hovering over the control by pressing predefined keyboard keys.
When you perform your coded UI test, hover over the control. Then press and hold Ctrl, while you press and hold the Shift and R keys on your keyboard. Release the keys. A mouse hover event is recorded by the Coded UT Test Builder.
After you generate the test method, code similar to the following example will be added to the UIMap.Desinger.cs file:
// Mouse hover '1' label at (87, 9)
Mouse.Hover(uIItem1Text, new Point(87, 9));
Configuring mouse hover keyboard assignments
If necessary, the default keyboard assignment of Ctrl+Shift+R that is used to apply mouse hovering events in your coded UI tests can be configured to use different keys.
To change the keyboard assignments, you must modify the following configuration file:
<drive letter:>\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CodedUITestBuilder.exe.config
In the configuration file, change the values for the HoverKeyModifier and HoverKey keys to modify the keyboard assignments:
<!-- Begin : Background Recorder Settings -->
<!-- HoverKey to use. -->
<add key="HoverKeyModifier" value="Control, Shift"/>
<add key="HoverKey" value="R"/>
If having issues with recording mouse hovers on a website, then here Is the fix
Setting implicit mouse hovers for the web browser
In many websites, when you hover over a particular control, it expands to show additional details. Generally, these look like menus in desktop applications. Because this is a common pattern, coded UI tests enable implicit hovers for Web browsing. For example, if you record hovers in Internet Explorer, an event is fired. These events can lead to redundant hovers getting recorded. Because of this, implicit hovers are recorded with ContinueOnError set to true in the UI test configuration file. This allows playback to continue if a hover event fails.
To enable the recording of implicit hovers in a Web browser, open the configuration file:
<drive letter:>\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CodedUITestBuilder.exe.config
Verify that the configuration file has the key RecordImplicitiHovers set to a to a value of true as shown in the following sample:
<!--Use this to enable/disable recording of implicit hovers.-->
<add key="RecordImplicitHover" value="true"/>