pycharm+robotframework实现ui自动化

 

一:关键字驱动

       1:switch frame

*** Test Cases ***
安居客演示demo
    #打开浏览器
    Open Browser            https://login.anjuke.com/login/form             chrome
    #使用id来定位frame
    Select Frame            id:iframeLoginIfm
    #输入文本内容
    Input Text              id:phoneIpt             13011111111
    #移出frame
    Unselect Frame
    #点击内容
    Click Element           css:div[id='footer']>ul>li:nth-child(1)>a

       2:Select Window->切换浏览器窗口----->Switch Window(建议使用这个,select window已经比较过时了)

*** Test Cases ***
切换浏览器窗口
    Open Browser                https://www.baidu.com           chrome
    #设置全局等待时间
    Set Selenium Implicit Wait              10
    #设置运行时间,类似模拟人为有一定的操作时间间隙
    Set Selenium Speed                      1
    #输入文本内容
    Input Text                  id:kw       helloworld
    #点击百度一下
    Click Element               id:su
    #点击第一个搜索项
    Click Element               css:div[id='content_left']>div:nth-child(1)>h3>a
    #切换到当前最后一个窗口
    Switch Window               New
    #在切换后的窗口上输入内容
    Input Text                  id:query            你好

     3:switch alert->切换alert窗口

*** Test Cases ***
切换alert窗口
    Set Selenium Speed          1
    Open Browser                http://49.233.108.117:3000/signin       chrome
    Input Text                  id:name             xxxxxx
    Input Password              id:pass             xxxxxx
    Click Element               css:[value='登录']
    Go To                       http://49.233.108.117:3000/user/testuser1
    Click Element               css:div[id='content']>div:nth-child(2)>div:nth-child(2)>div.topic_title_wrapper>a
    Click Element               css:[title='删除']
    ${message}                  Handle Alert        DISMISS
    log to console              >>>>${message}<<<<

    4:添加自定义关键字

          4.1:resource.txt文件->Keywords自定义关键字->可以写多个自定义关键字

*** Settings ***
Documentation               公共操作-登录


Library                     SeleniumLibrary



*** Keywords ***
用户登录到系统
    Open Browser                http://49.233.108.117:3000/signin       chrome
    Input Text                  id:name             xxxxxx
    Input Password              id:pass             xxxxxx
    Click Element              css:input[class="span-primary"]

用户发帖
    Click Element               css:select[id="tab-value"]>option:nth-child(2)
    Input Text                  id:title            hello world

        4.2:在testcases中调用->注意Resource关键字,引入自定义关键字文件

              在Test Cases中主要去调用自定义关键字即可

*** Settings ***
Documentation               演示使用resource
Library                     SeleniumLibrary
Resource                    resource.txt

*** Test Cases ***
发帖测试
    用户登录到系统
    Click Element               css:span[class="span-success"]
    用户发帖

       4.3:自定义关键字添加参数

       resource.txt

*** Settings ***
Documentation               公共操作-登录


Library                     SeleniumLibrary

*** Variables ***
${LoginURL}             http://49.233.108.117:3000/signin

*** Keywords ***
用户登录到系统
    [Arguments]                 ${username}         ${password}
    Open Browser                ${loginURL}       chrome
    Input Text                  id:name             ${username}
    Input Password              id:pass             ${password}
    Click Element              css:input[class="span-primary"]

用户发帖
    Click Element               css:select[id="tab-value"]>option:nth-child(2)
    Input Text                  id:title            hello world

[Arguments]固定写法,为自定义关键字添加变量

${username}

${password}变量名称

调用自定义的关键字时候需要添加对应的参数

topic.robot

*** Settings ***
Documentation               演示使用resource
Library                     SeleniumLibrary
Resource                    resource.txt

*** Test Cases ***
发帖测试
    用户登录到系统                 xxxxxx       xxxxxx
    Click Element               css:span[class="span-success"]
    用户发帖

        5:自定义关键字添加返回值

         resource.txt

