方式一、使用Robot库提供的TestSuite
Python脚本如下:
from robot.api import TestSuite
ts = TestSuite('G01')
ts.resource.imports.resource('D:/tools/resource.robot')
tc = ts.tests.create('login_success')
tc.keywords.create('Open Browser To Login Page', args=[])
tc.keywords.create('Input Username', args=[u'demo'])
tc.keywords.create('Input Password', args=[u'mode'])
tc.keywords.create('Submit Credentials', args=[])
tc.keywords.create('Welcome Page Should Be Open', args=[])
tc.keywords.create('Sleep', args=[u'3 seconds'])
tc.keywords.create('Close Browser', args=[])
ts.run()
运行结果如下:
image.png
resource.robot(引入的外部资源文件)定义了涉及的变量和关键字(类似方法),内容如下:
*** Settings ***
Documentation A resource file with reusable keywords and variables.
...
... The system specific keywords created here form our own
... domain specific language. They utilize keywords provided
... by the imported Selenium2Library.
Library Selenium2Library
*** Variables ***
${SERVER} localhost:7272
${BROWSER} Firefox
${DELAY} 0
${VALID USER} demo
${VALID PASSWORD} mode
${LOGIN URL} http://${SERVER}/
${WELCOME URL} http://${SERVER}/welcome.html
${ERROR URL} http://${SERVER}/error.html
*** Keywords ***
Open Browser To Login Page
Open Browser ${LOGIN URL} ${BROWSER}
Maximize Browser Window
Set Selenium Speed ${DELAY}
Login Page Should Be Open
Login Page Should Be Open
Title Should Be Login Page
Go To Login Page
Go To ${LOGIN URL}
Login Page Should Be Open
Input Username
[Arguments] ${username}
Input Text username_field ${username}
Input Password
[Arguments] ${password}
Input Text password_field ${password}
Submit Credentials
Click Button login_button
Welcome Page Should Be Open
Location Should Be ${WELCOME URL}
Title Should Be Welcome Page
方式二、使用Robot库提供的TestSuiteBuilder
Python脚本如下:
from robot.api import TestSuiteBuilder
suite = TestSuiteBuilder().build('D:/autotest/demo0314/G02.txt')
suite.run()
运行结果如下:
image.png
RF用例集脚本G02.txt的内容如下:
*** Settings ***
Suite Setup Open Browser To Login Page
Suite Teardown Close Browser
Test Setup Go To Login Page
Test Template Login With Invalid Credentials Should Fail
Resource ../../tools/resource.robot
*** Test Cases *** User Name Password
Invalid Username invalid ${VALID PASSWORD}
Invalid Password ${VALID USER} invalid
Invalid Username and Password
invalid whatever
Empty Username ${Empty} ${VALID PASSWORD}
Empty Password ${VALID USER} ${Empty}
*** Keywords ***
Login With Invalid Credentials Should Fail
[Arguments] ${username} ${password}
Input Username ${username}
Input Password ${password}
Submit Credentials
Login Should Have Failed
Sleep 3 seconds
Login Should Have Failed
Location Should Be ${ERROR URL}
Title Should Be Error Page
网友评论