使用剧作家获取所有TH标签和测试值

发布于 2025-02-11 18:50:17 字数 1892 浏览 1 评论 0原文

我正在使用剧作家测试页面,并且有下表。

我希望能够测试所有列的列,计数和值按正确的顺序,即产品,价格等。

我该怎么做?

这是我到目前为止尝试过的方法,尽管过度

test('the table headings are in place and in the correct order', async ({
      page,
    }) => {
      log('looking for the table headings...');

      const tableThead = page.$$('table.subscriptions > thead > tr > th');

      const allThs = await page.$$eval(
        'table.subscriptions > thead > tr',
        (ths) => {
          return ths.map((th) => {
            const product = th.querySelector('th:nth-child(1)');
            const price = th.querySelector('th:nth-child(2)');
            const term = th.querySelector('th:nth-child(3)');
            const renewalDate = th.querySelector('th:nth-child(4)');
            const renewalAmount = th.querySelector('th:nth-child(5)');
            const renewalType = th.querySelector('th:nth-child(6)');
            const paymentType = th.querySelector('th:nth-child(6)');
            const address = th.querySelector('th:nth-child(7)');
            return {
              product,
              price,
              term,
              renewalDate,
              renewalAmount,
              renewalType,
              paymentType,
              address,
            };
          });
        }
      );

      log('taking a screenshot...');

      await tableThead.screenshot({
        path: 'screenshots/subscriptions/table/table-head.png',
      });
    });

=========

edit 1:

这是我尝试Alapan Das的答案后得到的:

I'm using playwright to test a page, and I have the following table.

enter image description here

I want to be able to test all the thead th columns, the count and values are in the right order, i.e. Product, Price etc.

How would I go about doing this?

Here is what I've tried so far, although overkill

test('the table headings are in place and in the correct order', async ({
      page,
    }) => {
      log('looking for the table headings...');

      const tableThead = page.$('table.subscriptions > thead > tr > th');

      const allThs = await page.$eval(
        'table.subscriptions > thead > tr',
        (ths) => {
          return ths.map((th) => {
            const product = th.querySelector('th:nth-child(1)');
            const price = th.querySelector('th:nth-child(2)');
            const term = th.querySelector('th:nth-child(3)');
            const renewalDate = th.querySelector('th:nth-child(4)');
            const renewalAmount = th.querySelector('th:nth-child(5)');
            const renewalType = th.querySelector('th:nth-child(6)');
            const paymentType = th.querySelector('th:nth-child(6)');
            const address = th.querySelector('th:nth-child(7)');
            return {
              product,
              price,
              term,
              renewalDate,
              renewalAmount,
              renewalType,
              paymentType,
              address,
            };
          });
        }
      );

      log('taking a screenshot...');

      await tableThead.screenshot({
        path: 'screenshots/subscriptions/table/table-head.png',
      });
    });

=======

Edit 1:

This is what I get after trying Alapan Das's answer:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恰似旧人归 2025-02-18 18:50:17

我认为在这种情况下,您可以使用 loacators列表,然后比较这样的内部文本:

const expectedText = [
  product,
  price,
  term,
  renewalDate,
  renewalAmount,
  renewalType,
  paymentType,
  address,
]
const tableHeader = page.locator('th[role="columnheader"]')
const tableHeaderTexts = await tableHeader.allTextContents()
await tableHeaderTexts.forEach((text, index) => {
  expect(text).toEqual(expectedText[index])
})

I think for this case you can use the locators lists and then compare the inner texts like this:

const expectedText = [
  product,
  price,
  term,
  renewalDate,
  renewalAmount,
  renewalType,
  paymentType,
  address,
]
const tableHeader = page.locator('th[role="columnheader"]')
const tableHeaderTexts = await tableHeader.allTextContents()
await tableHeaderTexts.forEach((text, index) => {
  expect(text).toEqual(expectedText[index])
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文