*** Settings ***
Documentation               公共操作-登录


Library                     SeleniumLibrary

*** Variables ***
${LoginURL}             http://49.233.108.117:3000/signin

*** Keywords ***
用户登录到系统
    [Arguments]                 ${username}         ${password}
    Open Browser                ${loginURL}       chrome
    Input Text                  id:name             ${username}
    Input Password              id:pass             ${password}
    Click Element              css:input[class="span-primary"]
    ${usernametext}             Get Text                    css:span>a[class="dark"]
    [Return]                    ${usernametext}

用户发帖
    Click Element               css:select[id="tab-value"]>option:nth-child(2)
    Input Text                  id:title            hello world

  主要语句是下面内容

   ${usernametext}             Get Text                    css:span>a[class="dark"]
    [Return]                    ${usernametext}

下面这种做法是不可取的

             
   [Return]                    Get Text                    css:span>a[class="dark"]

调用带有返回值的自定义关键字时,也要先定义一个变量,再去操作变量       

        topic.robot

*** Settings ***
Documentation               演示使用resource
Library                     SeleniumLibrary
Resource                    resource.txt

*** Test Cases ***
发帖测试
    ${value}                    用户登录到系统                 xxxxxx       xxxxxx
    log to console              ${value}
    Click Element               css:span[class="span-success"]
    用户发帖

      6:运行时控制

*** Settings ***
Documentation               演示使用resource
Library                     SeleniumLibrary
Suite Setup                 用户登录到系统             xxxxxx           xxxxxx
Suite Teardown              Close Browser
Test Setup                  Log to Console      开始执行test        #每个case执行之前做的操作
Test Teardown               Capture Page Screenshot     #每个case执行之后做的操作
Resource                    resource.txt

*** Test Cases ***
发帖测试
    Click Element               css:span[class="span-success"]
    用户发帖

删除帖子测试
    log to console              ------删除话题------

Suite Setup:整个测试套件执行之前的操作

Suite TearDown:整个测试套件执行之后的操作

Test SetUp:每一个测试test执行之前的操作

Test TearDown:每一个测试用例执行之后的操作

       7:数据驱动

       data_driver.robot

*** Settings ***
Documentation               数据驱动
Library                     SeleniumLibrary
Test Template               用户登录
Suite Teardown              Close Browser

*** variables ***
${loginURL}                 http://49.233.108.117:3000/signin


*** Test Cases ***              Username            Password
用户名或密码错误                    xxxxx                xxxxxx
用户登录成功                          xxxxxx           xxxxxx


*** keywords ***
用户登录
    [Arguments]                 ${username}         ${password}
    Open Browser                ${loginURL}       chrome
    Input Text                  id:name             ${username}
    Input Password              id:pass             ${password}
    Click Element              css:input[class="span-primary"]

注意:一个robot文件中,只能写一个Test Template

使用的时候主要给Test Case中传不同值,有多少行值将执行多少次

数据驱动还支持csv文件,excel文件

       8:自定义库

       utils.py

def get_topic_id(url):
    res=url.split('/')
    return res[-1]

       test_library.robot

*** Settings ***
Documentation               如何使用自定义的库
Library                     utils.py

*** Test Cases ***
使用自定义库
    ${value}         get_topic_id            http://49.233.108.117:3000/topic/xxxxxxxxxxxxxxx
    log to console      ${value}

使用自定义库来完善操作

       9:运行设置

         -robot文件存放目录汇总,执行robot目录名运行所有robot文件

         -指定tag运行

-i:代表包含

robot -i 回归测试ORsmoketesting testcase        #or
robot -i 回归测试ANDsmoketesting testcase       #and
robot -i 回归测试 testcase

-e:代表不包含,同上,替换下-i

        -指定测试报告生成目录

robot -i 回归测试ORsmoketesting -d reports testcase

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值