【问题标题】:Symfony2 functional test - check table contentsSymfony2 功能测试 - 检查表内容
【发布时间】:2015-08-07 01:32:54
【问题描述】:

我只在断言中使用过contains() 之类的东西,所以我不确定如何处理如此复杂的事情。

假设我有一系列预期的答案 - 在这种情况下,答案是“是”、“是”、“否”。

这实际上意味着,对于第一个和第二个问题,我希望在第三个 <td> 中看到 <span class="glyphicon glyphicon-ok"></span>,对于第三个问题,我希望在第四个 <td> 中看到它。

这是我的 HTML 代码:

<table class="table table-curved">
    <tr>
        <th width="10%">Item</th>
        <th width="60%">Description</th>
        <th width="10%">YES</th>
        <th width="10%">NO</th>
        <th width="10%">NOT APPLICABLE</th>
    </tr>

    <tr>
        <td class="report-table-inner report-centre">1</td>
        <td class="report-table-inner">Check cargo is secure and undamaged.</td>
        <td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
        <td class="report-centre"></td>
        <td class="report-centre"></td>
    </tr>
    <tr>
        <td class="report-table-inner report-centre">2</td>
        <td class="report-table-inner">Is all cargo accounted for.</td>
        <td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
        <td class="report-centre"></td>
        <td class="report-centre"></td>
    </tr>
    <tr>
        <td class="report-table-inner report-centre">3</td>
        <td class="report-table-inner">Is all cargo checked by customs.</td>
        <td class="report-centre"></td>
        <td class="report-centre danger"><span class="glyphicon glyphicon-ok"></span></td>
        <td class="report-centre"></td>
    </tr>
    ...

我应该如何为此编写测试?以编程方式遍历&lt;tr&gt; 是否很难?

谢谢

【问题讨论】:

    标签: php symfony functional-testing


    【解决方案1】:

    我认为您应该查看有关测试和 DomCrawler 组件的文档页面:

    有非常简单的方法可以过滤 html 或 xml 内容。

    【讨论】:

    • 我看过这个,但很混乱。
    • 使用这些参考回答here
    【解决方案2】:

    参考资料:


    <?php
    
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    
    class PageTest extends WebTestCase
    {
        public function testPage()
        {
            // create a client to get the content of the page
            $client = static::createClient();
            $crawler = $client->request('GET', '/page');
    
            // retrieve table rows
            $rows = $crawler->filter('.table-curved tr');
    
            $statesColumnIndex = array(
                // 0 indexed
                'ok' => 2,
                'ko' => 3,
                'na' => 4,
            );
    
            $expectedValues = array(
                // 0 indexed, row index => [$values]
                1 => ['identifier' => 1, 'state' => 'ok'],
                2 => ['identifier' => 2, 'state' => 'ok'],
                3 => ['identifier' => 3, 'state' => 'ko'],
            );
    
            foreach ($expectedValues as $rowIndex => $values) {
                // retrieve columns for row
                $columns = $rows->eq($rowIndex)->filter('td');
    
                // check item identifier
                $identifierColumn = $columns->eq(0);
                $this->assertEquals(
                    (string) $values['identifier'],   
                    trim($identifierColumn->text())
                );
    
                // check state
                $stateColumn = $columns->eq($statesColumnIndex[$values['state']]);
                $this->assertEquals(1, $stateColumn->filter('.glyphicon-ok')->count());
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      请注意,我根本不使用 Symfony,但这里有一个使用纯 PHP DOM 的答案;它需要 $values 作为数组,其中包含“pass”(跳过此 &lt;tr&gt;)或哪个列的索引应该有 glyphicon-ok 类:

      <?php
      $data = <<<DATA
      <table class="table table-curved">
          <tr>
              <th width="10%">Item</th>
              <th width="60%">Description</th>
              <th width="10%">YES</th>
              <th width="10%">NO</th>
              <th width="10%">NOT APPLICABLE</th>
          </tr>
      
          <tr>
              <td class="report-table-inner report-centre">1</td>
              <td class="report-table-inner">Check cargo is secure and undamaged.</td>
              <td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
              <td class="report-centre"></td>
              <td class="report-centre"></td>
          </tr>
          <tr>
              <td class="report-table-inner report-centre">2</td>
              <td class="report-table-inner">Is all cargo accounted for.</td>
              <td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
              <td class="report-centre"></td>
              <td class="report-centre"></td>
          </tr>
          <tr>
              <td class="report-table-inner report-centre">3</td>
              <td class="report-table-inner">Is all cargo checked by customs.</td>
              <td class="report-centre"></td>
              <td class="report-centre danger"><span class="glyphicon glyphicon-ok"></span></td>
              <td class="report-centre"></td>
          </tr>
      </table>
      DATA;
      
      
      $dom = new DOMDocument();
      $dom->loadXML($data);
      $xpath = new DOMXPath($dom);
      $values = ['pass', 2, 2, 3];
      $idx = 0;
      foreach($xpath->query('//tr') as $tr) {
        if ($values[$idx] != 'pass') {
          $tds = $tr->getElementsByTagName('td');
          $td = $tds->item($values[$idx]);
          if ($td instanceof DOMNode && $td->hasChildNodes()) {
            if (FALSE !== strpos($td->firstChild->getAttribute('class'), 'glyphicon-ok')) {
                echo "Matched on ", $tds->item(1)->textContent, "\n";
            } else {
                echo "Not matched on ", $tds->item(1)->textContent, "\n";
            }
          }
        }
        ++$idx;
      }
      

      【讨论】:

      • 谢谢TML。我真的很感激。
      • Symfony2 DomCrawler 的东西可能会让一些手动的 DOM 树爬行更容易;不能确定。
      • 使用 Symfony Crawler 回答 here
      猜你喜欢
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2010-12-27
      相关资源
      最近更新 更多