robotframework automation attempt

robotframe plugin installation

Only use the TAB key

 Basic Templates and Syntax

*** 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




Package naming rules: 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

Use Case Execution: Import and Test Suite

*** 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 custom keywords: add timestamp, create folder, screenshot

Define y_m=time.strftime("%Y_%m"), which can be placed in a folder
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

Import custom python classes in robot.resource:

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

Run demo03.robot to generate folders and screenshots

Guess you like

Origin blog.csdn.net/weixin_42435798/article/details/124806692