如何使用代码功能测试对Laravel4上的单选按钮进行测试?

时间:2015-02-18 12:03:44

标签: php laravel-4 functional-testing codeception

使用代码在Laravel 4上编写功能测试。

我的tests/functional/UserSignUpCept.php文件。

<?php 
$I = new FunctionalTester($scenario);
$I->am('a guest');
$I->wantTo('sign up for a musician account');

$I->amOnPage('/');
$I->click('Sign Up as User');
$I->seeCurrentUrlEquals('/user/register');

$I->fillField(['name' => 'email'], 'jon@mail.com');
$I->fillField(['name' => 'password'], '123456');
$I->fillField(['name' => 'password_confirmation'], '123456');
$I->fillField(['name' => 'first_name'], 'jon');
$I->fillField(['name' => 'last_name'], 'deo');
$I->selectOption(['form select[name=gender]'], 'm');
$I->selectOption(['name' => 'zipcode_id'], '1');
$I->fillField(['name' => 'phone'], '01234567891');

$I->click('Register');

$I->seeCurrentUrlEquals('');
$I->see('The user has been successfully created. Activation mail has been sent to your email address.');

我的注册视图

{{ Form::open(array('route' => 'user.register', 'class' => 'form-horizontal', 'role' => 'form')) }}
    <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
        <div class="form-group">
            <label for="email" class="col-sm-4 control-label"><i class='small glyphicon glyphicon-asterisk' style='color: red;'></i> E-mail Address:</label>
            <div class="col-sm-8">
                {{ Form::email('email', null, array('id' => 'email', 'class' => 'form-control', 'placeholder' => 'example@example.com', 'tabindex' => 1)) }}
            </div>
        </div>
        <div class="form-group">
            <label for="password" class="col-sm-4 control-label"><i class='small glyphicon glyphicon-asterisk' style='color: red;'></i> Password:</label>
            <div class="col-sm-8">
                {{ Form::password('password', array('id' => 'password', 'class' => 'form-control', 'placeholder' => '******', 'tabindex' => 2)) }}
            </div>
        </div>
        <div class="form-group">
            <label for="password_confirmation" class="col-sm-4 control-label"><i class='small glyphicon glyphicon-asterisk' style='color: red;'></i> Password (Confirm):</label>
            <div class="col-sm-8">
                {{ Form::password('password_confirmation', array('id' => 'password_confirmation', 'class' => 'form-control', 'placeholder' => '******', 'tabindex' => 3)) }}
            </div>
        </div>
        <div class="form-group">
            <label for="first_name" class="col-sm-4 control-label"><i class='small glyphicon glyphicon-asterisk' style='color: red;'></i> First Name:</label>
            <div class="col-sm-8">
                {{ Form::text('first_name', null, array('id' => 'first_name' , 'class' => 'form-control', 'tabindex' => 4)) }}
            </div>
        </div>
        <div class="form-group">
            <label for="last_name" class="col-sm-4 control-label"><i class='small glyphicon glyphicon-asterisk' style='color: red;'></i> Last Name:</label>
            <div class="col-sm-8">
                {{ Form::text('last_name', null, array('id' => 'last_name', 'class' => 'form-control', 'tabindex' => 5)) }}
            </div>
        </div>
        <div class="form-group">
            <label for="gender" class="col-sm-4 control-label">Gender:</label>
            <div class="col-sm-8">
                <label class="radio-inline" for="gender_ns">
                    {{ Form::radio('gender', '', true, array('id' => 'gender_ns', 'tabindex' => 6)) }} Not Specified
                </label>
                <label class="radio-inline" for="gender_m">
                    {{ Form::radio('gender', 'm', false, array('id' => 'gender_m', 'tabindex' => 6)) }} Male
                </label>
                <label class="radio-inline" for="gender_f">
                    {{ Form::radio('gender', 'f', false, array('id' => 'gender_f', 'tabindex' => 6)) }} Female
                </label>
            </div>
        </div>
        <div class="form-group">
            <label for="zipcode" class="col-sm-4 control-label">Zipcode:</label>
            <div class="col-sm-8">
                {{ Form::select('zipcode_id', $zipcodes, null, array('id' => 'zipcode', 'class' => 'form-control', 'placeholder' => 'Start typing your zipcode', 'tabindex' => 7)) }}
            </div>
        </div>
        <div class="form-group">
            <label for="phone" class="col-sm-4 control-label">Phone:</label>
            <div class="col-sm-8">
                {{ Form::text('phone', null, array('id' => 'phone', 'class' => 'form-control', 'tabindex' => 8)) }}
                <p class="help-block">Eg. "02099999999"</p>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-8 col-sm-offset-4 control-label checkbox-inline" for="newsletter">
              {{ Form::checkbox('newsletter', '1', null, ['id' => 'newsletter', 'tabindex' => 9]) }} I want to subscribe to Musician newsletter
            </label>
        </div>

        <div class="form-group pull-right">
            <div class="col-sm-4">
                {{ Form::submit('Register', array('class' => 'btn btn-default', 'tabindex' => 10)) }}
            </div>
        </div>
    </div>
{{ Form::close() }}

我在终端上运行代码

vagrant@homestead:~/Code/musician$ vendor/bin/codecept run functional
Codeception PHP Testing Framework v2.0.11
Powered by PHPUnit 4.4.5 by Sebastian Bergmann.

Functional Tests (1) -------------------------------------------------------------------------------------------------
Sign up for a musician account (UserSignUpCept)                                                                 Fail
----------------------------------------------------------------------------------------------------------------------


Time: 2.58 seconds, Memory: 32.25Mb

There was 1 failure:

---------
1) Failed to sign up for a musician account in UserSignUpCept (/home/vagrant/Code/musician/tests/functional/UserSignUpCept.php)
Couldn't select option "form select[name=gender]"],"m":
Element with 0 'form select[name=gender]' was not found on page.

Scenario Steps:
10. I select option "form select[name=gender]"],"m"
9 . I fill field {"name":"last_name"},"deo"
8 . I fill field {"name":"first_name"},"jon"
7 . I fill field {"name":"password_confirmation"},"123456"
6 . I fill field {"name":"password"},"123456"
5 . I fill field {"name":"email"},"jon@mail.com"
4 . I see current url equals "/user/register"


FAILURES!                            
Tests: 1, Assertions: 1, Failures: 1.

我已经检查了http://codeception.com/docs/modules/Laravel4。但我无法解决。 如何为单选按钮组修复此测试?

提前致谢。

1 个答案:

答案 0 :(得分:0)

问题出在你正在使用的选择器上。 性别的HTML类似于:

<div class="form-group">
    <label for="gender" class="col-sm-4 control-label">Gender:</label>
    <div class="col-sm-8">
        <label class="radio-inline" for="gender_ns">
            <input id="gender_ns" tabindex="6" checked="checked" name="gender" type="radio" value=""> Not Specified
        </label>
        <label class="radio-inline" for="gender_m">
            <input id="gender_m" tabindex="6" name="gender" type="radio" value="m"> Male
        </label>
        <label class="radio-inline" for="gender_f">
            <input id="gender_f" tabindex="6" name="gender" type="radio" value="f"> Female
        </label>
    </div>
</div>

您在代码中使用form select[name=gender]。问题是你没有选择框。你有输入。

您应该使用form input[name=gender],问题应该得到解决。