首页 > 解决方案 > 如何让 Selenium 测试正常工作?

问题描述

我试图进行测试,但它失败了。我不确定“#None”是什么意思。在教程中一切正常。我很乐意提供帮助。

too_short_username_steps.feature

Feature: Username field
abc
Scenario: Too short Username
    Given user is on last.fm registration website
    When user fills in the username box with one character
    Then User can see Your username should be between 2 and 15 characters, begin with a letter and contain only letters, numbers, 

too_short_username_steps.py

  from behave import given, when, then

    @given('user is on last.fm register site')
    def step_start_page(context):
        context.driver.get('https://www.last.fm/join')

    @when('user fills one character in username field')
    def step_set_username(context):
        context.driver.find_element_by_id('id_userName').send_keys('a')

    @then('User can see ')
    def step_valid_warning(context):
        assert context.driver.find_element_by_css_selector("#mantle_skin > div.container.page-content > div > div > form > div.form-group.form-group--userName.js-form-group.has-error > div > div.js-form-group-error.form-group-error > div > p")

当我运行“表现”时:

Feature: Username field # features/too_short_username.feature:1
  dasdasd
  Scenario: Too short Username                                                                                                                # features/too_short_username.feature:3
    Given user is on last.fm registration website                                                                                             # None
    When user fills in the username box with one character                                                                                    # None
    Then User can see Your username should be between 2 and 15 characters, begin with a letter and contain only letters, numbers, '_' or '-'. # None


Failing scenarios:
  features/too_short_username.feature:3  Too short Username

0 features passed, 1 failed, 0 skipped
0 scenarios passed, 1 failed, 0 skipped
0 steps passed, 0 failed, 0 skipped, 3 undefined
Took 0m0.000s

You can implement step definitions for undefined steps with these snippets:

@given(u'user is on last.fm registration website')
def step_impl(context):
    raise NotImplementedError(u'STEP: Given user is on last.fm registration website')


@when(u'user fills in the username box with one character')
def step_impl(context):
    raise NotImplementedError(u'STEP: When user fills in the username box with one character')


@then(u'User can see Your username should be between 2 and 15 characters, begin with a letter and contain only letters, numbers, \'_\' or \'-\'.')
def step_impl(context):
    raise NotImplementedError(u'STEP: Then User can see Your username should be between 2 and 15 characters, begin with a letter and contain only letters, numbers, \'_\' or \'-\'.')

标签: pythonseleniumtestingselenium-webdriverautomated-tests

解决方案


场景中每个步骤的语句必须与文件 too_short_username_steps.py 中的步骤定义相同,然后场景中执行的步骤将映射到定义。例如,“用户在 last.fm 注册网站”不等于“用户在 last.fm 注册网站”,这就是它失败的原因,找不到步骤。它应该如下所示:

from behave import given, when, then

@given('user is on last.fm registration website')
def step_start_page(context):
    context.driver.get('https://www.last.fm/join')

@when('user fills in the username box with one character')
def step_set_username(context):
    context.driver.find_element_by_id('id_userName').send_keys('a')

@then('User can see Your username should be between 2 and 15 characters, begin with a letter and contain only letters, numbers,')
def step_valid_warning(context):
    assert context.driver.find_element_by_css_selector("#mantle_skin > div.container.page-content > div > div > form > div.form-group.form-group--userName.js-form-group.has-error > div > div.js-form-group-error.form-group-error > div > p")

推荐阅读