【问题标题】:Authentification test with behat, username not found使用 behat 进行身份验证测试,未找到用户名
【发布时间】:2015-02-06 20:28:53
【问题描述】:

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

我不明白为什么会出现此错误,因为我有一个名称为“用户名”的表单。请帮我。提前谢谢。

【问题讨论】:

    标签: php selenium-chromedriver behat mink


    【解决方案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. 如果有帮助,我会将这些用于我的案例:

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

    编辑:

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

    /**
     * @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->getSession()->getPage()->find('css','input[name="username"]')->设置值($用户名);但我得到另一个错误:调用非对象上的成员函数 setValue()
    • 听起来好像您不在正确的页面或字段名称错误。请参阅上面的编辑说明。我更新了答案。
    • 您的测试是否打开浏览器窗口?如果是这样,您看到登录表单了吗?
    • 我编辑了我的问题,是的,我看到了登录表单,但出现错误:“调用非对象上的成员函数 setValue()”
    • 运行测试时,你看到页面源代码中的元素了吗?正如我在上面的 EDIT 中所描述的,您需要添加 sleep() 以让自己有机会右键单击并打开源代码。
    【解决方案2】:

    错误是:我必须删除:

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

    现在没有这条线它可以工作

    【讨论】:

    • 好吧,如果您通过说“我的 base_url 是网站的管理部分”来指示行为,这在技术上是错误的方法,它应该没有管理部分。您只是在使用此解决方案隐藏潜在问题!你应该应用我在上面最后一条评论中所说的话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 2020-12-08
    • 1970-01-01
    • 2016-05-05
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多