行为驱动:python+behave,学习记录

一、安装behave:pip install behave
二、目录要求:features、features/stpes、features/XX.feature、features/stpes/BB.py
行为驱动:python+behave,学习记录_第1张图片
environment.py文件我是用来存储执行测试用例前后的一些操作,其中context是一个全局变量,可在steps中使用

#coding=utf-8
from selenium import webdriver
#运行前,其中context是全局变量
def before_all(context):
    context.driver=webdriver.Firefox();
#运行最后
def after_all(context):
    context.driver.close();

二、解释
1、XX.feature:自然语言描述用例,就是用自己的话,首先、然后操作什么等。(Feature、Scenario、When、And、Then)
Feature:可简单描述此场景是什么,如注册
Feature:Register User
Scenario:简单描述此用例是做什么的,如输入用户名
Scenario:input username
When:操作,如输入邮箱
When I set whit useremail “[email protected]
And:下一步操作,如输入用户名
And I set with username “musishi”
And…
Then:期望以上操作所得到的结果即预期结果
Then I except that text ‘验证码错误’

#coding=utf-8
Feature:Register User
    As a developer
    This is my fist bdd project
    Scenario:open registe1r website
        When I open the register webiste
        Then I expect that the title is "注册"
    Scenario:input username
        When I set whit useremail "[email protected]"
        And I set with username "musishi"
        And I set with password "musishi"
        And I set with code "123yU"
        And I click with registerbutton
        Then I except that text "验证码错误"

2、BB.py:编写测试用例
其中内容要对应XX.feature中的步骤

#coding=utf-8
from behave import *
#表示使用正则表达式
use_step_matcher('re')
#打开
@when('I open the register webiste')
def step_register(context):
    #context全局
    context.driver.get('http://www.5itest.cn/register?goto=http%3A//www.5itest.cn/course/1');
@then('I expect that the title is "([^"]*)"')
def step_register(context,title_name):
    #获取浏览器标题
    title=context.driver.title
    assert title_name in title
#输入邮箱
@when('I set whit useremail "([^"]*)"')
def step_inputUseremail(context,useremail):
   context.driver.find_element_by_id("register_email").send_keys(useremail);
   #输入用户名
@when('I set with username "([^"]*)"')
def step_inputUsername(context,username):
    context.driver.find_element_by_id("register_nickname").send_keys(username);
    #输入密码
@when('I set with password "([^"]*)"')
def step_inputpassword(context,userpass):
    context.driver.find_element_by_id("register_password").send_keys(userpass);
    #输入验证码
@when('I set with code "([^"]*)"')
def step_inputcode(context,usercode):
    context.driver.find_element_by_id('captcha_code').send_keys(usercode);
    #点击注册按钮
@when('I click with registerbutton')
def step_click(context):
    context.driver.find_element_by_id("register-btn").click();
@then('I except that text "([^"]*)"')
#判断实际结果与预期结果是否一致
def step_except(context,except_text):
    text=context.driver.find_element_by_id("captcha_code-error").text;
    #判断预期结果是否在实际结果中
    assert except_text in text

四、运行
cmd打开命令行,输入此XX.feature所在位置
如:behave G:test/feature

执行后
行为驱动:python+behave,学习记录_第2张图片
单引号会出现这样,改成双引号即可。
行为驱动:python+behave,学习记录_第3张图片
五、类型数据驱动,运行多个参数

#coding=utf-8
Feature:Register User
    As a developer
    This is my fist bdd project
    Scenario:open registe1r website
        When I open the register webiste
        Then I expect that the title is "注册"
    Scenario Outline:input username
        When I set whit useremail ""
        And I set with username ""
        And I set with password ""
        And I set with code ""
        And I click with registerbutton
        Then I except that text "验证码错误"
Examples:RegMessage
    |   useremail  |   username   | password |code|
    | 13500000001  |   123456qwe  | 12345678 |1234|
    | 13500000002  |  111111111111| 7888888  |456|

你可能感兴趣的:(python+selenium,python,selenium)