'User changes password' 行为中的验收测试

'User changes password' acceptance test in behave

我在使用 behave (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 或 t[= 的值24=]在 运行ning 测试之前分类数据库并播种我的固定装置。

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

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

    您可以使用 environmental-controls 进行设置和清理,或者您可以为此编写自己的步骤。