• 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

S-IDE では操作対象の要素を xpath で指定することができる。

例:xpathの明示
コマンド対象
clickxpath=//h2/a

対象要素を特定するこの機能は Element Locator と呼ばれ、 Selenium Core で提供されている。

   1  BrowserBot.prototype.findElement = function(locator, win) {
   2      var element = this.findElementOrNull(locator, win);
   3      if (element == null) throw new SeleniumError("Element " + locator + " not found");
   4      return element;
   5  }
(selenium-core-1.0.1/core/scripts/selenium-browserbot.js)

従って、S-IDE の対象指定以外での場所、 例えば自作関数内や getEval の中などで使いたい場合には、 上記の findElement を直接呼び出すとよい。

HP2703/2842
MP1413/1413

   1  function isHpFull(percent) {
   2    var path = '//tr[0]/td[1]';
   3    var hp   = selenium.browserbot.findElement(path).split('/');
   4    var hp1  = parseInt(hp[0]);
   5    var hp2  = parseInt(hp[1]);
   6    return ((hp1 * 100 / hp2) > percent);
   7  }

例:HPが90%以上ある場合は宿屋に入らない
コマンド対象
gotoIfisHpFull(90)skip_inn
clicklabel=宿屋
labelskip_inn

関連として、Selenium 環境ではない状態の Mozilla で xpath を扱うには、 document.evaluate が便利である。

   1  function xpath2text(path) {
   2    var result = document.evaluate(path, doc, null, 7, null);
   3    var item = result.snapshotItem(0);
   4    return item.innerHTML;
   5  }

参考

  • https://developer.mozilla.org/ja/Introduction_to_using_XPath_in_JavaScript
posted by Png maiha on Fri 25 Sep 2009 at 21:50

S-IDE でループや条件分岐といったフローコントロールを行うためのメモ

下策:TRをコピペ

[ユースケース] 無限でなくていいから適度にいますぐ

例:好きなだけコピペ
コマンド対象
open/users/new
typenamemaiha
typepassxxxxx
open/users/new
...

1,000オペレーションは全然大丈夫だったけど、 10,000ぐらい書いたら色々重くなったので注意

中策:flowControl extension を使う

[ユースケース] ループだけでなく条件分岐もできる

インストール

Andrey が作った flowControl は Selenium 用であるが、 それを Darren が S-IDE に移植した(goto_sel_ide.js) ので Selenium Core 拡張として読み込ませる。

  • http://51elliot.blogspot.com/2008/02/selenium-ide-goto.html

例:華麗に goto
コマンド対象
labelcreate_user
open/users/new
typenamemaiha
typepassxxxxx
gotocreate_user

上策:while で攻める

[ユースケース] goto はリアルで無限なので while で回数指定する

例:while で500ループ指定
コマンド対象
store0i
whilestoredVars.i++ < 500
open/users/new
typenamemaiha
typepassxxxxx
endWhile

flowControlのその他の命令及び詳細な使い方は

  • http://wiki.openqa.org/display/SEL/flowControl
posted by Png maiha on Mon 21 Sep 2009 at 17:40

自作関数をIDE上で利用するには、 定義した js ファイルを[オプション]→[設定]にある「Selenium Core 拡張スクリプト」で指定する。

user.js

   1  function eachTag(name, attr, value, func) {
   2    var win = selenium.page().getCurrentWindow();
   3    var tags = win.document.getElementsByTagName(name);
   4    for (var i=0; i<tags.length; i++) {
   5      if (tags[i].getAttribute(attr) == value) { func(tags[i]); }
   6    }
   7  }
   8  
   9  function reportErrors() {
  10    eachTag('div', 'class', 'fieldWithErrors' , function(e) {LOG.warn(e.innerHTML);});
  11  }

例:エラー内容のデバッグ表示
コマンド対象
open/users/new
typenamemaiha
clickAndWaitcreate
getEvalreportErrors()

例:ランダムな文字列とか
コマンド対象
open/users/new
typenamemaiha
typepassjavascript{randomWord(8)}

(※ "function randomWord(size)" が user.js に定義済として)

posted by Png maiha on Sun 20 Sep 2009 at 18:44

waitForCondition を使う

waitForCondition(script, timeout)
Arguments:

    * script - the JavaScript snippet to run
    * timeout - a timeout in milliseconds, after which this command will return with an error

停止条件は activeRequestCount で判定する。

例:PrototypeでAjaxリクエストを待つ
コマンド対象
clickfoo_button
waitForConditionselenium.browserbot.getCurrentWindow().Ajax.activeRequestCount == 05000

例:jQueryでAjaxリクエストを待つ
コマンド対象
clickfoo_button
waitForConditionselenium.browserbot.getCurrentWindow().jQuery.active == 05000

参考

  • http://d.hatena.ne.jp/cinquanta/20090501/1241675577
  • http://codelevy.com/2007/11/05/selenium-and-ajax-requests
posted by Png maiha on Thu 17 Sep 2009 at 10:41

「今日の日付」のような動的な値が必要な場合には "javascript{}" を利用する。 同ブロック内部の文字列は実行時に javascript コードとして評価され、 その評価結果が値として利用される。

例:いずれもabcが入力される
コマンド対象
typeval1abc
typeval1javascript{ "abc" }
typeval1javascript{ "ABC".toLowerCase() }

例:今日の日付を入力
コマンド対象
typedatejavascript{ var d=new Date(); d.getFullYear()+"/"+ ... }

posted by Png maiha on Wed 16 Sep 2009 at 19:03
click         ボタンやリンクをクリックする
clickAndWait  クリック後にページ遷移を待つ

例:ログイン(失敗)
コマンド対象
typeusermaiha
typepassxxxxx
clicklogin
verifyTextPresentようこそmaihaさん#ページ遷移が終わってないので失敗する

click を clickAndWait に変更すると成功する

posted by Png maiha on Wed 16 Sep 2009 at 06:46

pause を使う

pause(waitTime)
Arguments:
    * waitTime - the amount of time to sleep (in milliseconds)
Wait for the specified amount of time (in milliseconds)

例:3秒待つ
コマンド対象
pause3000

例:DHTMLの書き換えを待つ
コマンド対象
clicklink=詳細設定を開く
pause200
typesearch_number20

posted by Png maiha on Tue 15 Sep 2009 at 20:54