DebugLibraryを使って、Robot Framework のREPLやテスト実行中のREPLを試す

この記事は 「Robot Framework Advent Calendar 2017 - Qiita」 の16日目の記事です。

プログラム言語で何かをちょっと試したいという時は、REPL (Read-eval-print loop) が便利です。

Robot FrameworkでもREPLがないかを探したところ、Robotframework-DebugLibraryを使えば良さそうでした。
https://github.com/xyb/robotframework-debuglibrary

そこで今回は、Robotframework-DebugLibraryを使って、Robot Framework のREPLを試してみます。

 
目次

 

環境

  • Python 3.6.3
  • Robot Framework 3.0.2
  • Robotframework-DebugLibrary 1.0.2

Robotframework-DebugLibraryのインストールは pip install robotframework-debuglibrary でOKです。

 

コンソールでREPLを試す

起動

コンソールを起動し、 rfdebug コマンドを入力すると、REPLが起動します。

$ rfdebug
=========================
Robot Debugqqpsyuvs
=========================
RFDEBUG REPL
>>>>> Enter interactive shell
Only accepted plain text format keyword seperated with two or more spaces.
Type "help" for more information.
> 

 

コンソール出力

コンソール出力してみます。

> Log To Console  ハローワールド
ハローワールド

 

変数を使う

変数を使ってみます。

> ${ham} =  Set Variable  spam
# ${ham} = 'spam'
> Log To Console  ${ham}
spam

 

入力履歴

履歴も残っているようで、同じコマンドを打つと、前回のものが表示されます。

f:id:thinkAmi:20171216074446p:plain

 
右矢印キーを押すと、補完されます。

f:id:thinkAmi:20171216080823p:plain

 

入力補完

ある程度入力し、 Tab キーを押すと、入力候補が表示されます。

f:id:thinkAmi:20171216074745p:plain

 

インポートされているライブラリの確認

インポートされているライブラリを見てみます。 libs (もしくは l ) で表示されます。

> libs
< Imported libraries:
   BuiltIn 3.0.2
       An always available standard library with often needed keywords.
   DebugLibrary 1.0.2
       Debug Library for RobotFramework
   Easter 
   Reserved 
< Bultin libraries:
   BuiltIn 
   Collections 
   DateTime 
   Dialogs 
   Easter 
   OperatingSystem 
   Process 
   Remote 
   Reserved 
   Screenshot 
   String 
   Telnet 
   XML 

 

ヘルプの表示

どんなコマンドが用意されているかヘルプを表示してみます。

> help
Input Robotframework keywords, or commands listed below.
Use "libs" or "l" to see available libraries,
use "keywords" or "k" see list of library keywords,
use the TAB keyboard key to auto complete keywords.

Documented commands (type help <topic>):
========================================
EOF  exit  help  k  keywords  l  libs  pdb  s  selenium

 
Seleniumがあるみたいです。さらにヘルプを表示してみます。

> help selenium
Start a selenium 2 webdriver and open url in browser you expect.

        s(elenium)  [<url>]  [<browser>]

        default url is google.com, default browser is firefox.

 
そういえば、SeleniumLibraryをインストール済でした。

(rfenv363) $ pip list
...
robotframework-seleniumlibrary (3.0.1)

 
そこで、Seleniumを使ってみます。

> selenium
# import library  Selenium2Library
! keyword: import library  Selenium2Library
! Importing test library 'Selenium2Library' failed: ModuleNotFoundError: No module named 'Selenium2Library'
Traceback (most recent call last):
  None
PYTHONPATH:
  /path/to/rfenv363/bin
...
  /path/to/rfenv363/lib/python3.6/site-packages
# open browser  http://www.google.com/  firefox
! keyword: open browser  http://www.google.com/  firefox
! No keyword with name 'open browser' found.

Selenium2Libraryをインストールしていないので、起動に失敗したようです。 手元のSeleniumLibraryではダメなようですね。

 

REPLの終了

Ctrl + D にて終了します。

> (Ctrl + D を入力)

>>>>> Exit shell.
RFDEBUG REPL    | PASS |
----------------------------
Robot Debug Xgd1T H    | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
====================
Output:  None

 

Robot Frameworkのテストを実行中に、REPLを試す

Robot Framworkのテストを実行中に、REPLを使ってデバッグしたいことがあるかもしれません。

テストケース中にて Debug キーワードを使うことで、その場所の状態でREPLが起動します。

以下のテストケースで試してみます。

*** Settings ***
Library  DebugLibrary
Library  SeleniumLibrary

*** Keywords ***
say
    Log To Console  hello, world

ハロー
    Log To Console  はろー

testハロー
    Log To Console  testです

*** Test Cases ***
何かテストする
    Log To Console  hello
    ハロー
    ${ham} =  Set Variable  変数です
    
    # この時点の状態で、REPLが起動する
    Debug

    ${spam} =  Set Variable  書き換え可能です

 

REPLの起動

テストを実行すると、 Debug キーワードがあるところで自動的に停止します。

$ robot test_debug_library.robot 
===========================
Test Debug Library
===========================
何かテストする    hello
.はろー
..
>>>>> Enter interactive shell
Only accepted plain text format keyword seperated with two or more spaces.
Type "help" for more information.
> 

 

キーワードを使ってみる

さきほどのREPLと同じです。

> Log To Console  こんにちは
こんにちは

 

変数の中身を確認する

ためしに変数名を入力してみます。

> ${ham}
! keyword: ${ham}
! FAILED: TypeError("run_keyword() missing 1 required positional argument: 'name'",)

 
キーワードを入力しないといけないみたいですね。

> Log To Console  ${ham}
変数です

テスト中の変数の中身が出ました。

 

変数の中身を書きかえる

テストケース中の変数も書きかえられます。

> ${ham} =  Set Variable  書きかえました
# ${ham} = '書きかえました'
> 

 
REPLを抜けると変数が書きかわっています。

>>>>> Exit shell.
.書きかえました

 
参考:書きかえない場合

>>>>> Exit shell.
.変数です

 

ユーザ定義のキーワードを使う

Keywordssay キーワードを定義していたので使ってみます。

> say
hello, world

動きました。

日本語キーワードも問題ないです。

> ハロー
はろー

> testハロー
testです

 

importされているライブラリのキーワード使用

importされているライブラリのキーワードも使用できます。

今回はSeleniumLibraryをimportしているので、 Open Browser https://google.co.jp chrome とするとChromeが起動します。

> Open Browser  https://google.co.jp  chrome
< 1

 

入力補完

入力補完も可能です。

このテストケースではSeleniumLibraryをimportしているため、その分も候補に表示されます。

f:id:thinkAmi:20171216083740p:plain

 
なお、入力候補間の移動は

  • 進む : Tab
  • 戻る : Shift + Tab

です。

 

REPLの終了

Ctrl + D で終了します。

exit でも大丈夫なようです。

> help exit
Exit the interpreter. You can also use the Ctrl-D shortcut.

 
REPLを終了すると、テストが続きから実行されます。

> (Ctrl + Dを入力)

>>>>> Exit shell.
.変数です
何かテストする    | PASS |
--------------------
Test Debug Library    | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed

 
テストケース中でいろいろとできるのは便利ですね。