方法: Web パフォーマンス テスト エディターのカスタム HTTP ボディ エディターを作成する
発行: 2016年7月
カスタム コンテンツ エディターを作成して、Web サービス要求 (たとえば、SOAP、REST、asmx、wcf、RIA、その他の種類の Web サービス要求など) の文字列ボディのコンテンツまたはバイナリ ボディのコンテンツを編集できます。
必要条件
- Visual Studio Enterprise
次の種類のエディターを実装できます。
文字列コンテンツ エディターを使用してこの実装は、IStringHttpBodyEditorPluginインターフェイスです。
バイナリ コンテンツ エディターを使用してこの実装は、IBinaryHttpBodyEditorPluginインターフェイスです。
これらのインターフェイスは、Microsoft.VisualStudio.TestTools.WebTesting 名前空間に含まれます。
Windows コントロール ライブラリ プロジェクトの作成
Windows コントロール ライブラリ プロジェクトを使用して、ユーザー コントロールを作成します。
Visual Studio の [ファイル] メニューで、[新規作成]、[プロジェクト] の順にクリックします。
[新しいプロジェクト] ダイアログ ボックスが表示されます。
[インストールされたテンプレート] で、使用するプログラミングに応じて [Visual Basic] または [Visual C#] をクリックし、[Windows] を選択します。
注意
このサンプルでは、Visual C# を使用しています。
テンプレートの一覧で、[Windows フォーム コントロール ライブラリ] を選択します。
[名前] ボックスに「MessageEditors」などの名前を入力し、[OK] をクリックします。
注意
このサンプルでは、MessageEditors を使用しています。
プロジェクトが新しいソリューションに追加され、UserControl1.cs という名前の UserControl がデザイナーに表示されます。
ツールボックスの [コモン コントロール] カテゴリで、RichTextBox を UserControl1 のサーフェイスにドラッグします。
RichTextBox コントロールの右上隅にあるアクション タグ グリフ (
) をクリックして、[親コンテナーにドッキングする] を選択します。
ソリューション エクスプローラーで、Windows フォーム ライブラリ プロジェクトを右クリックし、[プロパティ] をクリックします。
[プロパティ] の [アプリケーション] タブをクリックします。
[対象とする Framework] ボックスの一覧で、[.NET Framework 4] を選択します。
[ターゲット フレームワークの変更] ダイアログ ボックスが表示されます。
[はい] をクリックします。
ソリューション エクスプローラーで、[参照設定] ノードを右クリックし、[参照の追加] をクリックします。
[参照の追加] ダイアログ ボックスが表示されます。
[.NET] タブをクリックします。スクロール ダウンし、[Microsoft.VisualStudio.QualityTools.WebTestFramework] を選択して、[OK] をクリックします。
ソリューション エクスプローラーでビュー デザイナーがまだ開いていない場合は、UserControl1.cs を右クリックし、[デザイナーの表示] をクリックします。
デザイン サーフェイスを右クリックし、[コードの表示] をクリックします。
(省略可能) クラスとコンストラクターの名前を、UserControl1 からわかりやすい名前 (MessageEditorControl など) に変更します。
注意
このサンプルでは、MessageEditorControl を使用しています。
namespace MessageEditors { public partial class MessageEditorControl : UserControl { public MessageEditorControl() { InitializeComponent(); } } }
次のプロパティを追加して、RichTextBox1 のテキストを取得および設定できるようにします。 IStringHttpBodyEditorPlugin インターフェイスでは EditString を使用し、IBinaryHttpBodyEditorPlugin では EditByteArray を使用します。
public String EditString { get { return this.richTextBox1.Text; } set { this.richTextBox1.Text = value; } } public byte[] EditByteArray { get { return System.Convert.FromBase64String(richTextBox1.Text); } set { richTextBox1.Text = System.Convert.ToBase64String(value, 0, value.Length); } }
Windows コントロール ライブラリ プロジェクトへのクラスの追加
プロジェクトにクラスを追加します。 このクラスは IStringHttpBodyEditorPlugin インターフェイスと IBinaryHttpBodyEditorPlugin インターフェイスの実装に使用されます。
この手順でのコードの概要
前の手順で作成した MessageEditorControl UserControl は、messageEditorControl としてインスタンス化されます。
private MessageEditorControl messageEditorControl
messageEditorControl インスタンスは、CreateEditor メソッドによって作成されたプラグイン ダイアログ内でホストされます。 また、messageEditorControl の RichTextBox には、IHttpBody のコンテンツが設定されます。 ただし、SupportsContentType が true を返さない場合、プラグインの作成を行うことはできません。 このエディターの場合、SupportsContentType の true に "xml" が含まれているときは、ContentType は IHttpBody を返します。
文字列ボディの編集が完了し、ユーザーがプラグイン ダイアログ ボックスで [OK] をクリックすると、Web テスト パフォーマンス エディターでは GetNewValue が呼び出されて、編集済みのテキストが文字列として取得され、要求の文字列ボディが更新されます。
クラスを作成して IStringHttpBodyEditorPlugin インターフェイス コードを実装するには
ソリューション エクスプローラーで、Windows フォーム コントロール ライブラリ プロジェクトを右クリックし、[新しい項目の追加] をクリックします。
[新しい項目の追加] ダイアログ ボックスが表示されます。
[クラス] を選択します。
[名前] ボックスに、クラスのわかりやすい名前 (MessageEditorPlugins など) を入力します。
[追加] をクリックします。
Class1 がプロジェクトに追加され、コード エディターに表示されます。
コード エディターで、次の using ステートメントを追加します。
using Microsoft.VisualStudio.TestTools.WebTesting;
IStringHttpBodyEditorPlugin インターフェイスから XmlMessageEditor クラスをインスタンス化し、必要なメソッドを実装するために、次のコードを記述またはコピーします。
/// <summary> /// Editor for generic text based hierarchical messages such as XML and JSON. /// </summary> public class XmlMessageEditor : IStringHttpBodyEditorPlugin { public XmlMessageEditor() { } /// <summary> /// Determine if this plugin supports the content type. /// </summary> /// <param name="contentType">The content type to test.</param> /// <returns>Returns true if the plugin supports the specified content type.</returns> public bool SupportsContentType(string contentType) { return contentType.ToLower().Contains("xml"); } /// <summary> /// Create a UserControl to edit the specified bytes. /// This control will be hosted in the /// plugin dialog which provides OK and Cancel buttons. /// </summary> /// <param name="contentType">The content type of the BinaryHttpBody.</param> /// <param name="initialValue">The bytes to edit. The bytes are the payload of a BinaryHttpBody.</param> /// <returns>A UserControl capable of displaying and editing the byte array value of the specified content type.</returns> public object CreateEditor(string contentType, string initialValue) { messageEditorControl = new MessageEditorControl(); messageEditorControl.EditString = initialValue; return this.messageEditorControl; } /// <summary> /// Gets the edited bytes after the OK button is clicked on the plugin dialog. /// </summary> public string GetNewValue() { return messageEditorControl.EditString; } private MessageEditorControl messageEditorControl; }
クラスへの IBinaryHttpBodyEditorPlugin の追加
IBinaryHttpBodyEditorPlugin インターフェイスを実装します。
この手順でのコードの概要
IBinaryHttpBodyEditorPlugin インターフェイスのコードの実装は、前の手順で説明した IStringHttpBodyEditorPlugin の場合と似ています。 ただし、バイナリ バージョンではバイト配列を使用して、文字列の代わりにバイナリ データを処理します。
最初の手順で作成された MessageEditorControl UserControl は、messageEditorControl としてインスタンス化されます。
private MessageEditorControl messageEditorControl
messageEditorControl インスタンスは、CreateEditor メソッドによって作成されたプラグイン ダイアログ内でホストされます。 また、messageEditorControl の RichTextBox には、IHttpBody のコンテンツが設定されます。 ただし、SupportsContentType が true を返さない場合、プラグインの作成を行うことはできません。 このエディターの場合、SupportsContentType の true に "msbin1" が含まれているときは、ContentType は IHttpBody を返します。
文字列ボディの編集が完了し、ユーザーがプラグインのダイアログ ボックスで [OK] をクリックすると、Web テスト パフォーマンス エディターで GetNewValue が呼び出されて、編集済みのテキストが文字列として取得され、要求の BinaryHttpBody.Data が更新されます。
クラスに IBinaryHttpBodyEditorPlugin を追加するには
前の手順で追加された XmlMessageEditor クラスで次のコードを記述またはコピーして、IBinaryHttpBodyEditorPlugin インターフェイスから Msbin1MessageEditor クラスをインスタンス化し、必要なメソッドを実装します。
/// <summary> /// Editor for MSBin1 content type (WCF messages) /// </summary> public class Msbin1MessageEditor : IBinaryHttpBodyEditorPlugin { /// <summary> /// /// </summary> public Msbin1MessageEditor() { } /// <summary> /// Determine if this plugin supports a content type. /// </summary> /// <param name="contentType">The content type to test.</param> /// <returns>Returns true if the plugin supports the specified content type.</returns> public bool SupportsContentType(string contentType) { return contentType.ToLower().Contains("msbin1"); } /// <summary> /// Create a UserControl to edit the specified bytes. This control will be hosted in the /// plugin dialog which provides OK and Cancel buttons. /// </summary> /// <param name="contentType">The content type of the BinaryHttpBody.</param> /// <param name="initialValue">The bytes to edit. The bytes are the payload of a BinaryHttpBody.</param> /// <returns>A UserControl capable of displaying and editing the byte array value of the specified content type.</returns> public object CreateEditor(string contentType, byte[] initialValue) { messageEditorControl = new MessageEditorControl(); messageEditorControl.EditByteArray = initialValue; return messageEditorControl; } /// <summary> /// Gets the edited bytes after the OK button is clicked on the plugin dialog. /// </summary> public byte[] GetNewValue() { return messageEditorControl.EditByteArray; } private MessageEditorControl messageEditorControl; private object originalMessage; }
プラグインのビルドおよび配置
IStringHttpBodyEditorPlugin および IBinaryHttpBodyEditorPlugin について生成される dll をビルドおよび配置するには
[ビルド] メニューの [<Windows フォーム コントロール ライブラリ プロジェクト名> のビルド] をクリックします。
Visual Studio のすべてのインスタンスを閉じます。
注意
これにより、.dll ファイルがコピーしようとする前にロックされていないことを確認します。
プロジェクトの bin\debug フォルダーの生成された .dll ファイル (MessageEditors.dll など) を %ProgramFiles%\Microsoft Visual Studio 12.0\Common7\IDE\PrivateAssemblies\WebTestPlugins にコピーします。
Visual Studio を開きます。
.dll が Visual Studio に登録されるようになりました。
Web パフォーマンス テストによるプラグインの検証
プラグインをテストするには
テスト プロジェクトを作成します。
Web パフォーマンス テストを作成し、ブラウザーで Web サービスへの URL を入力します。例: http://dev.virtualearth. net/webservices/v1/metadata/searchservice/dev.virtualearth. net.webservices.v1.search.wsdl。
記録が終了したら、Web パフォーマンス テスト エディターで Web サービスの要求を展開し、[文字列ボディ] または [バイナリ ボディ] を選択します。
[プロパティ] ウィンドウで、[文字列ボディ] または [バイナリ ボディ] を選択し、省略記号 (...) をクリックします。
[HTTP ボディ データの編集] ダイアログ ボックスが表示されます。
ここでデータを編集できます。[OK] をクリックします。 これにより、該当する GetNewValue メソッドを起動して、IHttpBody のコンテンツを更新できます。
コードのコンパイル
Windows コントロール ライブラリ プロジェクトの対象のフレームワークが .NET Framework 4.5 であることを確認します。 既定では、Windows コントロール ライブラリ プロジェクトは .NET Framework 4.5 クライアント フレームワークを対象としますが、その場合 Microsoft.VisualStudio.QualityTools.WebTestFramework 参照を含むことは許可されません。
詳細については、「[アプリケーション] ページ (プロジェクト デザイナー) (C#)」を参照してください。
参照
IStringHttpBodyEditorPlugin
CreateEditor
SupportsContentType
GetNewValue
IBinaryHttpBodyEditorPlugin
CreateEditor
SupportsContentType
GetNewValue
IHttpBody
ContentType
UserControl
RichTextBox
ロード テスト用のカスタム コードおよびカスタム プラグインの作成
方法 : 要求レベルのプラグインを作成する
Web パフォーマンス テストのカスタム抽出規則のコーディング
Web パフォーマンス テストのカスタム検証規則のコーディング
方法 : ロード テスト プラグインを作成する
[廃版] 方法: コード化された Web パフォーマンス テストを作成する
方法: Web パフォーマンス テスト結果ビューアー用に Visual Studio アドインを作成する