多重步驟的 Web 效能測試Multi-step web tests

您可以透過多步驟 web 測試, 監視一系列記錄的 Url 和與網站的互動。You can monitor a recorded sequence of URLs and interactions with a website via multi-step web tests. 本文將逐步引導您完成使用 Visual Studio Enterprise 建立多重步驟 web 測試的程式。This article will walk you through the process of creating a multi-step web test with Visual Studio Enterprise.

注意

多步驟 web 測試取決於 Visual Studio webtest 檔案。Multi-step web tests depend on Visual Studio webtest files. 宣佈, Visual Studio 2019 將會是具有 webtest 功能的最後一個版本。It was announced that Visual Studio 2019 will be the last version with webtest functionality. 請務必瞭解, 雖然不會新增任何新功能, 但目前仍支援 Visual Studio 2019 中的 webtest 功能, 並會在產品的支援生命週期中繼續受到支援。It is important to understand that while no new features will be added, webtest functionality in Visual Studio 2019 is still currently supported and will continue to be supported during the support lifecycle of the product. Azure 監視器產品小組在解決了有關多步驟可用性測試未來的問題。The Azure Monitor product team has addressed questions regarding the future of multi-step availability tests here.

先決條件Pre-requisites

  • Visual Studio 2017 企業版或更新版本。Visual Studio 2017 Enterprise or greater.
  • Visual Studio web 效能和負載測試工具。Visual Studio web performance and load testing tools.

找出必要的測試控管。To locate the testing tools pre-requisite. 啟動Visual Studio 安裝程式 > 個別元件 > 的調試和測試 Web 效能和負載測試工具。 > Launch the Visual Studio Installer > Individual components > Debugging and testing > Web performance and load testing tools.

[Visual Studio 安裝程式] UI 的螢幕擷取畫面, 其中已選取個別元件, 並在 Web 效能和負載測試工具專案旁邊有核取方塊

注意

多步驟 web 測試具有相關聯的額外成本。Multi-step web tests have additional costs associated with them. 若要深入瞭解, 請參閱官方定價指南To learn more consult the official pricing guide.

錄製多步驟 web 測試Record a multi-step web test

若要建立多重步驟測試,您可以使用 Visual Studio Enterprise 來記錄案例,然後將記錄結果上傳至 Application Insights。To create a multi-step test, you record the scenario by using Visual Studio Enterprise, and then upload the recording to Application Insights. Application Insights 依設定的間隔重新執行案例, 並確認回應。Application Insights replays the scenario at set intervals and verifies the response.

重要

  • 您無法在測試中使用已編碼的函式或迴圈。You can't use coded functions or loops in your tests. 測試必須完全包含於 .webtest 指令碼中。The test must be contained completely in the .webtest script. 不過,您可以使用標準外掛程式。However, you can use standard plugins.
  • 多重步驟 Web 測試僅支援英文字元。Only English characters are supported in the multi-step web tests. 如果您在 Visual Studio 中使用其他語言,請更新 Web 測試的定義檔,以轉譯/排除非英文字元。If you use Visual Studio in other languages, please update the web test definition file to translate/exclude non-English characters.

使用 Visual Studio Enterprise 來記錄 Web 工作階段。Use Visual Studio Enterprise to record a web session.

  1. 建立 Web 效能和負載測試專案。Create a Web performance and Load Test Project. [檔案] [新增 > 專案 ] 視覺效果C# 測試 > > > File > New > Project > Visual C# > Test

    Visual Studio 新增專案 UI

  2. .webtest開啟檔案並開始錄製。Open the .webtest file and start recording.

    Visual Studio 測試錄製 UI

  3. 在錄製過程中, 按一下您想要測試模擬的步驟。Click through the steps you want your test to simulate as part of the recording.

    瀏覽器錄製 UI

  4. 編輯本測試以進行下列事項:Edit the test to:

    • 加入驗證以檢查收到的文字和回應碼。Add validations to check the received text and response codes.
    • 移除任何 uneccesary 互動。Remove any uneccesary interactions. 您也可以移除圖片的相依要求, 或加入與您認為測試成功無關的追蹤網站。You could also remove dependent requests for pictures or add tracking sites which aren't relevant to you considering your test a success.

    請記住, 您只能編輯測試腳本, 您可以加入自訂程式碼或呼叫其他 web 測試。Keep in mind that you can only edit the test script - you can add custom code or call other web tests. 請勿在此測試中插入迴圈。Don't insert loops in the test. 您可以使用標準 Web 測試的外掛程式。You can use standard web test plug-ins.

  5. 在 Visual Studio 中執行測試, 以進行驗證並確定其運作正常。Run the test in Visual Studio to validate and make sure it works.

    Web 測試執行器會開啟網頁瀏覽器,並重複您已記錄的動作。The web test runner opens a web browser and repeats the actions you recorded. 請確定所有內容都如預期般運作。Make sure everything behaves as expected.

上傳 web 測試Upload the web test

  1. 在 [可用性] 窗格的 Application Insights 入口網站中, 選取 [建立測試 > 測試類型 > ] [多重步驟 web 測試]。In the Application Insights portal on the Availability pane select Create Test > Test type > Multi-step web test.

  2. 設定 [測試位置]、[頻率] 和 [警示] 參數。Set the test locations, frequency, and alert parameters.

頻率 & 位置Frequency & location

設定Setting 說明Explanation
測試頻率Test frequency 設定從每個測試位置執行測試的頻率。Sets how often the test is run from each test location. 預設頻率為 5 分鐘且有五個測試位置,則您的網站平均每一分鐘會執行測試。With a default frequency of five minutes and five test locations, your site is tested on average every minute.
測試位置Test locations 這是我們的伺服器將 web 要求傳送至您的 URL 的位置。Are the places from where our servers send web requests to your URL. 建議的測試位置數目下限為 5 , 以確保您可以區分網站中的問題與網路問題。Our minimum number of recommended test locations is five in order to insure that you can distinguish problems in your website from network issues. 您最多可以選取 16 個位置。You can select up to 16 locations.

成功準則Success criteria

設定Setting 說明Explanation
測試超時Test timeout 降低此值可收到有關回應變慢的警示。Decrease this value to be alerted about slow responses. 如果未在這段時間內收到您網站的回應,則測試會視為失敗。The test is counted as a failure if the responses from your site have not been received within this period. 如果已選取 [剖析相依要求],則必須在這段時間內收到所有映像、樣式檔、指令碼和其他相依資源。If you selected Parse dependent requests, then all the images, style files, scripts, and other dependent resources must have been received within this period.
HTTP 回應HTTP response 視為成功的傳回狀態碼。The returned status code that is counted as a success. 200 是表示已傳回標準 Web 網頁的代碼。200 is the code that indicates that a normal web page has been returned.
內容相符Content match 字串, 例如「歡迎!」A string, like "Welcome!" 我們會測試每個回應中的區分大小寫完全相符。We test that an exact case-sensitive match occurs in every response. 必須是單純字串,不含萬用字元。It must be a plain string, without wildcards. 別忘了,如果頁面內容變更,則可能需要更新。Don't forget that if your page content changes you might have to update it. 內容相符僅支援英文字元Only English characters are supported with content match

警示Alerts

設定Setting 說明Explanation
近乎即時 (預覽)Near-realtime (Preview) 我們建議使用近乎即時的警示。We recommend using Near-realtime alerts. 設定此類型的警示會在您的可用性測試建立後完成。Configuring this type of alert is done after your availability test is created.
傳統Classic 我們不再建議使用傳統警示來進行新的可用性測試。We no longer recommended using classic alerts for new availability tests.
警示位置閾值Alert location threshold 建議至少為位置數的 3/5。We recommend a minimum of 3/5 locations. 警示位置閾值與測試位置數目之間的最佳關聯性是測試位置的警示位置閾值 = (2), 最少五個測試位置。The optimal relationship between alert location threshold and the number of test locations is alert location threshold = number of test locations - 2, with a minimum of five test locations.

進階組態Advanced Configuration

將時間和亂數字插入您的測試中Plugging time and random numbers into your test

假設您要測試的工具會從外部來源取得與時間相關的資料 (例如股票)。Suppose you're testing a tool that gets time-dependent data such as stocks from an external feed. 當您記錄 Web 測試時,您必須使用特定的時間,但您將它們設為測試的參數:StartTime 和 EndTime。When you record your web test, you have to use specific times, but you set them as parameters of the test, StartTime and EndTime.

我的絕佳股票應用程式螢幕擷取畫面

當您執行測試時,希望 EndTime 永遠為目前時間,而 StartTime 為 15 分鐘前。When you run the test, you'd like EndTime always to be the present time, and StartTime should be 15 minutes ago.

Web 測試日期時間外掛程式提供處理參數化時間的方式。The Web Test Date Time Plugin provides the way to handle parameterize times.

  1. 針對您想要的每個變數參數值,各加入一個 Web 測試外掛程式。Add a web test plug-in for each variable parameter value you want. 在 Web 測試工具列中,選擇 [加入 Web 測試外掛程式]。In the web test toolbar, choose Add Web Test Plugin.

    新增 Web 測試外掛程式

    在此範例中,我們會使用兩個日期時間外掛程式執行個體。In this example, we use two instances of the Date Time Plug-in. 一個執行個體設定為 "15 minutes ago",另一個則設定為 "now"。One instance is for "15 minutes ago" and another for "now."

  2. 開啟每個外掛程式的屬性。Open the properties of each plug-in. 為屬性命名,然後將它設為使用目前時間。Give it a name and set it to use the current time. 對其中一個,設定 [加入分鐘] = -15。For one of them, set Add Minutes = -15.

    內容參數

  3. 在 Web 測試參數中,使用 {{plug-in name}} 來參考外掛程式名稱。In the web test parameters, use {{plug-in name}} to reference a plug-in name.

    StartTime

現在將您的測試上傳至入口網站。Now, upload your test to the portal. 在每次執行測試時,它會使用動態值。It will use the dynamic values on every run of the test.

處理登入Dealing with sign-in

