未找到behat、username的身份验证测试


Authentification test with behat, username not found

我使用behat、mink和selenium进行登录测试时遇到问题我得到错误

Form field with id|name|label|value "username" not found.

我的场景:

@javascript
Scenario: View users list
Given I am on "http://localhost/admin"
Then I wait 60 seconds
And And I am authenticated as "admin" using "admin"
Then I should see "List of customers"

我的功能上下文.php:

/**
 * @Then /^I wait ('d+) seconds$/
 */
public function iWaitSeconds($seconds)
{
    sleep($seconds);
}
/**
 * @Given /^And I am authenticated as "([^"]*)" using "([^"]*)"$/
 */
public function andIAmAuthenticatedAsUsing($username, $password) {
    $this->visit('http://localhost/admin');
    $this->getSession()->getPage()->find('css','input[name="username"]')->setValue($username);
    $this->getSession()->getPage()->find('css','input[name="password"]')->setValue($password);
    $this->pressButton('Login');
}

我的行为yml:

default:
extensions:
    Behat'MinkExtension'Extension:
        base_url: http:localhost/admin
        browser_name: chrome
        javascript_session: selenium2
        selenium2:
            browser: chrome
        goutte: ~
paths:
    features:  features
    bootstrap: features/bootstrap

我不明白为什么会出现这个错误,因为我有一个名为"username"的表单。请帮帮我。提前好了。

  1. 为什么这条线路And And I am authenticated as "admin" using "admin"需要另一个And?它不喜欢权利!

  2. 您的登录表单是在http://localhost/admin还是类似http://localhost/admin/login的内容中?

  3. 您在小黄瓜中说Given I am on "http://localhost/admin",然后在FeatureContext中调用$this->visit('http://localhost/admin');。为什么?

  4. 如果有帮助的话,我会在我的案例中使用这些:

版本1)

/**
 * @Given /^I am logged in$/
 * @Given /^I am logged in as "([^"]*)"$/
 */
public function iAmLoggedIn($username = 'user')
{
    $this->visit('/logout');
    $this->visit('/login');
    $this->fillField('username', $username);
    $this->fillField('password', 'password');
    $this->pressButton('_submit');
}

版本2)

use Behat'Behat'Context'Step;
/**
 * @When /^I log in as "([^"]*)"$/
 */
public function iLogInAs($username)
{
    return [
        new Step'Given('I am not logged in'),
        new Step'When('I go to "/login"'),
        new Step'When('I fill in "username" with "'.$username.'"'),
        new Step'When('I fill in "password" with "password"'),
        new Step'Then('I press "_submit"'),
        new Step'Then('I should be on "/welcome"'),
    ];
}

编辑:

Then I wait 60 seconds放置在Given I am on "http://localhost/admin"步骤之后。运行您的测试(在浏览器模式下),这样您就可以直观地验证您是否在登录页面所在的位置。

/**
 * @Given /^I wait ('d+) seconds$/
 */
public function iWaitSeconds($seconds)
{
    sleep($seconds);
}

示例behat.yml

default:
    context:
        class: Site'FrontendBundle'Features'Context'FeatureContext
        parameters:
            output_path: %behat.paths.base%/build/behat/output/
            screen_shot_path: %behat.paths.base%/build/behat/screenshot/
    extensions:
        Behat'Symfony2Extension'Extension:
            mink_driver: true
            kernel:
                env: test
                debug: true
        Behat'MinkExtension'Extension:
            base_url: 'http://localhost/myproject/web/app_test.php/'
            files_path: %behat.paths.base%/build/dummy/
            javascript_session: selenium2
            browser_name: firefox
            goutte: ~
            selenium2: ~
    paths:
        features: %behat.paths.base%/src
        bootstrap: %behat.paths.features%/Context
# Add "-p firefox" parameter to behat command to run tests with Firefox browser
firefox:
    extensions:
        Behat'MinkExtension'Extension:
            browser_name: firefox

错误是:我必须删除:

$this->visit('http://localhost/admin');

现在,如果没有这一行,它就可以工作