簡體   English   中英

如何使用代碼功能測試對Laravel4上的單選按鈕進行測試?

[英]How written test for radio button on Laravel4 with codeception functional test?

使用代碼在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 但我無法解決。 如何為單選按鈕組修復此測試?

提前致謝。

問題出在你正在使用的選擇器上。 性別的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]並解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM