“用户更改密码”验收行为

时间:2018-11-04 05:24:48

标签: python bdd acceptance-testing

当我遇到编写用于更改密码方案的测试的问题时,我正在使用行为(python)自动进行验收测试。

这是我在test_change_password.py

中的步骤
from behave import *
from features.steps.common_steps import fill_in_email, fill_in_password, show_main_page, register_user, logout
from features.steps.constants import *
import time

CHANGE_PASSWORD_USER_EMAIL = 'change_password@gmail.com'

use_step_matcher("re")


def login(context, password):
    context.browser.visit("/login")
    context.browser.is_element_present_by_css("//input")
    fill_in_email(context, 'test@gmail.com')
    fill_in_password(context, password)
    context.browser.find_by_xpath("//button").first.click()

def change_password_fields_and_logout(context):
    old_pass_field = context.browser.find_by_xpath('//form/div[%s]/input' % OLD_PASS_INDEX)
    old_pass_field.fill(NEW_PASSWORD)
    new_pass_field = context.browser.find_by_xpath('//form/div[%s]/input' % NEW_PASS_INDEX)
    new_pass_field.fill(OLD_PASSWORD)
    confirm_new_pass_field = context.browser.find_by_xpath('//form/div[%s]/input' % CONFIRM_NEW_PASS_INDEX)
    confirm_new_pass_field.fill(OLD_PASSWORD)
    context.browser.find_by_xpath('//button[text()="Submit"]').first.click()
    logout(context)


@given("change password user login")
def step_impl(context):
    context.browser.visit('/login')
    context.browser.is_element_present_by_css('//h1')
    fill_in_email(context, CHANGE_PASSWORD_USER_EMAIL)
    fill_in_password(context, NEW_PASSWORD)
    context.browser.find_by_xpath("//button[@type='submit']").first.click()
    if not context.browser.is_element_present_by_text('first name last name'):
        NEW_PASSWORD, OLD_PASSWORD = OLD_PASSWORD, NEW_PASSWORD
        fill_in_password(context, NEW_PASSWORD)


@when("user changed his password")
def step_impl(context):
    context.browser.is_element_present_by_text('first name last name')
    context.browser.find_by_text("first name last name").first.click()
    context.browser.is_element_present_by_text('Change password')
    context.browser.find_by_xpath('//a[text()="Change password"]').first.click()
    change_password_fields_and_logout(context)


@then("user login with new password")
def step_impl(context):
    login(context, NEW_PASSWORD)

第一次运行测试时,它可以按预期工作,但是由于它更改了数据库中的用户密码,因此在运行测试之前,我必须交换NEW_PASSWORDOLD_PASSWORD的值或截断DB并播种我的夹具。

我认为有更好,更自动化的方法。

1 个答案:

答案 0 :(得分:1)

  1. 连接到数据库并在设置方法中插入新用户。
  2. 在步骤1中测试更改已添加用户的密码。
  3. 在测试清理中从数据库中删除用户。

    您可以使用environmental-controls进行设置和清理,也可以为此编写自己的步骤。
相关问题