robotframework-UI自动化项目实战
自定义关键字
使用 *** Keywords ***
设置自定义关键字
*** Settings *** Documentation 用户的登录测试 Library SeleniumLibrary Suite Setup 打开浏览器设置最大化并隐式等待时间为5s Test Setup 导航到首页 Test Teardown 删除cookies并截屏 Suite Teardown 关闭浏览器 *** Keywords *** 打开浏览器设置最大化并隐式等待时间为5s Open Browser url=http://47.100.175.62:3000/ browser=chrome executable_path=./drivers/chromedriver.exe maximize browser window set selenium implicit wait 5 关闭浏览器 close all browsers 删除cookies并截屏 delete all cookies set screenshot directory screenshots capture page screenshot 导航到首页 go to http://47.100.175.62:3000/ *** Test Cases *** 测试正常的登录 click element xpath://*[text()="登录"] input text id:name fanmao_36 input password id:pass 123456 click element xpath://*[@value="登录"] location should be http://47.100.175.62:3000/ page should contain element xpath://div[@class="user_card"]//span[@class="user_name"] ${username}= get text xpath://div[@class="user_card"]//span[@class="user_name"] Should Be Equal ${username} fanmao_36 测试错误的登录-错误的用户名和密码 click element xpath://*[text()="登录"] input text id:name fanmao_36 input password id:pass 1234567 click element xpath://*[@value="登录"] 测试错误的登录-空的用户名和密码 click element xpath://*[text()="登录"] input text id:name None input password id:pass None click element xpath://*[@value="登录"]
将测试用例中的操作步骤转换为自定义的关键字
将测试用例中的操作步骤转换为自定义的关键字
cnode.robot
*** Settings *** Documentation 用户的登录测试 Library SeleniumLibrary Suite Setup 打开浏览器设置最大化并隐式等待时间为5s Test Setup 导航到首页 Test Teardown 删除cookies并截屏 Suite Teardown 关闭浏览器 *** Variables *** ${strnull}= *** Keywords *** 打开浏览器设置最大化并隐式等待时间为5s Open Browser url=http://47.100.175.62:3000/ browser=chrome executable_path=./drivers/chromedriver.exe maximize browser window set selenium implicit wait 5 关闭浏览器 close all browsers 删除cookies并截屏 delete all cookies set screenshot directory screenshots capture page screenshot 导航到首页 go to http://47.100.175.62:3000/ 点击登录链接到达登录页面 click element xpath://*[text()="登录"] location should contain /signin 使用用户名和密码进行登录 [Arguments] ${username} ${password} input text id:name ${username} input password id:pass ${password} click element xpath://*[@value="登录"] 验证登录成功-期望页面跳转到首页并在首页显示登录用户名 [Arguments] ${loginname} location should be http://47.100.175.62:3000/ page should contain element xpath://div[@class="user_card"]//span[@class="user_name"] ${username}= get text xpath://div[@class="user_card"]//span[@class="user_name"] Should Be Equal ${username} ${loginname} 验证登录失败-期望页面有对应的错误提示信息 [Arguments] ${msg} ${result_text}= get text xpath://strong Should Be Equal ${result_text} ${msg} *** Test Cases *** 测试正常的登录 点击登录链接到达登录页面 使用用户名和密码进行登录 fanmao_36 123456 验证登录成功-期望页面跳转到首页并在首页显示登录用户名 fanmao_36 测试错误的登录-错误的用户名和密码 点击登录链接到达登录页面 使用用户名和密码进行登录 fanmao_35 123456 验证登录失败-期望页面有对应的错误提示信息 用户名或密码错误 测试错误的登录-空的用户名和密码 点击登录链接到达登录页面 使用用户名和密码进行登录 ${strnull} ${strnull} 验证登录失败-期望页面有对应的错误提示信息 信息不完整。
项目pom
pom/homepage.robot
*** Settings *** Documentation 网站首页 Library SeleniumLibrary *** Keywords *** 点击登录到达登录页面 click element xpath://*[text()="登录"] location should contain /signin 点击注册到达注册页面 click element xpath://*[text()="注册"] location should contain /signup 点击【发布话题】按钮跳转到发布话题页面 click element xpath://*[text()="发布话题"]
pom/loginpage.robot
*** Settings *** Documentation 登录页面 Library SeleniumLibrary *** Keywords *** 使用用户名和密码进行登录 [Arguments] ${username} ${password} input text id:name ${username} input password id:pass ${password} click element xpath://*[@value="登录"]
testcases/basecase.robot
*** Settings *** Documentation 登录页面 Library SeleniumLibrary *** Keywords *** 打开浏览器设置最大化并隐式等待时间为5s Open Browser url=http://47.100.175.62:3000/ browser=chrome executable_path=./drivers/chromedriver.exe maximize browser window set selenium implicit wait 5 关闭浏览器 close all browsers 删除cookies并截屏 delete all cookies set screenshot directory screenshots capture page screenshot
在写测试用例的时候,可以引用已经定义好的关键字进行操作
testcases/testusers/testlogin.robot
*** Settings *** Documentation 测试登录功能 Resource ../basecase.robot Resource ../../pom/homepage.robot Resource ../../pom/loginpage.robot Suite Setup 打开浏览器设置最大化并隐式等待时间为5s *** Test Cases *** 使用正确的用户名和密码能够登录成功 点击登录到达登录页面 使用用户名和密码进行登录 fanmao_36 123456
执行
在项目的根目录进行运行。
数据驱动测试
数据驱动主要使用 test template 关键字设置
pom/loginpage.robot
*** Settings *** Documentation 登录页面 Library SeleniumLibrary *** Keywords *** 使用用户名和密码进行登录 [Arguments] ${username} ${password} input text id:name ${username} input password id:pass ${password} click element xpath://*[@value="登录"] 验证用户登录失败场景,错误提示信息为 [Arguments] ${msg} ${result_text}= get text xpath://strong Should Be Equal ${result_text} ${msg
testcases/testusers/testloginddt.robot
*** Settings *** Documentation 测试登录功能 Resource ../basecase.robot Resource ../../pom/homepage.robot Resource ../../pom/loginpage.robot Suite Setup 打开浏览器设置最大化并隐式等待时间为5s Test Template 用户登录的异常场景测试 Suite Teardown 关闭浏览器 *** Test Cases *** USERNAME PASSWORD EXPECT_MSG 使用错误的用户名和密码 fanmao123 123456 用户名或密码错误 使用空的用户名和密码 ${EMPTY} 123456 信息不完整。 使用用户名和空的密码 fanmao123 ${EMPTY} 信息不完整。 使用空的用户名和空的密码 ${EMPTY} ${EMPTY} 信息不完整。 *** Keywords *** 用户登录的异常场景测试 [Arguments] ${username} ${password} ${expect_msg} 点击登录到达登录页面 使用用户名和密码进行登录 ${username} ${password} 验证用户登录失败场景,错误提示信息为 ${expect_msg}
需要注意的是:
当使用数据驱动的方式进行代码运行,数据驱动只能放到一个robot文件中。
添加tags 标签
- settings 添加标签
*** Settings *** Documentation 测试登录功能 Resource ../basecase.robot Resource ../../pom/homepage.robot Resource ../../pom/loginpage.robot Suite Setup 打开浏览器设置最大化并隐式等待时间为5s Test Template 用户登录的异常场景测试 Suite Teardown 关闭浏览器 Default Tags ddt *** Test Cases *** USERNAME PASSWORD EXPECT_MSG 使用错误的用户名和密码 fanmao123 123456 用户名或密码错误 使用空的用户名和密码 ${EMPTY} 123456 信息不完整。 使用用户名和空的密码 fanmao123 ${EMPTY} 信息不完整。 使用空的用户名和空的密码 ${EMPTY} ${EMPTY} 信息不完整。 *** Keywords *** 用户登录的异常场景测试 [Arguments] ${username} ${password} ${expect_msg} 点击登录到达登录页面 使用用户名和密码进行登录 ${username} ${password} 验证用户登录失败场景,错误提示信息为 ${expect_msg}
在测试用例中添加
*** Settings *** Documentation 测试登录功能 Resource ../basecase.robot Resource ../../pom/homepage.robot Resource ../../pom/loginpage.robot Suite Setup 打开浏览器设置最大化并隐式等待时间为5s Suite Teardown 关闭浏览器 Default Tags ci *** Keywords *** 使用不同的用户名或密码登录 [Arguments] ${username} ${passwd} 点击登录到达登录页面 使用用户名和密码进行登录 ${username} ${passwd} *** Test Cases *** 错误的用户名和错误的密码登录 [Tags] smoke 使用不同的用户名或密码登录 fanmao-35 1234 空的用户名和错误的密码登录 使用不同的用户名或密码登录 ${Empty} 1234 空的用户名和空的密码登录 使用不同的用户名或密码登录 ${Empty} ${empty} 用户名和空的密码登录 使用不同的用户名或密码登录 fanmao_36 ${empty}
执行的时候使用
- -i 表示包含标签的方式 robot -i tags testcases
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?