1
votes

I have a problem with my login test using behat,mink and selenium I got the error

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

My scenario :

@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"

My 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');
}

My 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

I can't understand why this error because I have a form with this name : "username". Please help me. Thnx in advance.

2

2 Answers

1
votes
  1. Why do you need another And for this line And And I am authenticated as "admin" using "admin"? It doesn't like right!

  2. Is your login form inside http://localhost/admin or something like http://localhost/admin/login?

  3. You say Given I am on "http://localhost/admin" in your Gherkin and then call $this->visit('http://localhost/admin'); in your FeatureContext. Why?

  4. I use these for my case if it helps:

Version 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');
}

Version 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"'),
    ];
}

EDIT:

Place Then I wait 60 seconds after Given I am on "http://localhost/admin" step. Run your tests (in browser mode) so that you can visually verify if you're in where the login page is.

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

Example 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
votes

The error was that : I had to delete :

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

And now without this line it works