Wednesday 17 December 2014

How to schedule Microsoft coded UI test execution without microsoft TFS

  1. Create a release of stable coded UI project by following steps
  1. Open  Build > Batch build
  1. Click rebuild if a release was already built


  1. After successfully building coded UI project navigate to release folder of project and copy testAutomated.dll named with your project name
  1. Place this dll on remote machine where you want to schedule your test execution
  1. Place .dll in some directory of remote machine e.g I am placing in D:\coadedUitest


  1. Install Miscosoft Test agent from this link on this machine where you want to execute test . http://www.microsoft.com/en-us/download/details.aspx?id=38186
  1. After installing test agent verify MS test is present in location  
  1. Open note pad and enter following text
D:
cd coadedUitest
set mstestPath="C:\Program Files\Microsoft Visual Studio 12.0\Common7\IDE"
%mstestpath%\mstest /testcontainer:testAutomated.dll  




8. Save it with .bat extension



  1. Open .bat file by double clicking it to verify tests are executed or not
  1. After successful execution open windows task scheduler and create a scheduled task to run this created .bat file.

    Tuesday 11 November 2014

    Possible Exceptions and their cause in Coded UI


    Here are some of the guidelines to troubleshoot the failure during recording/playback.

     

    Recording Time Failures


    Possible ExceptionCauseComment
    TechnologyNotSupportedExceptionRecoding on a unsupported technology AppAs mentioned above Winforms/WPF/Web are three different kind of technologies supported by UITest so if user tries to record on some other technology we fail with the following exception.
    ApplicationFromNetworkShareExceptionRecording on a Network share applicationThis occurs when the recording is being done on a network share App.


    Playback time failures

    Playback does two major things in order to perform action on the control search and action on the control. Following are the possible reasons and their exceptions.

    Search Failure


    Following are some of the suggested tips to debug the search failure during playback.

    Possible ExceptionCauseComment
    UITestControlNotFoundExceptionThe control does not exist or search properties are not enough to find the controlUser can use AccExplorer and see whether the search properties and the hierarchy are correct
    UITestControlNotAvailableExceptionThe control does not exist in the app now.If the control has been invalidated or deleted during the playback, the window handle and other properties related to it would be invalid then.

    Action Failure


    Sometime though the search passes but the playback may not be able to do the action on the control. Following may be the possible reason/tips to debug the issue

    Possible ExceptionCauseComment/Possible workarounds
    ActionNotSupportedOnDisabled
    ControlException
    Playback is trying to perform action on a disabled controlThis is thrown when the playback try to do some action on the disabled control/read only controls. Following are some of the cases:
    · SetValue on a read only combobox/text box
    · Click/SetState on buttons/checkboxes
    FailedToLaunchApplicationExceptionWhen the playback fails to launch the AuTVerify whether the LaunchApp was recorded on the original exes or on some other file e.g., sometime if a App launches SPLASH screen before the original AuT LaunchApp is recorded on splash screen, user can work around this in API playback by changing the path
    FailedToPerformActionOnBlocked
    ControlException
    The control exists but not actionableThrown when a control is blocked in this case though the search passes but the required action will not be played back. Following might be some of the causes for this exception
    · The control is not visible in the view port and not scrollable even
    · Control is blocked due to some modal dialog on top of it
    DecodingFailedExceptionMay come in password text boxWhen the decoding fails due to incorrect key value in password encryption.
    PlaybackFailureExceptionDefault fall back exception for uncategorized issuesFor any other failure the playback engine would throw PlaybackFailureException. For example if Get/Set of some property fails for a control playback engine would throw PlaybackFailureException.

    Monday 10 November 2014

    Search Configurations for the control in CodedUI


    Apart from the control specific properties, some other properties may be required to identify the control, e.g., if the control is nameless or the parent has to be expended before looking for a control e.g, tree item. Following are some of those.

    Search configuration of the control


    We have some predefined search configuration which helps to narrow down the search space (search only in visible controls) or to do perform some prerequisite before actually starting the search (expand parent tree node before searching for the child node). Following are the different search configs:

    SearchConfig ParameterDescription
    AlwaysSearchUITest uses a cache while doing actions on an Application by adding Always Search in the search config user can force UITest to not to use the cached value for the control.
    DisambiguateSearchIf the parent and the controls properties are same there are chances that UITest would start doing action on the parent itself. This config can be used to ask the playback to act on its child rather than the parent itself.
    ExpandWhileSearchingExpand the control before looking for the other control inside it. E.g., TreeView
    NextSiblingSearch in the siblings inside the container. Sometime if the control is nameless we may iterate from the named control inside the same container to reach the control.
    VisibleOnlySearch only in the visible control. It helps to reduce the search space.

    Sunday 2 November 2014

    Resizing browser window using CodedUI


    How to test your websites responsive UI design with codedUI.
    The main requirement here is that you would be able to check if you can enter values on a specific screen in different browsers in different window sizes.
    So starting the browser in CodedUI is simple, you just use the BrowserWindow class and call the Launch method to start the browser in a reliable way. Starting different browsers (Chrome, firefox) is also possible by just setting theBrowserWindow.CurrentBrowser to a value of “chrome” or “firefox” so that is already solved.
    The main issue is the size of the browser window. For the responsive UI to show, you need to run a test case in e.g. different screen sizes. Unfortunately there is no method on the BrowserWindow class to set the screen size of the browser.
    But there is an easy fix for that.
    The BrowserWIndow class has a property called WindowHandle. We can use this property to call a windows API to resize the window.
    First an extension method if to be defined as below.
        public static class BrowserWindowExtensions
        {
            [DllImport("user32.dll", SetLastError = true)]
            [return: MarshalAs(UnmanagedType.Bool)]
            internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hwndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
            public static void ResizeWindow(this BrowserWindow control, int width, int height)
            {
                SetWindowPos(control.WindowHandle, (IntPtr)(-1), 0, 0, width, height, 0x0002 | 0x0040);
            }
        }
    then the TestMethod looks as shown below
            [TestMethod]
            public void BrowserResizing()
            {
                //define for Chrome
                BrowserWindow.CurrentBrowser = "Chrome";
                BrowserWindow myb = BrowserWindow.Launch();
                myb.NavigateToUrl(new System.Uri("http://bing.com/"));
                myb.Maximized = true;
                BrowserWindowExtensions.ResizeWindow(myb, 480, 800);
            }
    the same is applicable for IE and Firefox.

    Friday 31 October 2014

    Multiple browser tabs implementation using CodedUI


    While i was trying to open multiple sites in same browser in different tabs, it was bit tricky.

    I was trying it the way as shown below and was successful in opening a new tab, however i was lauching the site in a new browser.

                      //launching Bing.com
    BrowserWindow myBrowser = BrowserWindow.Launch();
    myBrowser.NavigateToUrl(new System.Uri("http://bing.com/"));

                     //Clicking on new Tab button

    WinTabList tabList = new WinTabList(myBrowser);
    WinButton newTabButton = new WinButton(tabList);
    newTabButton.SearchProperties[WinButton.PropertyNames.Name] = "New Tab (Ctrl+T)";
    Mouse.Click(newTabButton);

                   //launching yahoo.com

    myBrowser.NavigateToUrl(new System.Uri("http://yahoo.com/"));

                  //Clicking on new Tab button
    Mouse.Click(newTabButton);


                 //launching google.com

    myBrowser.NavigateToUrl(new System.Uri("http://google.com/"));


    By using the above code, there is a browser opened and two tabs opened, but all three sites are opened in the first tab itself as shown below





    the code is successful to open the new tabs but was unable to the to launch the url's in the tabs which were open.


    i modified the code and tried finding the new tab and launch the url and even it resulted the same

    BrowserWindow myb = BrowserWindow.Launch();
    myb.NavigateToUrl(new System.Uri("http://bing.com/"));

    WinTabList tabList = new WinTabList(myb);

    WinButton newTabButton = new WinButton(tabList);
    newTabButton.SearchProperties[WinButton.PropertyNames.Name] = "New Tab (Ctrl+T)";
    Mouse.Click(newTabButton);

                     // Finding the new tab opening

    myb.SearchProperties[UITestControl.PropertyNames.Name] = "New Tab";
    myb.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";


    myb.NavigateToUrl(new System.Uri("http://yahoo.com/"));


    Mouse.Click(newTabButton);


    myb.SearchProperties[UITestControl.PropertyNames.Name] = "New Tab";

    myb.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
    myb.NavigateToUrl(new System.Uri("http://google.com/"));


    Finally the code was modified as below to make it working.

    BrowserWindow myBrowser = BrowserWindow.Launch();
    myBrowser.NavigateToUrl(new System.Uri("http://bing.com/"));


    proc = myBrowser.Process;

    WinTabList tabList = new WinTabList(myBrowser);
    WinButton newTabButton = new WinButton(tabList);
    newTabButton.SearchProperties[WinButton.PropertyNames.Name] = "New Tab (Ctrl+T)";
    Mouse.Click(newTabButton);


    myBrowser = BrowserWindow.FromProcess(proc);

    myBrowser.SearchProperties[UITestControl.PropertyNames.Name] = "New Tab";
    myBrowser.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
    myBrowser.NavigateToUrl(new System.Uri("http://yahoo.com/"));

    Mouse.Click(newTabButton);

    myBrowser = BrowserWindow.FromProcess(proc);
    myBrowser.SearchProperties[UITestControl.PropertyNames.Name] = "New Tab";
    myBrowser.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
    myBrowser.NavigateToUrl(new System.Uri("http://google.com/"));



    happy coding :)

    Check this How to close a particular tab in a browser with multiple tabs

    Close a specific Tab in browser with Multiple tabs



    This is a little continuation of the previous post.

    Multiple browser tabs implementation using CodedUI

    So the scenario is a browser opened with three tabs, each one with Bing, yahoo and google opened.

    Now if i wanted to close the specific browser tab in it.

    this is how u do it

    public void CloseTab(string tabName)
            {
                BrowserWindow openBrowser = new BrowserWindow();
                Playback.PlaybackSettings.MatchExactHierarchy = true;
                openBrowser.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame";
                WinTabList openBrowserTabList = new WinTabList(openBrowser);
                openBrowserTabList.SearchProperties[WinTabList.PropertyNames.Name] = "Tab Row";
                WinTabPage myTabPage = new WinTabPage(openBrowserTabList);
                myTabPage.SearchProperties[WinTabPage.PropertyNames.Name] = tabName;
                Mouse.Click(myTabPage);
                //myTabPage.SetFocus();
                //myTabPage.DrawHighlight();
                WinButton close = new WinButton(myTabPage);
                close.SearchProperties.Add(WinButton.PropertyNames.Name, "Close Tab", PropertyExpressionOperator.Contains);
                //close.SearchProperties[WinButton.PropertyNames.Name] = "Close Tab (Ctrl+W)";
                Mouse.Click(close);
            }



    Happy coding :)

    Sunday 26 October 2014

    CodedUI playback on HtmlTable control


    This post is to share some insights in playback performance on HTML tables in your application.
    A problem that we generally encounter is the extreme long wait times when we want to read data from a cell in a HTM Table. So the main thing I would like to point out here is the performance implications of not completely understanding how CodedUI gets the actual control from the browser and the fact that using a property of e.g. the HtmlTable control can significantly impact your playback performance. this is due to the fact that the property can constantly evaluate it’s value each time you call the property getter.
    To show the differences, you can see a simple test page, where it has a table with 10 columns and 100 rows and each cell in the table has a different value. Now my UI test wants to pick out a cell and capture the value of that cell and assert that value. ( the red arrow pointing to an arbitrary cell I want to read the value from.
    image
    So the basic approach to getting this value would be, get the correct column index of the column we are interested in, next lookup the correct row that we are interested in and finally retrieve the value from the cell that we found.
    Lets see the three approaches, starting with the code generally followed in actual projects that had an performance problem.
    So we can do this in 3 different ways all having different performance characteristics.

    The naive approach

    So lets take the first approach found in our codebase. Here  you create a loop where we loop through the header columns and match the cell.FriendlyValue of that column to the name of the column I am interested in and then keep that index when found. Next we do the same but now for the rows and we do the lookup in the cell with index 0. When we found the row we capture the value. Now in this approach we use the call to Htmltable.Getcel(rowIndex, ColumnIndex), that will return the cell and then we can get the value from the cell. The code is here below.
    int nIndex = 0;
    for (nIndex = 0; nIndex < table.ColumnCount; nIndex++)
    {
        var headerCell = table.GetCell(0, nIndex);
        if (headerCell.FriendlyName.Contains("Lookupcolumn9"))
        {
            break; // nIndex is the column index we are looking for
        }
    }
    string lookupValue="";
    int nRowIndex = 0;
    for (nRowIndex = 0; nRowIndex < table.Rows.Count; nRowIndex++)
    {
        var controlCell = table.GetCell(nRowIndex, 0);
        if (controlCell.FriendlyName.Contains("rowLookup85"))
        {
            var cell = table.GetCell(nRowIndex, nIndex);
            lookupValue = cell.FriendlyName;
            break;
        }
    }
    
    If you run this code, you will notice it is pretty slow in playback and there is a statement in this code that is incurring performance impact while you probably will never notice just based on looking at the code. If you look at the red highlighted line, you see a call in the loop to table.Rows.Count. When you call the getter of this property this will invoke a call to the browser DOM. this means we do this  each time we iterate through the loop. the reason for the re-evaluation is that some javascript might have modified the table in the meanwhile and therefor it is re-evaluated each and every time we call the getter. So one simple performance improvement would be to cache the property local and use that as the test condition in the loop. The next thing that is incedibly slow, is the call to GetCell.
    Running the above code against the test page costs 160443 milliseconds to complete, so that is about 2.6 minutes to find that singe cell we are looking for.

    Using cached control values

    Now the second approach would be to use a foreach loop on the columns and the cells in stead of using an index into the rows and the columns. Now why is that a better approach you might ask yourself? Well when you use foreach, you are using an iterator over a collection. the basic principal of an iterator is that the collection it iterates over is fixed during the iteration. so that means the values are all cached. to show you the code, have a look here below:
    string lookupValue = "";
    // find the index of the column we want to lookup in a row
    int columnIndex = -1;
    foreach (HtmlCell header in ((HtmlRow)(table.Rows[0])).Cells)
    {
        if (header.FriendlyName.Contains("Lookupcolumn9"))
        {
            columnIndex = header.ColumnIndex;
            break;
        }
    }
    
    foreach (HtmlRow row in table.Rows)
    {
    
        if (row.Cells[0].FriendlyName.Contains("rowLookup85"))
        {
            // get the value and return
            lookupValue = row.Cells[columnIndex].FriendlyName;
            break;
        }
    }
    
    
    When I execute this test, this results in resolving the right cell in about 28 seconds. So just using a foreach in stead of the for loop saves a lot of time. Now the actual culprit of the problem is more or less that we just can’t tell which properties are cached value and which are not. But just using the iterator will ensure you iterate over a fixed collection.

    Leveraging CodedUI control search

    This algorithm still has a big problem in my opinion. The problem is that if I want to get the first column out of the first row, this call function would return pretty fast, since all loops only execute once. But when I want the last column from the last row, my performance degrades tremendous!
    So the third approach you can take is just by leveraging the search capabilities of CodedUI yourself. What i mean by that is that we are going to create a HtmlCell control and control it’s search properties to find the right header column right away and from that we determine the index that this cell has in the row, Next we create an HtmlRow control, and use it’s search properties to find the row that contains the value we are looking for, identifying the right row. The last step s to take the FriendlyName of the cell with the index we found in the previous search. The code below shows how this is done:
    HtmlCell directcell = new HtmlCell(browser);
    directcell.SearchProperties.Add(new PropertyExpression(HtmlCell.PropertyNames.InnerText,
     "Lookupcolumn9", PropertyExpressionOperator.Contains));
    int cellIndex = directcell.ColumnIndex;
    
    HtmlRow directRow = new HtmlRow(browser);
    directRow.SearchProperties.Add(new PropertyExpression(HtmlRow.PropertyNames.InnerText, 
     "rowLookup85", PropertyExpressionOperator.Contains));
    var lookupValue = directRow.Cells[cellIndex].FriendlyName;
    
    
    
    The big advantage of this approach is that we leverage the capabilities of codedUI to find the stuff we are looking for and not employing our own algorithm to find stuff in the table. Next advantage is that this algorithm will have the same performance regardless of the cell we are looking for and finally the performance of this approach is incredible faster then the previous approaches.

    CodedUI for CrossBrowser testing


    For testing any web application, it would naturally be best to test it with all of the most popular browsers.
    It’s easy to feel like you’re doing the same thing over and over again. What works in one browser doesn’t always work in other browsers. As a result, you end up in testing every single feature on all the major browsers that easily multiplies the time required for QA. Manually testing cross browser is time consuming and tedious process that is inefficient and conflicts with Today’s short development cycles. Hence the functional automation written once should be liable to run in all supporting browsers
    In this post, I’ll demonstrate how this problem might be resolved in a simple way by creating coded UI test cases that will execute against any modern browser, using only C#.
    Coded UI, inbuilt supports Internet Explorer.
    To run the same functional tests in other browsers using Coded UI tests against Chrome or Firefox using the Selenium cross-browser plugin and this is how you do it

    1. Download the Cross-browser plugin for Coded UI Test that uses Selenium to run tests against Chrome and Firefox from here: Selenium cross-Browser plugin
    image

    2.  After installing the Cross-Browser testing installer, verify that
    Microsoft.VisualStudio.TestTools.UITest.Extension.CrossBrowserProxy.dll
    is found in the following location :
    “%ProgramFiles%\Common Files\microsoft shared\VSTT\Cross BrowserSelenium Components” (for 32 bit machines)
    “%ProgramFiles(x86)%\Common Files\microsoft shared\VSTT\Cross BrowserSelenium Components” (for 64 bit machines)
    • Make sure to have all these file in the above mentioned path :
      3
    3. Record a Coded UI test in IE
    image


    image

    4. Change the browser to run the same test against Chrome using the BrowserWindow object i.e.
    BrowserWindow.CurrentBrowser = "Chrome";   //For Chrome
    BrowserWindow.CurrentBrowser = "FireFox";   //For Chrome
    image

    Web browsers evolve continuously and you may not realize when this happens. Sometimes this means certain features aren’t available in the new browser version, which in turn causes some tests to fail, even if they were passed previously. That’s why it’s important to disable automatic browser updates and wait until the new version is supported by the Selenium components for Coded UI Cross-Browser Testing. Otherwise, unexpected exceptions might occur during run time.

    Note:
    • Currently, Coded UI supports Firefox 25.0.1 version
    • Chrome 21 Plus version
    • IE9 /IE10/IE11
    • Apple Safari is not supported
    • The action of starting the web browser must be part of Coded UI Test. If you have a web browser already open and you want to run test on it, the playback will fail  unless you are using Internet explorer. Therefore it is a best practice to include the startup of browser as part of Coded UI Tests.

    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:
    Input1
    Input2
    ExpectedResult
    3
    4
    7
    5
    6
    11
    6
    8
    14
    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.  
          this.UIMap.AddNumbers(); 
      
      } 
    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 = 
          TestContext.DataRow["Input1"].ToString();
      this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
          TestContext.DataRow["Input2"].ToString();
      
    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 = 
          TestContext.DataRow["ExpectedResult"].ToString();
      
      This is how the coded UI test method should look with the parameters and the data source added to it:
      [DeploymentItem("DataDriven.csv"), 
          DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", 
              "|DataDirectory|\\DataDriven.csv", "DataDriven#csv", 
              DataAccessMethod.Sequential), 
          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.
          this.UIMap.AddTwoNumbersParams.TextInput1EditText = 
              TestContext.DataRow["Input1"].ToString();
          this.UIMap.AddTwoNumbersParams.TextInput2EditText = 
              TestContext.DataRow["Input2"].ToString();
          this.UIMap.AddTwoNumbers();
      
          this.UIMap.AssertforAddExpectedValues.TextAnswerEditText = 
              TestContext.DataRow["ExpectedResult"].ToString();
          this.UIMap.AssertforAdd();
      }
      


    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.