在SideeX中,區域變數可使用store系列指令來宣告與設定其值,以下面指令為例:
storeText | //input[@name='q'] | var_keyword
此指令執行後將宣告出一個變數var_keyword
,並且將其值設定為目標元素的文字(例如iphone
)。此變數可在同一個測試案例中的其他指令之Target與Value欄位中使用,使用格式為${var_keyword}
。
全域變數可於SideeX錄製器中下方Global Var
面板上宣告與設定初始值,此動作可手動將變數一一加入或透過匯入功能自外部JSON或CSV檔案匯入,同樣地,面板上的全域變數亦可匯出成檔案。此外,可使用storeGlobalVar
指令來宣告一個新的全域變數,或變更來自面板上全域變數的值。以下範例將產生一個全域變數GlobalVarA
並將其值設定為newValue
。
storeGlobalVar | newValue | GlobalVarA
我們不建議混合使用區域變數與全域變數,亦即區域變數名稱與全域變數名稱相同,但若須混合使用,以下為系統之執行規則。
v is predefined as ↓ | A. store|x|v | B. storeGlobalVar|x|v | C. storeXXX|x|v |
---|---|---|---|
1. local variable | Update the local variable | Create a global variable | Update the local variable |
2. global variable | Create a local variable | Update the global variable | Update the global variable |
3. local and global variables | Update the local variable | Update the global variable | Update the local variable |
4. not yet defined | Create a local variable | Create a global variable | Create a local variable |
底下為混合使用範例:
storeGlobalVar | globalStr | Var1
storeText | Google| Var1
storeText | SideeX | Var2
store | localStr | Var1
storeText | SideeX | Var1
此範例執行過程如下說明:
Var1
with value globalStr
. (規則B4
)Var1
to Google
. (規則C2
)Var2
with value SideeX
. (規則C4
)Var1
with value localStr
. (規則A2
)Var1
to SideeX
. (規則C3
)在Target與Value欄位中若出現變數引用,會將此變數值轉成字串,並取代原變數引用。例如:
store | Tom | name
echo | hello ${name} |
當執行時,Log將會顯示hello Tom
特別注意的是使用於Javascript表示式中,當變數值當作字串型態時須加上引號,例如:
runScript | if("${var_name}" == "Guest" ) ... |
若字串中已經包含單引號及雙引號,可以考慮使用String.raw()
runScript | if(String.raw`${var_name}` == "Guest" ) ... |
當變數值當作數字型態時不須加上引號,例如:
runScript | if(${var_num} == 100) ...|
若使用storeEval
指令來建立一個變數時,並且其Javascript表示式回傳值為陣列型態時,此變數被引用時將會被轉為一個JSON字串,範例如下:
storeEval | ["S","i","d","e","e","X"] | arr
storeEval | ${arr}.length | length
store | 0 | index
WHILE | ${index} < ${length}
storeEval | ${arr}[${index}] | show
echo | ${show}
storeEval | ${index} + 1 | index
END
此範例執行時將於Log依序顯示出陣列中的每個字母。
變數可巢狀引用,例如:
store | 1 | Ser
store | Hello SideeX | Msg1
echo | ${Msg${Ser}} |
執行過程如下:
Ser
的值將會被設定為1
Msg1
的值將會被設定為Hello SideeX
${Msg${Ser}}
將會先被轉成$Msg1
,接著轉成Hello SideeX
,然後於Log顯示出。將目標元素儲存至一個區域變數。此變數可以在其他指令的Target與Value欄位透過${}語法被使用,當中目標元素的屬性亦可被引用,例如:
storeElement | //h1[1] | elem
echo | ${elem.innerHTML} |
假設目標 //h1[1] 中的元素elem屬性innerHTML為Hello SideeX
。
當執行時,Log將會顯示Hello SideeX
。
亦可配搭其他變數作巢狀變數引用,例如:
Name | Telephone |
---|---|
Mary | 5201314 |
Peter | 0594184 |
storeElement | //table[0] | elem
store | 0 | count
WHILE | ${count}<${elem.rows.length} |
echo | ${elem.rows[${count}].innerText} |
storeEval | ${count}+1 |
END
當執行時,Log將會顯示:
Name Telephone
Mary 5201314
Peter 0594184
特別注意的是指令Target會影響變數的準確性,建議可配合TAC功能使用。
若您有多筆資料儲存於外部檔案中,將可透過Global Var
面板將其匯入成多個全域變數,然後於測試案例中引用,並搭配WHILE
、INCLUDE
或巢狀變數引用
以達成資料驅動測試。底下為使用步驟:
Global Var
面板匯入一個JSON格式檔案,例如:匯入後將自動建立下列全域變數:
Name | Value |
---|---|
numberOfRecords | 2 |
record.0.userName | User1 |
record.0.loginMsg | Welcome |
record.1.userName | User2 |
record.1.loginMsg | Please signup |
巢狀變數引用
來參照匯入的變數(變數index
將在步驟3中定義)ExampleTestSuite.MainTestCase
echo | ${record.${index}.userName} |
echo | ${record.${index}.loginMsg} |
Include
測試案例MainTestCase,MainTestCase中的index
將被依序代入0
與1
,並顯示此兩筆record的值。ExampleTestSuite.LeadingTestCase
store | 0 | index
WHILE | ${index} < ${numberOfRecords} |
INCLUDE | ExampleTestSuite.MainTestCase |
storeEval | ${index}+1 | index
END
測試案例執行結束後,區域變數與全域變數可於測試報告檔案中取得,請參閱測試報告格式。