prerequisitesdownload.microsoft.com/download/6/2/b/62b60ece-b9dc-4e8a... · web viewin this...

39
Hands-On Lab Testing and Debugging SharePoint Applications with Visual Studio 2015 Lab version: 14.0.23107.0 Last updated: 8/14/2015

Upload: phungkhanh

Post on 09-Mar-2018

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Hands-On LabTesting and Debugging SharePoint Applications with Visual Studio 2015Lab version: 14.0.23107.0

Last updated: 8/14/2015

Page 2: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

CONTENTS

OVERVIEW................................................................................................................................................. 3

EXERCISE 1: CREATING UNIT TESTS USING THE SHAREPOINT EMULATOR...................................5

EXERCISE 2: INTELLITRACE SUPPORT FOR SHAREPOINT..............................................................18

EXERCISE 3: WEB AND LOAD TESTING SHAREPOINT APPLICATIONS...........................................26

Page 3: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Overview

In this lab, you will learn about some of the features provided with Visual Studio 2015 that make testing and debugging SharePoint applications easier, thereby helping to improve the quality and scalability of your product.

Page 4: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Prerequisites

In order to complete this lab you will need the Visual Studio 2015 virtual machine provided by Microsoft. For more information on acquiring and using this virtual machine, please see this blog post.

Exercises

This hands-on lab includes the following exercises:

1. Creating Unit Tests using the SharePoint Emulator

2. IntelliTrace Support for SharePoint

3. Web and Load Testing SharePoint Applications

Estimated time to complete this lab: 60 minutes.

Page 5: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Exercise 1: Creating Unit Tests using the SharePoint Emulator

In this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit tests in order to help remove unnecessary dependencies to SharePoint and the SharePoint API, thereby isolating your tests and making sure that you are testing the code you want.

Note: Please note that in the virtual machine you may notice delays when attempting to open the team project portal while the necessary SharePoint services are started for the first time, and you may notice timeouts or unexpected errors.

In most cases, you can keep refreshing the page until it loads. Note that it may take a few minutes depending on the performance of the hardware you are hosting this virtual machine on. If after a few minutes it still doesn’t load, please confirm that you have configured this virtual machine according to the proper setup instructions. This includes ensuring that you have at least one network adapter installed and configured (an internal network adapter is recommended).

1. Log in as Adam (VSALM\Adam). All user passwords are P2ssw0rd.

2. Launch Visual Studio 2015 from the taskbar and then open the AppointmentsWebPart solution from C:\SharePointDemos\AppointmentsWebPart (File | Open | Project/Solution…).

Page 6: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 1Loading sample solution

Note: The taskbar link for Visual Studio has been configured to ‘run as administrator’, which is necessary for some operations such as deploying to SharePoint.

3. This solution contains a web part that allows a user to book an appointment. Let’s take a look at it in action to get an idea of what it does. Right-click on the AppointmentsWebPart project in Solution Explorer and select Deploy to deploy and activate the feature on the local SharePoint server.

Figure 2Deploying the web part

Page 7: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

4. After successful deployment, open Internet Explorer and navigate to the SharePoint site at http://vsalm.

Figure 3SharePoint site

5. Select the Edit button to edit the page.

Figure 4Edit button location

6. Place the cursor at the beginning of the page that is now in edit mode. It may be helpful to use the arrow keys in addition to the mouse.

Page 8: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 5Selecting insert location

7. Select the Insert tab.

Figure 6Insert tab location

8. Select the Web Part button to add in the custom web part.

Figure 7

Page 9: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Web Part button

9. Select the Custom category, the AppointmentsWebPart and then select the Add button to add it to the page.

Figure 8Adding in the web part

10. Select the Save button.

Page 10: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 9Save and close

11. The web part expects there to be a list named Appointments. To create one, select the gear icon from the top-right and then select the ‘Add an app’ option.

Page 11: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 10Navigate to Lists

12. Select the Appointments app tile.

Figure 11Create Appointments app

