dongyu3967
2015-02-18 12:03
浏览 45
已采纳

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

Writing functional test on Laravel 4 with codeception.

My tests/functional/UserSignUpCept.php file.

<?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.');

My register view

{{ 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() }}

I run the codeception on terminal

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.

I have examined http://codeception.com/docs/modules/Laravel4. But I could not solve. How can I fix this test for the radio button group?

Thanks in advance.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulv1760 2015-02-28 14:38
    已采纳

    The problem lies with the selector you're using. The HTML for the gender is something like:

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

    You are using form select[name=gender] in codeception. The problem is that you don't have a select box. You have inputs.

    You should use form input[name=gender] and the issue should be resolved.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题