Try   HackMD

變數

區域變數:使用store系列指令宣告

在SideeX中,區域變數可使用store系列指令來宣告與設定其值,以下面指令為例:

storeText | //input[@name='q'] | var_keyword

此指令執行後將宣告出一個變數var_keyword,並且將其值設定為目標元素的文字(例如iphone)。此變數可在同一個測試案例中的其他指令之Target與Value欄位中使用,使用格式為${var_keyword}

全域變數:使用Global Var面板或storeGlobalVar指令宣告

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

全域變數可於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

此範例執行過程如下說明:

  1. Create a global variable Var1 with value globalStr. (規則B4)
  2. Update the value of the global variable Var1 to Google. (規則C2)
  3. Create a local variable Var2 with value SideeX. (規則C4)
  4. Create a local variable Var1 with value localStr. (規則A2)
  5. Update the value of the local variable 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}} |

執行過程如下:

  1. 變數Ser的值將會被設定為1
  2. 變數Msg1的值將會被設定為Hello SideeX
  3. ${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面板將其匯入成多個全域變數,然後於測試案例中引用,並搭配WHILEINCLUDE巢狀變數引用以達成資料驅動測試。底下為使用步驟:

  1. Global Var面板匯入一個JSON格式檔案,例如:
{ "numberOfRecords":2, "record":[ { "userName":"User1", "loginMsg":"Welcome" }, { "userName":"User2", "loginMsg":"Please signup" } ] }

匯入後將自動建立下列全域變數:

Name Value
numberOfRecords 2
record.0.userName User1
record.0.loginMsg Welcome
record.1.userName User2
record.1.loginMsg Please signup
  1. 建立一個測試案例MainTestCase,並使用巢狀變數引用來參照匯入的變數(變數index將在步驟3中定義)

ExampleTestSuite.MainTestCase
echo | ${record.${index}.userName} |
echo | ${record.${index}.loginMsg} |

  1. 建立另一個測試案例重複Include測試案例MainTestCase,MainTestCase中的index將被依序代入01,並顯示此兩筆record的值。

ExampleTestSuite.LeadingTestCase
store | 0 | index
WHILE | ${index} < ${numberOfRecords} |
INCLUDE | ExampleTestSuite.MainTestCase |
storeEval | ${index}+1 | index
END

檔案輸出

測試案例執行結束後,區域變數與全域變數可於測試報告檔案中取得,請參閱測試報告格式