13. Enter a name of Appointments and select the Create button.

Page 12: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 12Creating Appointments app

14. Return to the home view for the site. One way to do this is to select the Home link from the left-hand navigation menu.

Figure 13Returning to home view

15. Manually test the appointment web part by entering some test data into the fields and then selecting the Submit button.

Page 13: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 14Testing the Appointments web part

16. Return to the Appointments app (look underneath the Recent node) and note that the appointment has been added as expected.

Figure 15New appointment

17. Now let’s take a look at how we can take advantage of the SharePoint Emulator and Microsoft Fakes Framework to develop and execute unit tests. The SharePoint Emulator code base is installed via NuGet. Return to Visual Studio, right-click on the AppointmentsWebPart.Tests project and select the Manage NuGet Packages option.

Page 14: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 16Manage NuGet packages

18. Filter on Installed packages and note that the Microsoft.SharePoint.Emulators package is already installed for the test project. The package includes the needed assemblies and adds the appropriate references to the project for you.

Figure 17Microsoft.SharePoint.Emulators already added to project

19. Close the NuGet Package Manager window.

Page 15: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

20. Load the UnitTest1.cs file from the AppointmentsWebPart.Tests project and navigate to the first test method that starts with “ScheduleAppointment…” This test method uses the SharePoint Emulator to create a test list, add fields to it, and then use the web part to test the scheduling of an appointment.

Figure 18Unit test definition

21. Most of what you see looks identical to normal SharePoint code, however it is all wrapped in a Using statement with an instance of SharePointEmulationScope. This is responsible for redirecting normal SharePoint calls to the shims provided by the emulator.

Figure 19Use of SharePointEmulationScope

22. Select the Test Status indicator just above the test method definition and then select the Run link. If it isn’t visible yet, try pressing Ctrl + Shift + B to build the solution first.

Figure 20

Page 16: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Run unit test

23. Note that this test reports success.

Figure 21Successful unit test run with SharePoint Emulator

Note: If the unit test unexpectedly fails, please restart Visual Studio and try again.

24. Scroll down to the second test method and take a look at what it does. It creates a list, inserts test appointments into the list, and then calls the GetAppointmentsForToday method from the web part under test to ensure that appointments for the current date are returned. It also uses the same SharePointEmulationScope as before.

Figure 22Unit test definition

25. As you work with the SharePoint Emulator, you may run into circumstances where features have not yet been implemented. In these cases, expect to see a NotSupportedException occur during test runs with information about the unimplemented shim. It turns out that the GetAppointmentsForToday method on the web part under test makes use of the SPList.GetItems method, which will throw a NotSupportedException when running the version of the SharePoint Emulator installed on this VM.

Page 17: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

26. Fortunately, it is straightforward to specify an implementation of the missing shim, as evidenced by the following code from the test method. Note that there is an explicit test to run this code block when running in emulation mode.

Figure 23Implementing a shim

Note: This shim implementation simply grabs the first item from the list for the purposes of this demo, regardless of the query passed in. If you would like to learn more about the Microsoft Fakes Framework, please see the MSDN documentation here.

27. Select the Test Status indicator just above the test method definition and then select the Run link to make sure it works as expected.

28. You can also run your unit tests against a real SharePoint instance by changing the emulation mode to bypass all shims and directly call into the original SharePoint assembly. Scroll to the top of the “GetAppointments…” test method and change EmulationMode.Enabled to EmulationMode.Passthrough.

Figure 24Configuring unit test to pass through the emulator

Note: In a real-world scenario, you would likely want to re-use your unit test code in both emulated and passthrough modes. To do this, you could define the emulation mode to use at the test class level and use test initialize and cleanup to create and destroy the scope. Automation of the selected emulation mode could then be implemented using preprocessor

Page 18: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

directives, with the definition provided in the test project file or via the build command line. For more information, please see the MSDN SharePoint Emulator article here.

29. Before we run the modified test, we need to change the Default Processor Architecture to X64 in Test | Test Settings. Otherwise, you will receive an error asking you to do just this in the next step.

