--- title: Using Variables in Rapi tags: Rapi Document Chinese description: Using Variables in Rapi --- # 變數 ## 區域變數:使用store系列指令宣告 在Rapi中,區域變數可使用store系列指令來宣告與設定其值,以下面指令為例: `storeText | //input[@name='q'] | var_keyword` 此指令執行後將宣告出一個變數`var_keyword`,並且將其值設定為目標元素的文字(例如`iphone`)。此變數可在同一個測試案例中的其他指令之Target與Value欄位中使用,使用格式為`${var_keyword}`。 ## 全域變數:使用Global Var面板或storeGlobalVar指令宣告  全域變數可於Rapi錄製器中下方`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| 底下為混合使用範例: :::info storeGlobalVar | globalStr | Var1 storeText | Google| Var1 storeText | Rapi | Var2 store | localStr | Var1 storeText | Rapi | 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 `Rapi`. (規則`C4`) 4. **Create** a local variable `Var1` with value `localStr`. (規則`A2`) 5. **Update** the value of the local variable `Var1` to `Rapi`. (規則`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字串,範例如下: :::info storeEval | return ["S","i","d","e","e","X"] | arr storeEval | return \${arr}.length | length store | 0 | index WHILE | \$\{index\} < \$\{length\} storeEval | return \$\{arr\}\[\$\{index\}\] | show echo | \$\{show\} storeEval | return \$\{index\} + 1 | index END ::: 此範例執行時將於Log依序顯示出陣列中的每個字母。 ### 巢狀變數引用 變數可巢狀引用,例如: :::info store | 1 | Ser store | Hello Rapi | Msg1 echo | \${Msg${Ser}} | ::: 執行過程如下: 1. 變數`Ser`的值將會被設定為`1` 2. 變數`Msg1`的值將會被設定為`Hello Rapi` 3. `${Msg${Ser}}`將會先被轉成`$Msg1`,接著轉成`Hello Rapi`,然後於Log顯示出。 ### 元素變數引用 將目標元素儲存至一個區域變數。此變數可以在其他指令的Target與Value欄位透過${}語法被使用,當中目標元素的屬性亦可被引用,例如: `storeElement | //h1[1] | elem` `echo | ${elem.innerHTML} |` 假設目標 //h1[1] 中的元素elem屬性innerHTML為`Hello Rapi`。 當執行時,Log將會顯示`Hello Rapi`。 亦可配搭其他變數作巢狀變數引用,例如: | Name | Telephone | | -------- | -------- | | Mary | 5201314 | | Peter | 0594184 | :::info storeElement | //table[0] | elem store | 0 | count WHILE | \${count}<\${elem.rows.length} | echo | \${elem.rows[\${count}].innerText} | storeEval | return \${count}+1 | END ::: 當執行時,Log將會顯示: `Name Telephone` `Mary 5201314` `Peter 0594184` 特別注意的是指令Target會影響變數的準確性,建議可配合TAC功能使用。 ## 檔案輸入 若您有多筆資料儲存於外部檔案中,將可透過`Global Var`面板將其匯入成多個全域變數,然後於測試案例中引用,並搭配`WHILE`、`INCLUDE`或`巢狀變數引用`以達成資料驅動測試。底下為使用步驟: 1. 至`Global Var`面板匯入一個JSON格式檔案,例如: ```jsonld= { "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 | 2. 建立一個測試案例MainTestCase,並使用`巢狀變數引用`來參照匯入的變數(變數`index`將在步驟3中定義) :::info **ExampleTestSuite.MainTestCase** echo \| \${record.\${index}.userName} \| echo \| \${record.\${index}.loginMsg} \| ::: 3. 建立另一個測試案例重複`Include`測試案例MainTestCase,MainTestCase中的`index`將被依序代入`0`與`1`,並顯示此兩筆record的值。 :::info **ExampleTestSuite.LeadingTestCase** store \| 0 \| index WHILE \| \${index} < ${numberOfRecords} \| INCLUDE \| ExampleTestSuite.MainTestCase \| storeEval \| return \${index}+1 \| index END :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up