簡體   English   中英

使用 Robot Framework 從帶有遠程數據的 select2 中選取一個項目

[英]Using Robot Framework to pick an item from select2 with remote data

我正在嘗試以編程方式從遠程數據填充的 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”,最后選擇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 上得到了相同的結果。

也許用 Javascript ?

只需使用 select2 功能。

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

這應該工作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM