[E2E] Robot Framework introduction

We will use demo project as an example, go though QuickStart repo.

 

Install:

First you should have python & pip installed on your machine, then install robot framework libaraies.

pip install robotframework
pip install robotframework-selenium2library

 

Clone the repo.

 

Run:

robot login_tests

login_tests is the folder contains all the test cases. After running the tests, it should all pass.

 

Keywords:

You are able to define keywords, you can think 'keywords' is something like composeable function. It combines multi functions together as a single function. In Robot framkwork, it combines multi test cases into one single suit. 

For example:

*** Keywords ***
User "${username}" logs in with password "${password}"
    Input username    ${username}
    Input password    ${password}
    Submit credentials

"User "${username}" logs in with password "${password}"" is a new "keyword" which can be use elsewhere:

*** Test Cases ***
Valid Login
    Given browser is opened to login page
    When user "demo" logs in with password "mode"
    Then welcome page should be open

"Given", "When" && "Then" are built in keyword in Robot framework.

 

And the keyword itself also combines multi keywords:

复制代码
Input Username
    [Arguments]    ${username}
    Input Text    username_field    ${username}

Input Password
    [Arguments]    ${password}
    Input Text    password_field    ${password}

Submit Credentials
    Click Button    login_button
复制代码

"Argumenets" here you can think it is function params, so when user use "Input Username", should also give a param.

"Input Text" is a built in keywords,from seleium libaray,  "username_field" is a id selector:

<td><input id="username_field" size="30" type="text"></td>

 


 

 

Variables:

resource.robot:

复制代码
*** Variables ***
${SERVER}         localhost:7272
${BROWSER}        Firefox
${DELAY}          0
${VALID USER}     demo
${VALID PASSWORD}    mode
${INVALID USER}    invalid
${INVALID PWD}    invalid
${LOGIN URL}      http://${SERVER}/
${WELCOME URL}    http://${SERVER}/welcome.html
${ERROR URL}      http://${SERVER}/error.html
复制代码

You can define variables, and you can use those inside other files, just need to import the file contains variables:

*** Settings ***
Documentation     A test suite with a single Gherkin style test.
...
...               This test is functionally identical to the example in
...               valid_login.robot file.
Resource          resource.robot

Then you can use those like:

Login Should Have Failed
    Location Should Be    ${ERROR URL}
    Title Should Be    Error Page

 

Different way to write tests:

复制代码
*** Settings ***
Documentation     A test suite containing tests related to invalid login.
...
...               These tests are data-driven by their nature. They use a single
...               keyword, specified with Test Template setting, that is called
...               with different arguments to cover different scenarios.
...
...               This suite also demonstrates using setups and teardowns in
...               different levels.
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          resource.robot

*** Test Cases ***
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}
Empty Username And Password    ${EMPTY}    ${EMPTY}

*** Keywords ***
Login With Invalid Credentials Should Fail
    [Arguments]    ${username}    ${password}
    Input Username    ${username}
    Input Password    ${password}
    Submit Credentials
    Login Should Have Failed
复制代码

 

or 

 

复制代码
*** Settings ***
Documentation     A test suite containing tests related to invalid login.
...
...               These tests are data-driven by their nature. They use a single
...               keyword, specified with Test Template setting, that is called
...               with different arguments to cover different scenarios.
...
...               This suite also demonstrates using setups and teardowns in
...               different levels.
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          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}
Empty Username And Password      ${EMPTY}         ${EMPTY}

*** Keywords ***
Login With Invalid Credentials Should Fail
    [Arguments]    ${username}    ${password}
    Input Username    ${username}
    Input Password    ${password}
    Submit Credentials
    Login Should Have Failed
复制代码

 

posted @   Zhentiw  阅读(261)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-04-20 [RxJS] Combining streams in RxJS
点击右上角即可分享
微信分享提示