このセクションでは、CreateXPath フックでカスタム ロケーターを作成する方法について説明します。カスタム ロケーターを使用して、Web シナリオのテストを記録するときに要素を識別できます。その後、カスタム ロケーターを検証と抽出に使用できます。

このセクションの内容:

フックとは

カスタマイズしたフックは、SOAtest の特定の実行ポイントで渡される値の記録や修正に使用できます。

フックは、カスタム メソッドを使用したスクリプトで定義してカスタマイズします。同じファイル内に複数のフックを定義できます。 1 つのフックに 2 つ以上のメソッドを追加した場合、フックを呼び出すと、そのフックで定義されたすべてのメソッドが実行されます。

SOAtest で他のスクリプトを作成、適応、呼び出すときと同じ方法で、フックを定義するスクリプトを作成、適応、呼び出すことができます。起動時 (JavaScript および Jython スクリプトだけ)、Extension Tool を作成し適用することによって、および指定のパス ノードにスクリプトを追加することによって、作成、適応、呼び出しが可能です。ユーザーは、目的の機能を使用するために、その時々にフックを呼び出すことができます。たとえば、すべての SOAtest プロジェクトと Web サイトにスクリプトのフック機能を使用したい場合、そのフックを定義し使用する JavaScript または Jython スクリプトを <INSTALL>/plugins/com.parasoft.ptest.libs.web_<VERSION>/root/startup ディレクトリに追加します。その後、プログラムがフックを呼び出すと、常に関連するユーザー定義メソッドが実行されます。メソッドは、フックで clear () を呼び出すまで実行されます。

CreateXPath フックとは 

Web シナリオを記録しているときに、SOAtest はユーザーが対話する要素を、それらの要素の特定の属性から識別します。たとえば、 SOAtest は要素の ID テキスト コンテンツを使用して、その要素を検出します。機能テスト中に要素の識別に使用された属性を拡張する必要がある場合、CreateXPath フックを使用できます。

CreateXPath フックは 2 つの引数を取ります。1 つ目は org.w3c.dom.Node のインスタンスであり、ロケーターがビルドされるノードです。 2 つ目は org.w3c.dom.Document のインスタンスであり、対象ノードがある文書です。CreateXPath 関数は文書中のノードを識別する XPath を返すか、あるいは XPath が作成できない場合は NULL を返すべきです。

たとえば、"class" 属性で要素を識別したい場合、CreateXPath フックを使用してカスタム識別子を作成できます。カスタム識別子は、要素を "class" 属性でチェックし、発見した場合、"class" 属性の値によって要素を識別する XPath を返します。

検証する要素を選択するとき、または記録中に要素をクリックするとき、 SOAtest はこのカスタム フックを使用します。要素はカスタム フックへ渡され、カスタム フックは "class" 属性で要素をチェックします。該当する要素が見つかった場合は、その要素を識別する "class" 属性値を使用して XPath を返します。 

以下は、 2 つ以上のノードが同じ "class" を持つ場合に、"class" 属性とインデックスを使用して要素を識別するスクリプトのサンプルです。 

app = Packages.com.parasoft.api.Application; 
settings = Packages.webtool.browsertest.XPathCreator;
 
// establish the locator builder priority order
// we use our custom locator after using the id, name and attribute locators // and before using the position xpath locator
settings.locatorBuildersOrder[0] = settings.ID_LOCATOR;
settings.locatorBuildersOrder[1] = settings.NAME_LOCATOR;
settings.locatorBuildersOrder[2] = settings.ATTRIBUTE_XPATH_LOCATOR; 
settings.locatorBuildersOrder[3] = settings.HOOK_LOCATOR;
settings.locatorBuildersOrder[4] = settings.POSITION_XPATH_LOCATOR;
 
// gets the index of the node out of a list of nodes returned using xpath. 
function getIndex(xpath, node, doc) {
  index = -1;
   if (doc) {
    try {
         list = Packages.org.apache.xpath.XPathAPI.selectNodeList(doc, xpath); 
         count = 0;
         while (index == -1 && count < list.getLength()) {
             candidate = list.item(count); 
             if (candidate.equals(node)) {
                index = count; 
             } else {
                  ++count;
             }
        }
    } catch (e) {}
  }
  return index;
}
 
// the hook function to link to the CreateXPath hook.
// looks for the class attribute and uses it (along with an index) to create 
// a locator for the given node.
function createXPathHook(node, doc) {
  nodeName = node.getNodeName();
  attributes = node.getAttributes();
  classValue = attributes.getNamedItem("class");   
  if (classValue) {
    xpath = "//" + nodeName + "[@"+ classValue + "]";
    index = getIndex(xpath, node, doc);
    if (index != -1) {
          return xpath + "[" + index + "]";
    }
  }
  return null;
}
 
// sets up the hook 
function setHook() {
  hook = app.getHook("CreateXPath");
  hook.set(createXPathHook);
}
  • No labels