【问题标题】:Using Robot Framework to pick an item from select2 with remote data使用 Robot Framework 从 select2 中选择一个带有远程数据的项目
【发布时间】:2020-03-23 11:13:42
【问题描述】:

我正在尝试以编程方式从从远程数据填充的 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

本意是从“Loading remote data”部分打开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 得到相同的结果。

【问题讨论】:

    标签: robotframework select2


    【解决方案1】:

    Maybe with Javascript ?

    只需使用 select2 函数。

    【讨论】:

    • 我宁愿使用带有 CSS 选择器的 Robot Framework 关键字。对于这种测试,通过 JavaScript 触发 select2 似乎有点太低级了。无论如何感谢您提到这一点,我不知道它在另一种情况下可能会派上用场。
    【解决方案2】:

    这里的方法是使用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
    

    【讨论】:

      【解决方案3】:

      这应该可行

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-09
        • 1970-01-01
        • 2015-12-13
        • 1970-01-01
        相关资源
        最近更新 更多