Cypress:验证表格中同一行中多个<td>的值

huangapple go评论56阅读模式
英文:

Cypress: Verify value of multiple <td> in same row on Table

问题

I noticed an issue in your code. You have a typo in the variable name "staus." It should be "status." Here's the corrected code:

verifyRowValue(class, items, status) {
    const secondTd = cy.get('table').find('tbody tr').find('td:nth-child(3)');
    const span = secondTd.find('span');
    const classToCheck = 'Birds'; // Corrected variable name
    const itemsToVerify = ['Eagle', 'Duck', 'Parrot']; // Corrected variable name
    const statusToVerify = 'BLue'; // Corrected variable name
    const type = status === 'Rare' ? 'Red' : 'BLue';

    if (span.contains(classToCheck)) {
        const tr = secondTd.closest('tr');
        // Check 4th <td>
        tr.find('td').eq(3)
            .find('span').invoke('text').then(text => {
                const spanText = text.trim();
                for (let item of itemsToVerify) { // Corrected "in" to "of"
                    expect(spanText).to.include(item);
                }
            }).then(() => { // Check 5th <td>
                tr.find('td').eq(4)
                    .find('span').invoke('text')
                    .then(selType => {
                        expect(selType).to.eq(type);
                    });
            });
    }
}

The corrections include using the correct variable names and fixing the loop from for (let item in items) to for (let item of itemsToVerify) for iterating through the itemsToVerify array.

英文:

I tried to verify the value in [Item] and [Status] columns based on [Class] column value as below table.

Detail is I find <tr> contain value of 3rd <td> 'Birds' then verify items = ['Eagle', 'Duck', 'Parrot'] and staus = 'BLue'

Here is HTML of the row I will check.

&lt;tr class=&quot;animal&quot;&gt;
        &lt;td&gt;
            &lt;div&gt;
                &lt;i aria-hidden=&quot;true&quot;
                   class=&quot;theme--light&quot;&gt;
                &lt;/i&gt;
                &lt;input
                    aria-checked=&quot;false&quot; role=&quot;checkbox&quot; type=&quot;checkbox&quot; value=&quot;&quot;&gt;
            &lt;/div&gt;
        &lt;/td&gt;
        &lt;td&gt;
            &lt;div&gt;2&lt;/div&gt;
        &lt;/td&gt;
        &lt;td&gt;
            &lt;div class=&quot;text-truncate&quot;&gt;
                &lt;span&gt;
                    Birds
                &lt;/span&gt;
            &lt;/div&gt;
            &lt;span&gt;&lt;!----&gt;&lt;/span&gt;
        &lt;/td&gt;
        &lt;td&gt;
            &lt;div class=&quot;text-truncate&quot;&gt;&lt;span&gt;&lt;!----&gt;
                            Eagle , Duck , Parrot
                          &lt;/span&gt;&lt;/div&gt;
            &lt;span&gt;&lt;!----&gt;&lt;/span&gt;&lt;/td&gt;
        &lt;td&gt;&lt;span class=&quot;blue--text&quot;&gt;BLue&lt;/span&gt;&lt;/td&gt;
 &lt;/tr&gt;

Cypress:验证表格中同一行中多个<td>的值

Here my code:

 verifyRowValue(class, items, status) {
    const secondTd = cy.get(&#39;table&#39;).find(&#39;tbody tr&#39;).find(&#39;td:nth-child(3)&#39;)
    const span = secondTd.find(&#39;span&#39;);
    const class= &#39;Birds&#39;;
    const items = [&#39;Eagle&#39;, &#39;Duck&#39;, &#39;Parrot&#39;];
    const staus= &#39;Normal&#39;;
    const type = status === &#39;Rare&#39; ? &#39;Red&#39; : &#39;BLue&#39;;

    if (span.contains(class)) {
        const tr = secondTd.closest(&#39;tr&#39;)
        //Check 4th &lt;td&gt;
        tr.find(&#39;td&#39;).eq(3)
            .find(&#39;span&#39;).invoke(&#39;text&#39;).then(text =&gt; {
            const spanText = text.trim();
            for (let item in items) {
                expect(spanText).to.includes(items[item])
            }
        }).then(() =&gt; {   // =&gt; Check 5th &lt;td&gt;
            tr.find(&#39;td&#39;).eq(4)
                .find(&#39;span&#39;).invoke(&#39;text&#39;)
                .then(selType =&gt; {
                    expect(selType).to.eq(type);
                });
        })
    }  
}

I run verify 4th <td> ok, but 5th <td> is failed.

Maybe I used then() in the wrong way?

答案1

得分: 4

你忘了在第五列上使用.trim()

.then(selType =&gt; {
  expect(selType.trim()).to.eq(type);
});
英文:

You forgot to use .trim() on column 5.

.then(selType =&gt; {
  expect(selType.trim()).to.eq(type);
});

huangapple
  • 本文由 发表于 2023年5月10日 11:11:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定