How to: Create Automated UI Test Methods for SharePoint

This topic explains how to use the Microsoft Visual Studio Team System Web test record and playback engine. The following are the basic steps.

  1. Create the scenario before you attempt to automate the test. The scenario outlines the sequence of steps to complete the operation that you are testing.
  2. Record the Web test by following the steps in the scenario, and then save the Web test.
  3. Clean your test environment, and then run the coded test. If your test passes, you can skip step 4.
  4. Create an extraction rule to extract the GUIDs, and then replace the hard-coded GUIDs with the rule. You will receive the exception "There is no context parameter with the name …" when you run the recorded test if a GUID needs to be extracted. There might be more than one GUID that needs to be extracted in a test.
  5. Generate a coded test for the saved test.
  6. Clean your test environment and run the coded test.

Software Requirements

You must have the following software installed to follow the procedures in this topic:

  • Microsoft Visual Studio Team System 2008
  • Windows SharePoint Services (WSS) 3.0 or Microsoft Office SharePoint Server (MOSS) 2007

The procedures in this topic use a SharePoint team site named TestGuidance, which is located at the URL https://localhost/sites/TestGuidance. However, the procedures will work with any other URL location for the team site.

Writing a SharePoint Test Method that Has No GUID to Extract

Use the following steps to create a very simple test method that illustrates the basic concepts involved in creating an automated test for SharePoint.

Step 1: Creating the Scenario

Creating the scenario involves identifying the pages, data, and clicks that you will enter for the test case. You will follow the scenario script when you use the record and playback engine to create the test. For this scenario example, you browse to https://localhost/sites/TestGuidance and verify that a successful response is returned.

Step 2: Recording the Web Test

The following procedure describes how to use Visual Studio Team System to record a test.

To record the test

  1. Start Visual Studio, and then create a test project.

  2. On the Test menu, click New Test. The Add New Test dialog box appears.

  3. Select Web Test.

  4. Type https://localhost/sites/TestGuidance as the URL. This starts the record and playback engine, as show in the following illustration.

    Record and playback engine

    Ff648556.64e395a1-36f6-4463-8d85-eb0e48d4265f(en-us,PandP.10).png

  5. When the browser appears in recording mode, notice that in the Web Test Recorder pane (the left pane) the Record button is selected. Because this scenario is only a single step that retrieves the Web page, click the Stop button (next to the Record button) to stop recording and close Internet Explorer.

  6. On the File menu, click Save to save the Web test.

    Note

    In most scenarios, you will see pop-up messages that contain information about detecting dynamic parameters. However, this test does not generate the messages.

    After processing is finished, the recorded Web test will appear, showing the steps that were recorded. In this scenario, the test will have a single step for viewing the page.

Step 3: Cleaning the Test Environment and Running the Saved Web Test

In this scenario, there is no clean-up required for the test environment. You should be able to run WebTest1.webtest successfully.

Step 4: Creating the Extraction Rule to Extract the GUIDs

The successful test is an indicator that there are no GUIDs to be replaced. Therefore, in this scenario, you do not need to create a GUID extraction rule.

Step 5: Generating the Coded Test and Replacing the GUIDs with extracted value

The following procedure describes how to generate the coded test.

To generate the test code

  1. Open WebTest1.webtest, and click Generate Code as shown in the following illustration. (Alternatively, you can right-click WebTest1.webtest, and then click Generate Code.)

    Generating the coded test

    Ff648556.c8ec7b27-d557-40a0-96f7-d345309aaea9(en-us,PandP.10).png

  2. This produces a C# file named WebTest1Coded, as shown in the following example code.

    public class WebTest1Coded : WebTest
        {
    
            public WebTest1Coded()
            {
                this.PreAuthenticate = true;
            }
    
            public override IEnumerator<WebTestRequest> GetRequestEnumerator()
            {
                // Initialize validation rules that apply to all requests in the WebTest
                if ((this.Context.ValidationLevel >= Microsoft.VisualStudio.TestTools.WebTesting.ValidationLevel.Low))
                {
                    ValidateResponseUrl validationRule1 = new ValidateResponseUrl();
                    this.ValidateResponse += new EventHandler<ValidationEventArgs>(validationRule1.Validate);
                }
    
                WebTestRequest request1 = new WebTestRequest("https://localhost/sites/TestGuidance/default.aspx");
                yield return request1;
                request1 = null;
            }
        }  
    
  3. You can now delete WebTest1.webtest, and use the coded test.

