【问题标题】:Looping through filter result yield bogus empty HTML attributes遍历过滤器结果会产生虚假的空 HTML 属性
【发布时间】:2017-11-06 17:11:28
【问题描述】:

我的功能测试中有这个:

$questionnaires = [4, 5];
$boxes = $this->crawler->filter('#' . CustomerQuestionnairesType::BLOCK_PREFIX . '_questionnaires input[type=checkbox]');
$count = $boxes->count();
$this->assertGreaterThan(0, $count, "Couldn't find questionnaire check boxes");

for ($i = 0; $i < $count; $i++) {
    $box = $boxes->getNode($i);
    $value = $box->getAttribute('value');
    $checked = $box->getAttribute('checked');
    $in_array = in_array($value, $questionnaires);
    echo ";test $i: with value=$value checked=" . $checked . " should be=$in_array ". $box->getAttribute('id')."\n";
    $this->assertEquals($in_array ? 'checked' : '', $checked, "Invalid questionnaire map");
}

这是 HTML(保存自 $client-&gt;getResponse()-&gt;getContent()):

<div id="mobilformbundle_customer_questionnaires_questionnaires">
    <div class="checkbox">
        <label class=""><input checked="checked" id="mobilformbundle_customer_questionnaires_questionnaires_4" name="mobilformbundle_customer_questionnaires[questionnaires][]"
         type="checkbox" value="4"> questionnaire1</label>
    </div>
    <div class="checkbox">
        <label class=""><input checked="checked" id="mobilformbundle_customer_questionnaires_questionnaires_5" name="mobilformbundle_customer_questionnaires[questionnaires][]"
         type="checkbox" value="5"> questionnaire2</label>
    </div>
    <div class="checkbox">
        <label class=""><input checked="checked" id="mobilformbundle_customer_questionnaires_questionnaires_6" name="mobilformbundle_customer_questionnaires[questionnaires][]"
         type="checkbox" value="6"> questionnaire3</label>
    </div>
    <div class="checkbox">
        <label class=""><input id="mobilformbundle_customer_questionnaires_questionnaires_10" name="mobilformbundle_customer_questionnaires[questionnaires][]"
         type="checkbox" value="10"> generic questionnaire</label>
    </div>
</div>

如您所见,前 3 个框已选中(HTML 属性 checked 的值为 checked)。

这是我的测试的输出:

;test 0: with value=4 checked=checked should be=1 mobilformbundle_customer_questionnaires_questionnaires_4
;test 1: with value=5 checked= should be=1 mobilformbundle_customer_questionnaires_questionnaires_5

Expected :'checked'
Actual   :''

我不明白为什么$checked PHP 变量是空的。它应该包含checked

我的循环中是否存在某种错误?

【问题讨论】:

  • 循环对我来说看起来不错。有点奇怪吧?

标签: php symfony functional-testing


【解决方案1】:

$this-&gt;crawler 对象来自上一个请求。

我替换了这个(我粘贴的代码块上方的语句):

$this->client->submit($form, $formData);

与:

$this->crawler = $this->client->submit($form, $formData);

它成功了:

;test 0: with value=4 checked=checked should be=1 mobilformbundle_customer_questionnaires_questionnaires_4
;test 1: with value=5 checked=checked should be=1 mobilformbundle_customer_questionnaires_questionnaires_5
;test 2: with value=6 checked=checked should be= mobilformbundle_customer_questionnaires_questionnaires_6
;test 3: with value=10 checked= should be= mobilformbundle_customer_questionnaires_questionnaires_10

测试通过!

抱歉这个愚蠢的问题。随意关闭投票。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 2017-03-22
    • 2018-08-14
    • 2012-01-14
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多