30. Run the test once again, but note that this time the test will run against a real SharePoint instance. Note that it takes significantly longer to execute this test.

Figure 25Unit test passing through the emulator

Exercise 2: IntelliTrace Support for SharePoint

In this exercise, you will learn about how to utilize IntelliTrace to improve the debugging experience of SharePoint applications.

1. Launch Visual Studio 2015 from the taskbar (if needed) and then open the SharePointProject1 solution file from c:\SharePointDemos\SharePointProject1.

2. Open WebPart1.cs from Solution Explorer. Note that this web part simply identifies the current user name and renders some output HTML.

Figure 26Sample web part code

Page 19: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

3. Right-click on the SharePointProject1 project and select Deploy to deploy and activate the feature on the local SharePoint server.

4. Let’s go ahead and take a look at this web part in action. Open Internet Explorer and navigate to http://vsalm.

5. Select the gear icon from the top-right and then select the ‘Add a page’ option.

Figure 27Creating a new page

6. Use “IntelliTrace Demo” for the new page name and then select Create.

Figure 28Creating a new test page

Page 20: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

7. Select the Insert tab.

Figure 29Insert tab location

8. Select the Web Part button to add in the custom web part.

Figure 30Web Part button

9. Select the Custom category, the SharePointProject1 web part and then select the Add button to add it to the page.

Page 21: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 31Inserting the web part

10. Here we can see that the web part is deployed and functional.

Figure 32Custom web part in action

11. Select the Save button.

Figure 33

Page 22: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Saving the page

12. Now let’s assume that this web part has been working for some time but that an additional feature has been added in and users are now reporting intermittent errors. Return to Visual Studio and uncomment the line that throws an exception.

Figure 34Artificially introducing an exception for demo

13. Right-click on the SharePointProject1 project and select Deploy to deploy the updated web part.

14. At this point, you can use IntelliTrace to collect diagnostic data for SharePoint using the IntelliTrace PowerShell module. Start IntelliTrace by right-clicking on StartIntelliTraceDemo.cmd and selecting the “Run as administrator” option. This script can be found in C:\SharePointDemos.

Figure 35Start IntelliTrace session

Page 23: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Note: If you would like to learn more about how to use IntelliTrace in a production environment, including the details involved with these scripts, please see the “Diagnosing Issues in Production with IntelliTrace and Visual Studio 2015” lab.

15. After IntelliTrace has started, return to Internet Explorer and refresh the “IntelliTrace Demo” page. An error page should now be displayed by SharePoint. This is something that an end user would likely see and then report back to the development team. Expand the “Technical Details” section, highlight the Correlation ID and then press Ctrl + C to copy it to the clipboard.

Figure 36Typical SharePoint error showing correlation ID

16. Execute the StopIntelliTraceDemo.cmd script to stop IntelliTrace (run as administrator).

Figure 37Stop IntelliTrace session

Page 24: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

17. The scripts that executed IntelliTrace collection were configured to output data to C:\LogFileLocation. Double-click on the IntelliTrace file found at that location to load it in Visual Studio.

Figure 38IntelliTrace log file

18. In the IntelliTrace Summary view, note that there is an Analysis section at the top that shows an unhandled exception. We could start debugging the unhandled exception by clicking on the Debug Exception button, but let’s start by pasting the Correlation ID we received from our end user in to the blank text box (use Ctrl+V) to review matching web requests. Select the View Details button.

Figure 39Viewing web request details associated with Correlation ID

19. Here we can see request information associated with the SharePoint Correlation ID. We can see the target URL, user agent, and so on.

Page 25: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 40Web request details

20. Close the web request details window and return to the IntelliTrace file.

21. Select the Debug Exception button to the right of the unhandled exception.

Figure 41Start debugging the unhandled exception

22. Once Visual Studio is in debug mode, you should see that the location of the unhandled exception is highlighted in the WebPart1.cs source file and that you are now in historical debugging mode.

