robotframework自动化尝试

robotframe插件安装

只能用TAB

 基础模板和语法

*** Settings ***
Library     SeleniumLibrary

*** Variables ***
${url}          http://127.0.0.1:8047/mgr/sign.html
${username}     byhy
${password}     88888888
${add_user}     bobo
${add_phone_number}     18888999911

*** Keywords ***
使用用户名密码登陆
    Input Text    id:username    ${username}
    Input Text    id:password    ${password}
    Click Button    xpath://*[@type="submit"]
    Set Selenium Implicit Wait    2

添加用户
    Click Element    xpath://*[@type="button"]

输入添加客户的相关内容
    [Arguments]     ${name}     ${phone_number}
    Input Text    xpath://*[@id="root"]/div/section[2]/div[1]/div[1]/div[1]/input    ${name}
    Input Text    xpath://*[@id="root"]/div/section[2]/div[1]/div[1]/div[2]/input    ${phone_number}
    Click Button    xpath://*[@id="root"]/div/section[2]/div[1]/div[2]/button[1]


*** Test Cases ***
用例:登陆白月黑羽
    Open Browser    ${url}     googlechrome
    Maximize Browser Window
    使用用户名密码登陆
    添加用户
    输入添加客户的相关内容     lele        199009999




封装命名规则:                add_user.resource

*** Settings ***
Documentation       用户添加关键字
Library             SeleniumLibrary

*** Variables ***
${main_url}     http://127.0.0.1:8047/mgr/sign.html


*** Keywords ***
打开浏览器并到白月黑羽首页
    Open Browser    ${main_url}     chrome
    Maximize Browser Window
    Set Selenium Implicit Wait    5
使用用户名和密码登陆
    [Arguments]     ${username}     ${password}
    Input Text    id:username    ${username}
    Input Text    id:password    ${password}
    Click Button    xpath://*[@type="submit"]
登录失败的提示信息
    [Arguments]                 ${expect_alert}
    Alert Should Be Present     ${expect_alert}

截图
    Capture Page Screenshot
关闭浏览器
    Close All Browsers

用例执行:导入以及测试套件

*** Settings ***
Documentation       测试用户登录
Resource            ../testcase_robot_keywords/add_user.resource
Suite Setup         打开浏览器并到白月黑羽首页
Suite Teardown      关闭浏览器
Test Setup          截图
Test Teardown       截图

*** Test Cases ***
用户名密码为空,测试
    使用用户名和密码登陆  ${EMPTY}     88888888
    登录失败的提示信息    请输入用户名

错误的用户名密码登录失败
    使用用户名和密码登陆  123    88888888
    登录失败的提示信息    登录失败 : 用户名或者密码错误

错误的用户名密码登录失败
    使用用户名和密码登陆  byhy    8888888
    登录失败的提示信息    登录失败 : 用户名或者密码错误

python自定义关键字:加上时间戳,创建文件夹,截图

定义y_m=time.strftime("%Y_%m"),可放到一个文件夹里
import os
import time

class DiyKeywords:

    def get_screenshots_path(self):
        root_path=os.path.dirname(os.path.dirname(__file__))
        screenshots=os.path.join(root_path,"screenshots")
        if not os.path.exists(screenshots):
            os.mkdir(screenshots)
        y_m=time.strftime("%Y_%m_%d  %H_%M_%S")
        ym_dir=os.path.join(screenshots,y_m)
        if not os.path.exists(ym_dir):
            os.mkdir(ym_dir)
        return ym_dir

在robot.resource导入自定义python类:

Library             ../testcase_robot_diykeywords/DiyKeyWords.py
*** Settings ***
Documentation       用户添加关键字
Library             SeleniumLibrary
#python自定义类
Library       ../testcase_robot_diykeywords/DiyKeyWords.py


*** Variables ***
${main_url}     http://127.0.0.1:8047/mgr/sign.html


*** Keywords ***
打开浏览器并到白月黑羽首页
    Open Browser    ${main_url}     chrome
    Maximize Browser Window
    Set Selenium Implicit Wait    5
使用用户名和密码登陆
    [Arguments]     ${username}     ${password}
    Input Text    id:username    ${username}
    Input Text    id:password    ${password}
    Click Button    xpath://*[@type="submit"]
登录失败的提示信息
    [Arguments]                 ${expect_alert}
    Alert Should Be Present     ${expect_alert}

截图
    ${pngdir}=  Get Screenshots Path
    Set Screenshot Directory        ${pngdir}
    Capture Page Screenshot
关闭浏览器
    Close All Browsers

运行demo03.robot产生文件夹和截图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值