使用 Resource - daniel-qa/RobotFramework GitHub Wiki

p.s Resource 檔案,如果在被呼叫的 keyword 檔案中,又再引入一次,會有名稱衝突的問題( Multiple keywords with name)

p.s 只要在主要呼叫的程式中,引入共用函式,其他的先註解

p.s Resource 的 keyword 最好有預設值,避免空值時的異常狀況

资源文件

资源文件应该只包含关键字和变量,而不应该包含测试用例或任务。

这是因为资源文件的主要目的是存储可重用的关键字,以便在不同的测试套件中引用和调用。

  • Resource 文件範本

登入_Resource.robot

*** Settings ***
Documentation     登入
Library           SeleniumLibrary

*** Variables ***
${LOGIN_URL}      https://url.net
${USERNAME}       guest
${PASSWORD}       guest

*** Keywords ***
登入
    [Documentation]    執行登入動作
    [Arguments]    ${url}=${LOGIN_URL}
    Open Browser    ${url}    chrome
    Maximize Browser Window
    Sleep    5s
    Click Element     xpath=//img[contains(@src,'/img/icon_teacher')]
    Input Text        id=tmdID    ${USERNAME}
    Input Password    id=tmdpw    ${PASSWORD}
    Click Element     xpath=//div[@id='login']/div[3]/div/div[2]/form/div[2]/div/div/span/i
    #Element Should Be Visible    xpath=//div[contains(text(),'Welcome')]

demotest
    [Documentation]    Resource Test
    Log    Resource test
  • 呼叫 Resource 文件

main.robot

*** Settings ***
Resource    登入_Resource.robot

*** Test Cases ***
Example Test
	demotest
	登入
        ${LOGIN_URL}=    Set Variable    https://myurl.net
	登入    ${LOGIN_URL}

${CURDIR} 是Robot Framework内置的变量,用于表示当前目录。

使用相對路徑

*** Settings ***

Resource    ExternalKeywords.robot         # 當前目錄

Resource    Resources\\Subdirectory\\ExternalKeywords.robot    # 子目錄

Resource    ${CURDIR}\\ExternalKeywords.robot    # 使用 ${CURDIR},取得當下路徑的完整目錄