使用webdriver实现基于GUI的测试

2024-09-10 15:15:26 浏览数 (4)

前面介绍了如何使用Selenium2Library实现基于GUI的测试,Selenium2Library是Selenium的插件,而本文介绍的webdriver是playwright的插件

代码语言:javascript复制
*** Settings ***
Library           Browser
*** Test Cases ***

基本测试

代码语言:javascript复制
    New Page    https://www.baidu.com
    ${SearchWords}    Set Variable    软件测试
    Fill Text    id=kw    ${SearchWords}
    Click    id=su
    Get Title    contains    ${SearchWords}_百度搜索
    Close Page

Frame切换

代码语言:javascript复制
    Set Browser Timeout    50000    # Timeout为隐私等待时长
    New Page    http://www.3testing.com/
    Click    id=head >>> id=introduce  #>>>为Frame切换
    Get Title    contains    顾翔
    Close Page

Windows切换

代码语言:javascript复制
    New Page    https://www.baidu.com
    Click    xpath=//*[@id="s-top-left"]/div/a
    Close page
    Get Title    contains    百度产品大全
    Close Page

鼠标悬停

代码语言:javascript复制
    New Browser    firefox    headless=false  #使用firefox,不使用无头浏览器进行测试
    ${SearchWords}    Set Variable    软件测试
    New Page    http://www.baidu.com
    Fill Text    id=kw    ${SearchWords}
    Click    id=su
    Get Title    contains    ${SearchWords}_百度搜索
    Hover    xpath=//*[@id="u"]/a[2]  #Hover鼠标悬停
    Click    css=#u > div.bdpfmenu > a.last > span
    Close Page
    Close Browser

窗口滚动

代码语言:javascript复制
    New Browser    headless=false
    New Page    https://www.jd.com
    Hover    body
    Mouse Wheel    0    5000  #Mouse Wheel窗口滚动
    Sleep    5
    Close Page
    Close Browser

键盘操作

代码语言:javascript复制
New Page    https://www.baidu.com
    ${SearchWords}    Set Variable    软件测试
    Fill Text    id=kw    ${SearchWords}
    Keyboard Key    press    Enter  #Enter回车
    Wait For Condition    Title    should start with    ${SearchWords}
    ${title}    Get Title
    Should be Equal    ${title}    ${SearchWords}_百度搜索
    Close Page

访问电子商务

代码语言:javascript复制
New Page    http://127.0.0.1:8000
    Fill Text    css=input[name=username]    cindy
    Fill Text    css=input[name=password]    123456
    Click    css=body > div > form > button
    ${title}    Get Title
    Should be Equal    ${title}    电子商务系统
    Close Page

cookies操作

代码语言:javascript复制
New Page    http://127.0.0.1:8000
    Fill Text    css=input[name=username]    cindy
    Fill Text    css=input[name=password]    123456
    Click    css=body > div > form > button
    ${title}    Get Title
    Should be Equal    ${title}    电子商务系统
    Add Cookie    1    1    http://127.0.0.1:8000  #设置Cookie
    Go To    http://127.0.0.1:8000/goods_view/
    Get Text    xpath=//*[@id="navbar"]/ul[1]/li[3]/a    contains    查看购物车1
    Click    css=#navbar > ul:nth-child(1) > li:nth-child(3) > a
    Get Attribute    [value=修改]    type
    Close Page

单复选框

代码语言:javascript复制
    New Page    http://127.0.0.1:8080/sec/33/index.html
    Click    xpath=//input[@type='radio' and @value='Traditional_Chinese']
    Check Checkbox    xpath=//input[@type='radio' and @value='Traditional_Chinese']    force=true
    ${condition}    Check Checkbox    id=c1    force=false
    Run Keyword If    '${condition}' == 'true'    Log    复选框已被选中
    ...    ELSE    Click    id=c1
    Check Checkbox    id=c1    force=true
    Close Page

选择复选框所有选项

代码语言:javascript复制
    @{list}    Create List    1    2    3
    New Page    http://127.0.0.1:8080/sec/33/index.html
    FOR    ${item}    IN    @{list}
        Click    id=c${item}
    END
    FOR    ${item}    IN    @{list}
        Check Checkbox    id=c${item}    force=true
    END
    Close Page

下拉条

代码语言:javascript复制
    New Page    http://127.0.0.1:8080/sec/32/index.html
    Select Options By    select[name=phones]    value    华为
    Get Selected Options    //select[1]    label    ==    华为
    ${promise} =    Promise To    Wait For Alert    action=accept
    Click    id=button
    ${text} =    Wait For    ${promise}
    Should Be Equal    ${text}    我选择的是华为
    Close Page

0 人点赞