Step 6: Cleaning Your Test Environment and Running the Coded Test

In this scenario, there is no clean-up required for the test environment. You should be able to run the WebTest1Coded test successfully.

Write a SharePoint Test Method That has a GUID to Extract

When sites and other elements are created in SharePoint, they often have GUIDs associated with them. As a result, a recorded Web test might be tied to the GUIDs created for that site or element, and might not work with a different element instance if you run the test again. Examples of elements that create GUIDs are sites, lists, list items, and Web parts. This topic explains how to write a more complex test method to replace the GUIDs in these cases. Dynamic parameters are used to replace the GUIDs at test execution time.

Step 1: Creating the Scenario

The following scenario lists the steps that will be recorded.

To create the scenario

  1. Browse to your TestGuidance team site.
  2. Create a subsite under the TestGuidance team site.
  3. Browse to the new subsite.
  4. Create an announcement.
  5. Stop the test.

Step 2: Recording the Web Test

The following procedure describes how to use Visual Studio Team System to record the test.

To record the test

  1. Start Visual Studio, and then create a test project.

  2. On the Test menu, click New Test. The Add New Test dialog box appears.

  3. Select Web Test.

  4. Name the test WebTest3.

  5. Start the recording, and then perform the scenario steps listed in step 1. Stop recording after you complete the scenario steps. The following illustration shows the test that you created.

    Recorded test example

    Ff648556.b5d3a59f-f924-4514-bfde-45efe3f7dd87(en-us,PandP.10).png

Step 3: Cleaning Your Test Environment and Running the Saved Web Test

Delete the subsite that was created when you recorded the test, and then run WebTest3 again. The test will fail because the GUID is incorrect, as shown in the next illustration.

Test failure caused by invalid GUID

Ff648556.06f55c9f-d503-4326-95aa-d12c687f7eb4(en-us,PandP.10).png

When you run the test again, it creates another site that has a different GUID. The test attempts to use the GUID created for the original site, which causes the test to fail. To correct the test, you have to locate the GUID, extract it, store it in a dynamic parameter, and then create code to replace the GUID in the generated test with the GUID in the dynamic parameter.

Step 4: Creating the Extraction Rule to Extract GUIDs

This step outlines how to identify and create a rule to extract GUIDs.

Before you create the extraction rule, click the error and confirm that that the error was caused by an incorrect GUID. In the example there is a 36-digit string—af382975_fa11_4a83_8427_7e7788c3e483—that represents a GUID from the original site created during the test recording session. If you repeat the steps and create another recorded test, you will see a different GUID value for the site that was created during your recording session.

Gathering the Extraction Information for the Dynamic GUID

The extraction rule is different for different controls. To extract its value, you must find out what the GUID represents. First, locate the failing request in your scenario.

The request fails because it contains a GUID from the instance of the site used during the recording session. You must update the request to use the dynamic parameter that extracts the GUID from the response for the current instance.

Open the HTML response for the page that you submitted before you received the error (see the next illustration). To identify the GUID, you have to extract a GUID for the next request. This value will be different from the GUID described in the error message.

To extract the value of the GUID

  1. Click the WebTest3 tab.

  2. Select the NewForm.aspx file that appears before the failing step, as shown in the following illustration.

    Response and HTML

    Ff648556.a0a02437-3bff-48db-9e7c-2e528d53fe46(en-us,PandP.10).png

  3. Click the Response tab.

  4. Most GUIDs start with $m$g in the response. Click View in HTML editor and search for $m$g. Look for the GUID information that follows so that you know what needs to be extracted. There may be more than one GUID, and you will need to determine the correct GUID to use. You can see the build verification test (BVT) cases included with the guidance for examples of more complex cases with multiple GUIDs. In this example, there is only one GUID. You need to extract the following GUID information:

    • Tag: Input
    • Attribute: type and its value: button
    • Attribute with GUID: name
    • String that contains the GUID

Because the GUID changes each time you run the test, the values that you see will be different from those shown in the following illustration.

GUID information to extract

Ff648556.c4f9e6bd-c0cf-431a-b4f9-0e8724eb5239(en-us,PandP.10).png

Adding Extraction Rules to Your Saved Web Test

After you locate the GUID information, you have to add the extract operation to the Web request. Visual Studio Team System has a feature for extracting any value in a Web response. You can use code or a Visual Studio Team System dialog box to add this capability. In this example, you will use the wizard to add the extraction operations. After you complete the wizard, you can generate the C# file, which will form your final SharePoint test method.

To add the extraction rule

  1. Open the WebTest3.webtest file.

  2. Right-click the page request that occurs just before the failing request, and then click Add Extraction Rule. (See the next illustration.)

    Adding the extraction rule

    Ff648556.186915cc-4402-42ab-b097-ea0e0fa744d9(en-us,PandP.10).png

  3. The Add Extraction Rule wizard appears (see the next illustration). In the Options box, type the Content Parameter Name. In the Parameters box, type the Tag Name, Attribute Name for the GUID, MatchAttributeName, and Match Attribute Value, based on the information you gathered in the previous procedure.

    Add Extraction Rule wizard

    Ff648556.e4e168d3-8720-4de8-810a-33b64d2be82d(en-us,PandP.10).png

  4. Click OK.

    You should now see the extract operation attached to the Web test, as shown in the following illustration.

    Extract operation added to Web test

    Ff648556.4e1f117b-1fb4-4676-8a98-95b2bbd5bd8f(en-us,PandP.10).png

Step 5: Generating the Coded Test and Replacing the GUIDs with Extracted Values

The following procedure describes how to generate the coded test from the original recorded Web test and then replace the GUID with the extracted values from the dynamic parameter.

To generate the test code

  1. Open the WebTest3.webtest file, and then click Generate Code as shown in the following illustration. (Alternatively, you can right-click WebTest3, and then click Generate Code.)

    Generating the coded Web test

    Ff648556.7c0cd5c8-95d1-477a-8902-c13736b803ed(en-us,PandP.10).png

    This produces a C# file named WebTest3Coded.

  2. In the C# file, locate the Extract object by searching for the context parameter name. The example used the context parameter name TestValueGUID when the extraction rule was created. See the following illustration.

    Locating the extraction object

    Ff648556.ba29a203-250f-4946-ba5b-31a0267d1867(en-us,PandP.10).png

  3. Create a GUID parameter to capture the extracted dynamic GUID from the extracted context parameter, as shown in the following example.

    string TestValueGUID = Context["TestValueGUID"].ToString();
                //Now extract the GUID the string
                int i = TestValueGUID.IndexOf('_') + 1; //Note the underscore, not dash
                TestValueGUID = TestValueGUID.Substring(i, 36);
    

    The test framework runtime extracts the complete GUID string from the response and puts it into the context. The following illustration shows the code example above inserted in the test code to save the extracted GUID in the variable TestValueGUID.

    GUID parameter inserted in the test code

    Ff648556.0ba9da45-a778-4739-80e2-6c954657921a(en-us,PandP.10).png

  4. Locate the hard-coded GUID value in the C# code for the failing request.

  5. Find the GUID value that needs to be replaced by the dynamic parameter value from the code you created in step 3. To do this, look at the error information for the failed request. In the example, the GUID is the 36-digit string af382975_fa11_4a83_8427_7e7788c3e483.

  6. Locate the numeric strings in the C# code, as shown in the following illustration.

    Numeric strings in C# code

    Ff648556.f80870c9-547a-47bc-a982-e277f7bf00cd(en-us,PandP.10).png

  7. Replace the GUID value af382975_fa11_4a83_8427_7e7788c3e483 with the GUID parameter string**“+TestValueGuid.Replace(“-“,”_”).ToString()+”** as shown in the following illustration.

    Replacing the GUID value

    Ff648556.c383f2ac-688e-4d48-bece-47900316398d(en-us,PandP.10).png

  8. Save the files and then build the solution. Make sure that it compiles successfully.

Step 6: Cleaning Your Test Environment and Running the Coded Test

This section describes how to clean your test environment before you run the coded test.

To clean the environment

  1. Delete all generated subsites under your test team site.
  2. Open the Test List View, and then select the C# test method that you just created.
  3. Run the test. It should be successful.
  4. If the test fails, check the following:
    • Have you edited and extracted the GUID correctly?
    • Have you missed a GUID value?
  5. If the test still fails, repeat the steps to replace the GUID value dynamically.

Home page on MSDN | Community site