Page 26: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 42Historical debugging

23. You can now stop the debugging session and close this instance of Visual Studio.

Exercise 3: Web and Load Testing SharePoint Applications

In this exercise, you will learn about how web and load testing tools work with SharePoint applications. To learn more about web and load testing in general, please see the “Introduction to Web Performance and Load Testing with Visual Studio Enterprise 2015”.

1. Launch Visual Studio 2015 from the taskbar (if necessary) and then open the SP_Web_LoadTest_Demo solution file from c:\SharePointDemos\WebAndLoadTestProject1.

2. This solution contains a test project with two web tests and a load test. We will take a closer look at these in a moment.

Page 27: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 43Sample web and load test project

3. Visual Studio automatically handles many of the otherwise tedious tasks when creating web tests against SharePoint. View the available options by selecting Tools | Options | Web Performance Test Tools | Web Test | SharePoint in the main menu of Visual Studio.

Figure 44SharePoint web test options (showing defaults)

Page 28: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

4. After taking a look at the default options for SharePoint web tests, press Escape to exit the Options window.

5. Open Upload.webtest in the web test editor. This web test was recorded by navigating to a SharePoint site, loading the Shared Documents library, and uploading a document.

Figure 45Upload web test definition

6. Expand the first request to AllItems.aspx in the list and note that a SharePoint specific extraction rule was automatically added to grab the list ID and store it as a context parameter.

Figure 46SharePoint extraction rule to find list ID

Page 29: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 47Properties of extraction rule (press F4)

7. The list ID context parameter is then used in subsequent requests, for example in the request to Upload.aspx.

Figure 48Use of list ID context parameter

Page 30: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

8. The actual file that is uploaded during the test run is parameterized as well. Expand the second web request to Upload.aspx (there are two) and scroll to the bottom of the Form Post Parameters and select last option, which is a File Upload Parameter. This shows that when we run the web test, a unique filename will be used when uploading the file.

Figure 49File Upload Parameter

9. Double-click on Download.webtest in Solution Explorer to load it in the web test editor. This web test was recorded by navigating to a SharePoint site, loading the Shared Documents library, and downloading a specific document.

10. Expand the second request to Home.aspx and note that there are already a few SharePoint related extraction rules in place. These extract values such as List and View ID values and store them in context parameters for use in later requests.

Figure 50

Page 31: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

SharePoint extraction rules

11. After the test was recorded, a SharePoint extraction rule was automatically added to find the document item ID and store it as a context parameter using the “SharePoint – Extract Text on Key” rule.

Figure 51Use of a SharePoint extraction rule to find a document item

12. There are a number of useful extraction rules that you can use with your SharePoint web requests. Right-click on one of the web requests and select the Add Extraction Rule option.

Figure 52Location of Add Extraction Rule option

13. The additional SharePoint extraction rules allow you to do things like find a specific list and document IDs, calendar dates, values of text boxes, workflow instance IDs, and so on. Scroll to the right in order to see more rules.

Page 32: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 53New SharePoint extraction rules

14. Press the Escape key to close the Add Extraction Rule window.

15. Double-click on SPLoadTest1.loadtest from Solution Explorer to load it in the load test editor. This is a basic load test definition that will spend half of the time on the Download web test and the other half on the Upload web test.

Page 33: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 54Load test definition

16. Go ahead and run the load test to see it in action by selecting the Run Load Test button. The load test is configured to run for 1 minute.

Figure 55Location of Run Load Test button

17. After the results summary displays, you can see the individual page result details if you scroll down. To visualize the performance details over time, you can also try the Graphs view.

Page 34: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 56Summary results of load test

Page 35: Prerequisitesdownload.microsoft.com/download/6/2/B/62B60ECE-B9DC-4E8A... · Web viewIn this exercise, you will learn how to take advantage of the SharePoint Emulator in your unit

Figure 57Graph view of load test results

To give feedback please write to [email protected]

Copyright © 2023 by Microsoft Corporation. All rights reserved.