Skip to main content
Skip table of contents

Procedures and Libraries

プロシージャは再利用可能なコードの断片を作成することを可能にします。これまでのトピックに注意を払っていたなら、startAppという名前のプロシージャを既に発見しているかもしれません。

各プロシージャーは、ヘッダー、ボディ、右閉じ中かっこで構成されます。ヘッダーは1行で定義する必要があり、"procedure "キーワード、プロシージャー名(大文字と小文字を区別する)、左開きの中小括弧で構成されます。プロシージャーは、その定義後、スクリプトの任意の場所で名前を付けて呼び出すことができます。このような呼び出しには、任意の数のパラメータを含めることができ、それらは、例えば"{1}"、"{2}"などのように、1から始まる番号の付いた変数を通して、本文中で利用可能になる。ゼロ変数の"{0}"は、常にプロシージャー名を含んでおり、例えば報告目的で使用することができます。

プロシージャーとグローバル変数を組み合わせると、ライブラリを作成することができます。ライブラリとは、通常、再利用可能な変数とプロシージャーを含むスタンドアローンのスクリプトのことです。スクリプト言語はIncludeRunコマンドを使ってライブラリを含めることができます。ライブラリに変数とプロシージャだけが含まれている場合、両コマンドはこれらのオブジェクトを現在のスクリプトにインポートするだけです。ライブラリにスタンドアロン・コマンドが含まれている場合(メイン・コード・ボディを持つ通常のスクリプトであることを意味します)、コマンドRunコマンドはそれらを実行します (Includeしない)。コマンドはJavaコードをロードして実行することもできることに注意してください。Interoperability of Scripts and Java Codeトピックを参照してください。

このチュートリアルの前半で定義した電卓スクリプトを再利用してみましょう:

calculator.tpr (Windows バージョン)

calculator.tpr (Linux/Gnome バージョン)

JS
# Generic procedure to start an application on Windows. 
# We take advantage of the Windows+r key to open the Run box. 
procedure startApp { 
  Press Windows+r wait= 3s 
  Typeline "{1}" wait= 4s
} 

# Start calculator, type "5+5" followed by Enter and take a screenshot.
startApp calc 
Typeline "5+5" wait= 2s 
Screenshot calculator_result.jpg desc= "Result of 5+5"
JS
# Generic procedure to start an application on Linux/GNOME. 
# We take advantage of the Alt+F2 key to open the Run box. 
procedure startApp { 
  Press Alt+F2 wait= 3s 
  Typeline "{1}" wait= 4s
} 

# Start calculator, type "5+5" followed by Enter and take a screenshot.
startApp gnome-calculator 
Typeline "5+5" wait= 2s 
Screenshot calculator_result.jpg desc= "Result of 5+5"

私たちの目標は、再利用可能なコードの断片をライブラリに抽出し、WindowsとLinuxの両方で一般的な電卓テストができるスクリプトにすることです。そのために、calculateという新しいプロシージャを定義します。このプロシージャは、テストするOSに関係なく、パラメータで指定された計算式を計算します。結果のスクリーンショットを表示するため、このチュートリアルでは初めてHTMLReporting.も作成します。

出来上がったコードは以下のようになります。

calculator.tpr (Windows version)

calculator-lib.tpr

JS
# Define the OS variable because the library expects it. 
Var OS="Windows" 

# Include the library. 
Include calculator-lib. tpr

# Define HTML report. 
Report calculator-{OS}.html desc="Calculator testing on {OS}."

# Calculate "5+5" and "10*5".
calculate "5+5" 
calculate "10*5" 

JS
# Variable initialization based on target OS. 
# If the "OS" variable is not initialized, default to Windows. 
Var RUN_BOX_KEY="Windows+r"
Var CALCULATOR_CMD="calc" 

# Both systems close applications through Alt+F4 
Var CLOSE_KEY="Alt+F4" 

if ("{OS}" == "Linux") { 
  Var RUN_BOX_KEY="Alt+F2"
  Var CALCULATOR_CMD="gnome-calculator"
} 

# Start an application through the Run box. 
# Params: {1} ... application start command 
procedure startApp { 
  Press "{RUN_BOX_KEY}" wait=5s
  Typeline "{1}" wait=4s
} 

# Start the calculator, type the expression, save 
# a screenshot to a uniquely named file and close it. 
# Params: {1} ... numeric expression to type, for ex. "5+5" 
procedure calculate {
  startApp "{CALCULATOR_CMD}" 
  Typeline "{1}" wait=2s
  Screenshot calculator_{_CURTIME}.jpg desc="Result of {1}" 
  Press "{CLOSE_KEY}" wait=2s
}

calculator.tpr (Linux version)

JS
# Define the OS variable because the library expects it. 
Var OS="Linux" 

# Include the library. 
Include calculator-lib. tpr

# Define HTML report. 
Report calculator-{OS}.html desc="Calculator testing on {OS}."

# Calculate "5+5" and"10*5".
calculate "5+5" 
calculate "10*5"

Windows用とLinux用の2つのスクリプトがあることに異論があるかもしれません。必ずしもそうである必要はありません。GUIから手動でスクリプトを実行する場合は、2つのスクリプトを使うか、テスト環境に応じてOS変数を書き換える必要があります。しかし、自動実行の場合、スクリプトは1つだけとなります。 CLI ExecutionOS "変数をターゲット テスト システムに応じて "Windows "または "Linux "に設定します。その方法は、以前のトピックで紹介しました:

  • リモートデスクトップがWindowsの場合:java -jar robot.jar -c <desktop> -p <password> -r calculator.tpr -v OS=Windows

  • リモートデスクトップがLinuxの場合:java -jar robot.jar -c <desktop> -p <password> -r calculator.tpr -v OS=Linux

もちろん、これが唯一の解決策ではありません。Windows用とLinux用の2つのライブラリを作成することもできます。これらを動的に含めるには、Include引数を変数(" Include「{MYLIB}"")として指定し、CLIで適切なライブラリファイルの値を設定します。

明らかな疑問として、OSの検出を自動化できるかどうかが挙げられます。答えとしては可能ではありますが、vncでは実現できません。残念ながらRFBプロトコルには、リモートデスクトップのOSを識別する仕組みが一切備わっていないためです。デスクトップ画像から推測するしかなく、信頼性は決して十分とは言えません。ただし、RDPやローカルデスクトップドライバーなど、当社のロードマップに載っている他の技術ではOS識別をサポートする可能性があります。

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.