How to use Library as a variable

时间:2019-04-08 14:02:14

标签: robotframework

I have a project in RobotFramework that already have more than 50 tests created. And I want to use AppiumLibrary or Selenium2Library according a type of test. I would like to avoid have the same test repeated, just because the Selenium2Library don't work on Mobile

*** Settings ***
Library           AppiumLibrary
Library           Selenium2Library

*** Test Cases ***
TC001
    [Setup]    Configuration
    Pause Execution    ${globalType}|${globalLibrary}
    Start Test
    ${globalLibrary}.Wait Until Page Contains Element    id=age-gate-yes    60s
    Sleep    3s
    ${globalLibrary}.Click Element    xpath=//a[@id='age-gate-yes']
    ${globalLibrary}.Wait Until Page Contains Element    xpath=//a[@href="/profile"]    60s
    ${globalLibrary}.Click Element    xpath=//a[@href="/profile"]
    ${globalLibrary}.Wait Until Page Contains Element    xpath=//input[@name="email"]    60s
    ${globalLibrary}.Click Element    xpath=//input[@name="email"]
    ${globalLibrary}.Input Text    xpath=//input[@name="email"]    johndoe@john.doe
    ${globalLibrary}.Click Element    xpath=//input[@name="password"]
    ${globalLibrary}.Input Text    xpath=//input[@name="password"]    JohnDoe123
    ${globalLibrary}.Click Element    xpath=//div[@id='react-component-login-header']//button/div/span[1]
    ${globalLibrary}.Wait Until Page Does Not Contain Element    xpath=//div[@id='react-component-login-header']//button/div/span[1]    15s
    [Teardown]    End Test

*** Keywords ***
Configuration
    Set Global Variable    ${globalType}    ${Type}
    Run Keyword If    '${Type}'=='Desktop'    Set Global Variable    ${globalLibrary}    Selenium2Library
    Run Keyword If    '${Type}'=='Mobile'    Set Global Variable    ${globalLibrary}    AppiumLibrary
    Pause Execution    ${globalType}|${globalLibrary}

Start Test
    Run Keyword If    '${Type}'=='Desktop'    Start Desktop
    Run Keyword If    '${Type}'=='Mobile'    Start Mobile

End Test
    Run Keyword If    '${Type}'=='Desktop'    End Desktop
    Run Keyword If    '${Type}'=='Mobile'    End Mobile

Start Mobile
    AppiumLibrary.Open Application    http://127.0.0.0:4444/wd/hub    platformName=iOS    platformVersion=12.1    deviceName=iPhone Simulator    browserName=Safari
    AppiumLibrary.Go To Url    https://www.somewebsite.com/

Start Desktop
    Selenium2Library.Open Browser    https://www.somewebsite.com/    Chrome
    Maximize Browser Window

End Mobile
    Close All Applications

End Desktop
    Close All Browsers

I was hoping that the global variable could fullfill the Library, however I am getting:

TC001                                                                 | FAIL |
No keyword with name '${globalLibrary}.Wait Until Page Contains Element' found. Did you mean:
    AppiumLibrary.Wait Until Page Contains Element

1 个答案:

答案 0 :(得分:4)

您可以使用导入库(from Builtin)动态实例化所需的库,而不是将库名放入变量中:

Configuration
    Run Keyword If    '${Type}'=='Desktop'    Import Library     Selenium2Library
    Run Keyword If    '${Type}'=='Mobile'     Import Library     AppiumLibrary

通过这种方式,无论是台式机测试还是移动测试,它都将非常灵活。