行为驱动(Behave Driven Development)测试,是一种敏捷的开发方法,通常应用在自动化测试中,通过使用自然描述语言确定自动化脚本。
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : register_user.py
@Time : 2019/06/26 23:40:33
@Author : Crisimple
@Version : 1.0
@Contact : Crisimple@foxmail.com
@License : (C)Copyright 2017-2019, Micro-Circle
@Desc : None
'''
# here put the import lib
from behave import *
from feature.lib.pages.register_page import RegisterPage
use_step_matcher('re')
@When('I open the register website')
def step_register(context, url):
# context.driver.get(url)
RegisterPage(context).get_url(url)
@Then('I except that the title is "([^"]*)"')
def step_register(context, title_name):
title = RegisterPage(context).get_title()
assert title_name in title
@When('I set with useremail "([^"]*)"')
def step_register(context, useremail):
RegisterPage(context).send_useremail(useremail)
@When('I set with username "([^"]*)"')
def step_register(context, username):
RegisterPage(context).send_userename(username)
@When('I set with password "([^"]*)"')
def step_register(context, password):
RegisterPage(context).send_password(password)
@When('I set with code "([^"]*)"')
def step_register(context, code):
RegisterPage(context).send_code(code)
@When('I click with registerbutton')
def step_register(context):
RegisterPage(context).click_register_button
@Then('I except that text "([^"]*)"')
def step_register(context, code_text):
text = context.driver.find_element_by_id("captcha_code-error").text
assert code_text in text
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : base_page.py
@Time : 2019/06/27 03:09:07
@Author : Crisimple
@Version : 1.0
@Contact : Crisimple@foxmail.com
@License : (C)Copyright 2017-2019, Micro-Circle
@Desc : 抽离页面中公用的方法
'''
# here put the import lib
from selenium.webdriver.common.by import By
class BasePage(object):
def __init__(self, driver):
self.driver = driver
# 打开网页
def get_url(self, url):
self.driver.get(url)
# 获取title
def get_title(self):
return self.driver.title
# 定位元素
def find_element(self, *loc):
self.driver.find_element(*loc)
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : register_page.py
@Time : 2019/06/27 03:11:22
@Author : Crisimple
@Version : 1.0
@Contact : Crisimple@foxmail.com
@License : (C)Copyright 2017-2019, Micro-Circle
@Desc : None
'''
# here put the import lib
from feature.lib.pages.base_page import BasePage
from selenium.webdriver.common.by import By
class RegisterPage(BasePage):
def __init__(self, context):
super(RegisterPage, self).__init__(context.driver)
def send_useremail(self, useremail):
self.find_element(By.ID, "register_email").send_keys(useremail)
def send_userename(self, username):
self.find_element(By.ID, "register_nickname").send_keys(username)
def send_password(self, password):
self.find_element(By.ID, "register_password").send_keys(password)
def send_code(self, captcha_code):
self.find_element(By.ID, "captcha_code").send_keys(captcha_code)
def click_register_button(self):
self.find_element(By.ID, "register-btn").click()