使用behat进行身份验证测试,找不到用户名

时间:2015-02-06 09:23:52

标签: php selenium-chromedriver behat mink

我的登录测试有问题,使用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"

我的FeatureContext.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');
}

我的behat.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”。请帮我。 Thnx提前。

2 个答案:

答案 0 :(得分:1)

  1. 为什么这行And还需要另一个And And I am authenticated as "admin" using "admin"?它不是对的!

  2. 您的登录表单是http://localhost/admin还是http://localhost/admin/login

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

  4. 如果有帮助,我会将这些用于我的案例:

  5. 版本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
    

答案 1 :(得分:-1)

错误是:我不得不删除:

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

现在没有这条线就行了