Skip to main content
Skip table of contents

Procedures and Libraries

プロシージャーは、再利用可能なコードの断片を作成することができます。これまでのトピックに注目していれば、すでにstartApp.

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

プロシージャーとグローバル変数を組み合わせると、ライブラリを作成することができます。ライブラリとは、通常、再利用可能な変数とプロシージャーを含むスタンドアローンのスクリプトのことです。スクリプト言語はLanguage Reference#includeLanguage Reference#runコマンドを使ってライブラリを含めることができる。ライブラリに変数とプロシージャだけが含まれている場合、両コマンドはこれらのオブジェクトを現在のスクリプトにインポートするだけです。ライブラリにスタンドアロン・コマンドが含まれている場合(メイン・コード・ボディを持つ通常のスクリプトであることを意味する)、コマンドLanguage Reference#runコマンドはそれらを実行する (Language Reference#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に関係なく、パラメータで指定された計算式を計算します。計算結果のスクリーンショットを表示するために、このチュートリアルで初めてHTMLのReporting.

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

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を識別するメカニズムを提供していない。デスクトップイメージから推測するしかないのだが、これでは十分な信頼性は得られないだろう。しかし、OSの識別は、RDPやローカル・デスクトップ・ドライバなど、私たちのロードマップにある他の技術でサポートされるかもしれない。

JavaScript errors detected

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

If this problem persists, please contact our support.