query: tag:selenium

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

|caption=例:xpathの明示
|
|コマンド,対象,値
|
|click,xpath=//h2/a,

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

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

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

|HP,2703/2842
|MP,1413/1413

javascript>>
function isHpFull(percent) {
var path = '//tr[0]/td[1]';
var hp = selenium.browserbot.findElement(path).split('/');
var hp1 = parseInt(hp[0]);
var hp2 = parseInt(hp[1]);
return ((hp1 * 100 / hp2) > percent);
}
<<--

|caption=例:HPが90%以上ある場合は宿屋に入らない
|
|コマンド,対象,値
|
|gotoIf,isHpFull(90),skip_inn
|click,label=宿屋,
|label,skip_inn,

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

ruby>>
function xpath2text(path) {
var result = document.evaluate(path, doc, null, 7, null);
var item = result.snapshotItem(0);
return item.innerHTML;
}
<<--

参考

posted by maiha maiha on Fri 25 Sep 2009 at 21:51 with 0 comments

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

user.js

javascript>>
function eachTag(name, attr, value, func) {
var win = selenium.page().getCurrentWindow();
var tags = win.document.getElementsByTagName(name);
for (var i=0; i<tags.length; i++) {
if (tags[i].getAttribute(attr) == value) { func(tags[i]); }
}
}

function reportErrors() {
eachTag('div', 'class', 'fieldWithErrors' , function(e) {LOG.warn(e.innerHTML);});
}
<<--

|caption=例:エラー内容のデバッグ表示
|
|コマンド,対象,値
|
|open,/users/new,
|type,name,maiha
|clickAndWait,create,
|getEval,reportErrors(),

|caption=例:ランダムな文字列とか
|
|コマンド,対象,値
|
|open,/users/new,
|type,name,maiha
|type,pass,javascript{randomWord(8)}

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

posted by maiha maiha on Sun 20 Sep 2009 at 18:26 with 0 comments

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 で判定する。

|caption=例:PrototypeでAjaxリクエストを待つ
|
|コマンド,対象,値
|
|click,foo_button,
|waitForCondition,selenium.browserbot.getCurrentWindow().Ajax.activeRequestCount == 0, 5000

|caption=例:jQueryでAjaxリクエストを待つ
|
|コマンド,対象,値
|
|click,foo_button,
|waitForCondition,selenium.browserbot.getCurrentWindow().jQuery.active == 0, 5000

参考

posted by maiha maiha on Thu 17 Sep 2009 at 10:04 with 0 comments

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

|caption=例:いずれもabcが入力される
|
|コマンド,対象,値
|
|type,val1,abc
|type,val1,javascript{ "abc" }
|type,val1,javascript{ "ABC".toLowerCase() }

|caption=例:今日の日付を入力
|
|コマンド,対象,値
|
|type,date,javascript{ var d=new Date(); d.getFullYear()+"/"+ ... }

posted by maiha maiha on Wed 16 Sep 2009 at 18:29 with 0 comments
click         ボタンやリンクをクリックする
clickAndWait  クリック後にページ遷移を待つ

|caption=例:ログイン(失敗)
|
|コマンド,対象,値,
|
|type,user,maiha,
|type,pass,xxxxx,
|click,login,,
|verifyTextPresent,ようこそmaihaさん,,#ページ遷移が終わってないので失敗する

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

posted by maiha maiha on Wed 16 Sep 2009 at 06:27 with 0 comments

pause を使う

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

|caption=例:3秒待つ
|
|コマンド,対象,値
|
|pause,3000,

|caption=例:DHTMLの書き換えを待つ
|
|コマンド,対象,値
|
|click,link=詳細設定を開く,
|pause,200,
|type,search_number,20

posted by maiha maiha on Tue 15 Sep 2009 at 20:36 with 0 comments