如果使用者登入您的應用程式,您有許多模擬登入的選項,以便在登入後方測試頁面。If your users sign in to your app, you have various options for simulating sign-in so that you can test pages behind the sign-in. 您使用的方法取決於應用程式所提供的安全性類型。The approach you use depends on the type of security provided by the app.

在所有情況下,您應該只為了測試用途在您的應用程式中建立帳戶。In all cases, you should create an account in your application just for the purpose of testing. 可能的話,限制此測試帳戶的權限,讓 Web 測試不可能影響實際使用者。If possible, restrict the permissions of this test account so that there's no possibility of the web tests affecting real users.

簡單的使用者名稱和密碼以一般方式記錄 web 測試。Simple username and password Record a web test in the usual way. 先刪除 Cookie。Delete cookies first.

SAML 驗證SAML authentication

屬性名稱Property name 描述Description
物件 UriAudience Uri SAML 權杖的物件 URI。The audience URI for the SAML token. 這是存取控制服務 (ACS) 的 URI-包括 ACS 命名空間和主機名稱。This is the URI for the Access Control Service (ACS) – including ACS namespace and host name.
憑證密碼Certificate Password 將授與內嵌私密金鑰存取權之用戶端憑證的密碼。The password for the client certificate which will grant access to the embedded private key.
用戶端憑證Client Certificate 具有 Base64 編碼格式之私密金鑰的用戶端憑證值。The client certificate value with private key in Base64 encoded format.
名稱識別碼Name Identifier 權杖的名稱識別碼The name identifier for the token
不在之後Not After 權杖將會有效的 timespan。The timespan for which the token will be valid. 預設值是 5 分鐘。The default is 5 minutes.
生效時間Not Before 在過去建立之權杖的時間範圍將會有效 (以處理時間扭曲)。The timespan for which a token created in the past will be valid (to address time skews). 預設值為 (負) 5 分鐘。The default is (negative) 5 minutes.
目標內容參數名稱Target Context Parameter Name 將接收所產生之判斷提示的內容參數。The context parameter that will receive the generated assertion.

用戶端密碼如果您的應用程式具有牽涉到用戶端密碼的登入路由, 請使用該路由。Client secret If your app has a sign-in route that involves a client secret, use that route. Azure Active Directory (AAD) 是可提供用戶端密碼登入的服務範例。Azure Active Directory (AAD) is an example of a service that provides a client secret sign-in. 在 AAD 中,用戶端密碼是應用程式金鑰。In AAD, the client secret is the App Key.

以下是使用應用程式金鑰之 Azure Web 應用程式的 Web 測試範例︰Here's a sample web test of an Azure web app using an app key:

範例螢幕擷取畫面

從使用用戶端密鑰 (AppKey) 的 AAD 取得權杖。Get token from AAD using client secret (AppKey). 從回應中擷取持有人權杖。Extract bearer token from response. 使用授權標頭中的持有人權杖呼叫 API。Call API using bearer token in the authorization header. 請確定 web 測試是實際的用戶端, 也就是它在 AAD 中有自己的應用程式, 並使用其 clientId + 應用程式金鑰。Make sure that the web test is an actual client - that is, it has its own app in AAD - and use its clientId + app key. 您測試中的服務在 AAD 中也有自己的應用程式︰此應用程式的 appID URI 會反映於 [資源] 欄位中的 Web 測試。Your service under test also has its own app in AAD: the appID URI of this app is reflected in the web test in the resource field.

開放驗證Open Authentication

使用您的 Microsoft 或 Google 帳戶登入即是開放驗證的範例。An example of open authentication is signing in with your Microsoft or Google account. 許多使用 OAuth 的應用程式都提供替代用戶端密碼,第一個技巧就是調查該可能性。Many apps that use OAuth provide the client secret alternative, so your first tactic should be to investigate that possibility.

如果您的測試必須使用 OAuth 登入,則常用的方式是:If your test must sign in using OAuth, the general approach is:

使用 Fiddler 等工具來檢查網頁瀏覽器、驗證網站及您的應用程式之間的流量。Use a tool such as Fiddler to examine the traffic between your web browser, the authentication site, and your app. 使用不同的電腦或瀏覽器,或以較長時間間隔 (讓權杖過期) 執行兩次以上的登入。Perform two or more sign-ins using different machines or browsers, or at long intervals (to allow tokens to expire). 藉由比較不同的工作階段,識別從驗證網站傳回的權杖,登入之後此權杖會傳遞至您的應用程式伺服器。By comparing different sessions, identify the token passed back from the authenticating site, that is then passed to your app server after sign-in. 使用 Visual Studio 記錄 Web 測試。Record a web test using Visual Studio. 將權杖參數化,當驗證器傳回權杖時設定參數,然後在查詢網站時使用參數。Parameterize the tokens, setting the parameter when the token is returned from the authenticator, and using it in the query to the site. (Visual Studio 會嘗試將測試參數化,但不會正確地將權杖參數化。)(Visual Studio attempts to parameterize the test, but does not correctly parameterize the tokens.)

疑難排解Troubleshooting

專屬的疑難排解文章Dedicated troubleshooting article.

後續步驟Next steps