使用Robot Framework从select2中选择具有远程数据

时间:2016-11-03 15:39:06

标签: robotframework select2

我正在尝试以编程方式从从远程数据填充的select2下拉列表中选择一个项目。

以下是基于select2演示页面的几个测试用例:

*** Settings ***
Library         Selenium2Library

*** Variables ***
${URL}       https://select2.github.io/examples.html
${BROWSER}        Chrome


*** Test Cases ***

Test select2 input with click
    Open browser    ${URL}  ${BROWSER}
    Wait Until Page Contains    Loading remote data
    Click Element   xpath=/html/body/div/div/div[1]/section[3]/p[4]/span
    Input Text      xpath=/html/body/span/span/span[1]/input      robotframework
    Wait Until Page Contains    Generic test automation
    Click Element   xpath=//*[@id="select2-aiw0-results"]/li


Test select2 input with select from
    Open browser    ${URL}  ${BROWSER}
    Wait Until Page Contains    Loading remote data
    Click Element   xpath=/html/body/div/div/div[1]/section[3]/p[4]/span
    Input Text      xpath=/html/body/span/span/span[1]/input      robotframework
    Wait Until Page Contains    Generic test automation
    Select From List By Index   xpath=/html/body/span         0

目的是打开“加载远程数据”部分的select2输入,输入“robotframework”,最后选择机器人框架项。这是最新的行动,我无法弄清楚如何正确地做。这是我从Robot Framework获得的输出:

$ robot select2.robot 
==============================================================================
Select2                                                                       
==============================================================================
Test select2 input with click                                         | FAIL |
ValueError: Element locator 'xpath=//*[@id="select2-aiw0-results"]/li' did not match any elements.
------------------------------------------------------------------------------
Test select2 input with select from                                   | FAIL |
ValueError: Element locator 'xpath=/html/body/span' did not match any elements.
------------------------------------------------------------------------------
Select2                                                               | FAIL |
2 critical tests, 0 passed, 2 failed
2 tests total, 0 passed, 2 failed
==============================================================================
Output:  /home/al/essai/robotframework/output.xml
Log:     /home/al/essai/robotframework/log.html
Report:  /home/al/essai/robotframework/report.html

我在Chrome和Firefox上获得了相同的结果。

3 个答案:

答案 0 :(得分:1)

Maybe with Javascript ?

只需使用select2功能。

答案 1 :(得分:0)

此处的方法是使用Click Element关键字。我的第一个测试用例不起作用,因为它依赖于select2动态生成的id(select2-aiw0-results),并且在每次执行时都是不同的。

这是一个针对select2演示页面的测试用例:

*** Settings ***
Library         Selenium2Library

*** Variables ***
${URL}       https://select2.github.io/examples.html
${BROWSER}        Chrome


*** Test Cases ***

Test select2 input with click
    Open browser    ${URL}  ${BROWSER}
    Wait Until Page Contains    Loading remote data
    # Click on the input
    Click Element   xpath=/html/body/div/div/div[1]/section[3]/p[4]/span
    # Enter text to trigger autocompletion
    Input Text      xpath=/html/body/span/span/span[1]/input      robotframework
    Wait Until Page Contains    Generic test automation
    # Select the first suggestion from the autocompletion list
    Click Element   css=.select2-results li:nth-child(1)
    # Check that the input contains text corresponding to the selected item
    Element Text Should Be  css=body > .container section:nth-child(3) .js-example-data-ajax + .select2-container .select2-selection__rendered    robotframework/robotframework
    Close Browser

答案 2 :(得分:0)

这应该有效

Select2 Input
[Arguments]     ${css}     ${text}
Execute Javascript   $("${css}").val("${text}"); $("${css}").select2().trigger('change');