beh中的“用户更改密码”验收测试

2024-04-25 01:07:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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)

它在我第一次运行测试时按预期工作,但是因为它更改了用户在DB中的密码,所以在运行测试之前,我必须交换NEW_PASSWORDOLD_PASSWORD的值,或者截断DB并为我的fixture设置种子。你知道吗

我认为有更好的,更自动化的方法。你知道吗


Tags: inbrowserfieldnewbycontextloginpass
1条回答
网友
1楼 · 发布于 2024-04-25 01:07:19
  1. 连接到数据库并在设置方法中插入新用户。你知道吗
  2. 在步骤1中测试更改添加用户的密码。你知道吗
  3. 在测试清理中从数据库中删除用户。

    可以使用environmental-controls进行设置和清理,也可以编写自己的步骤。你知道吗

相关问题 更多 >