節省那惱人的試驗時間

之前在透過Selenium發送Ctrl+A時,先透過了Firefox Selenium IDE做試驗,再去撰寫Robot。但遇到的問題是,

  1. 待測瀏覽器與試驗瀏覽器XPath有差異。
  2. Selenium指令Robot與Selenium IDE有差異。
  3. 使用Robot跑一次測試的時間過長。

為了解省這些時間,於是我想透過Runtime發送指令的方式,去解決這個問題。

我想到的方式是:

  1. 導到要試驗的測試案例頁面。
  2. 讀取一個檔案內容。
  3. 確認檔案內容指令。
  4. 執行指令。若遇到EXIT即離開。

在我的測試程式中,

  1. 接著我會在程式將讀取的檔案中,輸入指令。
  2. 測試程式將會執行這些指令。
  3. 最後輸入EXIT離開程式。

看看我預先拍好的Demo視頻: https://www.youtube.com/watch?v=vC8hk-igWCw&feature=youtu.be

*** Settings ***
Library           Collections
Library           String
Library           OperatingSystem
Library           SeleniumLibrary
 
*** Test Cases ***
test
    [Setup]    Start Selenium Server
    Open Browser    http://www.internic.net/whois.html    googlechrome
    Test Select All
    [Teardown]    Run Keywords    Close All Browsers    Stop Selenium Server
 
*** Keywords ***
Test Select All
    ${filePath}    Set Variable    C:/Users/TonyLin/Desktop/test.txt
    ${preModifiedTime}    Set Variable    ${EMPTY}
    : FOR    ${index}    IN RANGE    100000
    \    Sleep    1s
    \    @{checkResult}    Run Keyword And Ignore Error    Should Exist    ${filePath}
    \    Continue For Loop If    '@{checkResult}[0]'!='PASS'
    \    ${modifiedTime}    Get Modified Time    ${filePath}
    \    Continue For Loop If    '${modifiedTime}'=='${preModifiedTime}'
    \    ${preModifiedTime}    Set Variable    ${modifiedTime}
    \    ${content}    Get File    ${filePath}
    \    @{lines}    Split To Lines    ${content}
    \    ${length}    Get Length    ${lines}
    \    Run Keyword If    ${length}!=0    log    command=@{lines}[0]
    \    @{checkResult}    Run Keyword And Ignore Error    Run Keyword If    ${length}!=0    Should Be Equal    EXIT
    \    ...    @{lines}[0]
    \    Exit For Loop If    '@{checkResult}[0]'=='PASS' and ${length}!=0
    \    Run Keyword And Ignore Error    Run Keyword If    ${length}!=0    Run Command    @{lines}[0]
 
Run Command
    [Arguments]    ${command}
    @{tokens}    Split String    ${command}    |
    ${keyword}    Set Variable    @{tokens}[0]
    Remove From List    ${tokens}    0
    Run Keyword And Ignore Error    Run Keyword    ${keyword}    @{tokens}

不管在練習新的Keyword、試驗XPath或是要確認撰寫是否正確,Runtime的執行的確是可以節省許多重新讀取所花費的時間。但在使用一陣子後,目前有幾點問題:

  1. 對於執行成功的指令該如何處理?
  2. report的內容中,會有相當多無意義的loop log。
  3. 如果要一次執行多個指令呢?

第一個問題,我們可以將執行成功的指令,輸出到另外一個檔案,也可以直接去從log中複製貼上。如果從log中去複製貼上,就會遇到第二個問題。
第二個問題可以透過延長log時間,但也等於延長了讀取到指令的時間。之前也想過可以透過Socket方式將指令發送過去執行,但目前還沒有急迫的需求,有需要的可以使用這個方法試試看。另外一個方法是將讀取指令的動作另外寫成一個Keyword,但撰寫會複雜許多。
第三個問題純粹是讀取指令後的處理,只要針對讀取內容做處理即可。

友藏內心獨白: 最近都在思考如何在有限時間做很多事情。