Robotframework 使用Robot框架从select2中选择具有远程数据的项目

Robotframework 使用Robot框架从select2中选择具有远程数据的项目,robotframework,select2,Robotframework,Select2,我试图通过编程从select2下拉列表中选择一个项目,该下拉列表由远程数据填充 以下是基于select2演示页面的两个测试用例: *** Settings *** Library Selenium2Library *** Variables *** ${URL} https://select2.github.io/examples.html ${BROWSER} Chrome *** Test Cases *** Test select2 inp

我试图通过编程从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
*** 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
其目的是从“加载远程数据”部分打开select2输入,输入“robotframework”,最后选择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上得到了相同的结果。


只需使用select2功能。

这里的方法是使用
Click元素
关键字。我的第一个测试用例不起作用,因为它依赖于一个id(
select2-aiw0-results
),该id由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
*** 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
这应该行得通

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

我宁愿使用带有CSS选择器的Robot Framework关键字。通过JavaScript触发select2对于这种测试来说似乎有点太低了。无论如何,谢谢你提到这一点,我不知道这一点,它可以在其他情况下派上用场。