如何在 Robot Framework 中单击并按住按钮?以及如何获取按钮的背景颜色?

问题描述 投票:0回答:1

我刚刚开始学习自动化,robot是我的第一个框架,我正在实践中解决letcode.in的一些测试。但我无法解决第二次测试中的两个问题:1)获取按钮的背景颜色2)按住按钮。我将不胜感激任何帮助或意见。

*** Settings ***
Documentation    First robot test
Library          SeleniumLibrary
Library    .venv/Lib/site-packages/robot/libraries/XML.py


*** Variables ***
${URL}    https://letcode.in/buttons
${BROWSER}        Chrome

${hold_button}    css://#color

*** Keywords ***
Prepare for test
    Open Browser    ${URL}    ${BROWSER}
    Set Selenium Speed    3 seconds


*** Test Cases ***
Buttons
    Prepare for test
    Maximize Browser Window

    Click Button    id:home
    Go Back

    ${y_coord}=    Get Vertical Position    id:position
    ${x_coord}=    Get Horizontal Position    id:position
    Log    Y coordinate: ${y_coord}
    Log    X coordinate: ${x_coord}


    # ${color}=    Get Element Attribute    id:color    style.background-color
    # Log    Button color:${color}

    ${tall&fat}=    Get Element Size    id:property
    Log    Button sizes: ${tall&fat}

    Element Should Be Disabled    id:isDisabled

    # Click Button    ${hold_button}
    # Click Element   ${hold_button}    
    # Sleep    3
    

我已经评论了我最近的尝试,我是初学者,所以我不确定我是否擅长阅读文档和在互联网上搜索信息。

selenium-webdriver automation robotframework browser-automation
1个回答
0
投票

此代码目前对我有用:

*** Settings ***
Documentation    First robot test
Library          SeleniumLibrary


*** Variables ***
${URL}            https://letcode.in/buttons
${BROWSER}        Chrome
${HOLD_BUTTON}    //button[contains(., 'Button')]

*** Test Cases ***
Buttons
    [Documentation]    Test case
    Prepare For Test
    Maximize Browser Window
    Click Button    id:home
    Go Back
    ${y_coord}=    Get Vertical Position    id:position
    ${x_coord}=    Get Horizontal Position    id:position
    Log    Y coordinate: ${y_coord}
    Log    X coordinate: ${x_coord}
    ${color}=    Get Element Attribute    id:color    style.background-color
    Log    Button color:${color}
    ${tall&fat}=    Get Element Size    id:property
    Log    Button sizes: ${tall&fat}
    Element Should Be Disabled    id:isDisabled
    # Click Element   ${HOLD_BUTTON}
    Mouse Down        ${HOLD_BUTTON}
    Wait Until Page Contains    Button has been long pressed    timeout=5s
    Mouse Up    ${HOLD_BUTTON}

*** Keywords ***
Prepare For Test
    Open Browser            ${URL}    ${BROWSER}
    Set Selenium Speed      1 seconds
© www.soinside.com 2019 - 2024. All rights reserved.