Best JavaScript code snippet using playwright-internal
example.spec.js
Source:example.spec.js  
...220  test('should remove completed items when clicked', async ({ page }) => {221    const todoItems = page.locator('.todo-list li');222    await todoItems.nth(1).locator('.toggle').check();223    await page.locator('.clear-completed').click();224    await expect(todoItems).toHaveCount(2);225    await expect(todoItems).toHaveText([TODO_ITEMS[0], TODO_ITEMS[2]]);226  });227  test('should be hidden when there are no items that are completed', async ({ page }) => {228    await page.locator('.todo-list li .toggle').first().check();229    await page.locator('.clear-completed').click();230    await expect(page.locator('.clear-completed')).toBeHidden();231  });232});233test.describe('Persistence', () => {234  test('should persist its data', async ({ page }) => {235    for (const item of TODO_ITEMS.slice(0, 2)) {236      await page.locator('.new-todo').fill(item);237      await page.locator('.new-todo').press('Enter');238    }239    const todoItems = page.locator('.todo-list li');240    await todoItems.nth(0).locator('.toggle').check();241    await expect(todoItems).toHaveText([TODO_ITEMS[0], TODO_ITEMS[1]]);242    await expect(todoItems).toHaveClass(['completed', '']);243    // Ensure there is 1 completed item.244    checkNumberOfCompletedTodosInLocalStorage(page, 1);245    // Now reload.246    await page.reload();247    await expect(todoItems).toHaveText([TODO_ITEMS[0], TODO_ITEMS[1]]);248    await expect(todoItems).toHaveClass(['completed', '']);249  });250});251test.describe('Routing', () => {252  test.beforeEach(async ({ page }) => {253    await createDefaultTodos(page);254    // make sure the app had a chance to save updated todos in storage255    // before navigating to a new view, otherwise the items can get lost :(256    // in some frameworks like Durandal257    await checkTodosInLocalStorage(page, TODO_ITEMS[0]);258  });259  test('should allow me to display active items', async ({ page }) => {260    await page.locator('.todo-list li .toggle').nth(1).check();261    await checkNumberOfCompletedTodosInLocalStorage(page, 1);262    await page.locator('.filters >> text=Active').click();263    await expect(page.locator('.todo-list li')).toHaveCount(2);264    await expect(page.locator('.todo-list li')).toHaveText([TODO_ITEMS[0], TODO_ITEMS[2]]);265  });266  test('should respect the back button', async ({ page }) => {267    await page.locator('.todo-list li .toggle').nth(1).check();268    await checkNumberOfCompletedTodosInLocalStorage(page, 1);269    await test.step('Showing all items', async () => {270      await page.locator('.filters >> text=All').click();271      await expect(page.locator('.todo-list li')).toHaveCount(3);272    });273    await test.step('Showing active items', async () => {274      await page.locator('.filters >> text=Active').click();275    });276    await test.step('Showing completed items', async () => {277      await page.locator('.filters >> text=Completed').click();278    });279    await expect(page.locator('.todo-list li')).toHaveCount(1);280    await page.goBack();281    await expect(page.locator('.todo-list li')).toHaveCount(2);282    await page.goBack();283    await expect(page.locator('.todo-list li')).toHaveCount(3);284  });285  test('should allow me to display completed items', async ({ page }) => {286    await page.locator('.todo-list li .toggle').nth(1).check();287    await checkNumberOfCompletedTodosInLocalStorage(page, 1);288    await page.locator('.filters >> text=Completed').click();289    await expect(page.locator('.todo-list li')).toHaveCount(1);290  });291  test('should allow me to display all items', async ({ page }) => {292    await page.locator('.todo-list li .toggle').nth(1).check();293    await checkNumberOfCompletedTodosInLocalStorage(page, 1);294    await page.locator('.filters >> text=Active').click();295    await page.locator('.filters >> text=Completed').click();296    await page.locator('.filters >> text=All').click();297    await expect(page.locator('.todo-list li')).toHaveCount(3);298  });299  test('should highlight the currently applied filter', async ({ page }) => {300    await expect(page.locator('.filters >> text=All')).toHaveClass('selected');301    await page.locator('.filters >> text=Active').click();302    // Page change - active items.303    await expect(page.locator('.filters >> text=Active')).toHaveClass('selected');304    await page.locator('.filters >> text=Completed').click();305    // Page change - completed items.306    await expect(page.locator('.filters >> text=Completed')).toHaveClass('selected');307  });308});309async function createDefaultTodos(page) {310  for (const item of TODO_ITEMS) {311    await page.locator('.new-todo').fill(item);...calculator-page.spec.js
Source:calculator-page.spec.js  
...14      "backgroundColor",15      "rgb(255, 255, 255)"16    );17    const inputs = page.locator("#player-name-text-input");18    await expect(inputs).toHaveCount(2);19    await page.fill("#player-name-text-input >> nth=0", "sa");20    await expect(page.locator("text=Sabalenka")).toBeVisible();21    await expect(page.locator("text=Badosa")).toBeVisible();22    await expect(page.locator("text=Sakkari")).toBeVisible();23    await page.locator("text=Sakkari").click();24    await expect(page.locator("#player-name-text-input >> nth=0")).toHaveValue(25      "Sakkari, Maria"26    );27    await expect(page.locator("text=Badosa")).not.toBeVisible();28    await expect(page.locator("text=Sabalenka")).not.toBeVisible();29    await page.fill("#player-name-text-input >> nth=1", "si");30    await expect(page.locator("text=Siniakova")).toBeVisible();31    await expect(page.locator("text=Anisimova")).toBeVisible();32    await page.locator("text=Anisimova").click();33    await expect(page.locator("#player-name-text-input >> nth=1")).toHaveValue(34      "Anisimova, Amanda"35    );36    await expect(page.locator("text=Siniakova")).not.toBeVisible();37    await page.locator("text=submit").click();38    await expect(39      page.locator(".wrapper-calculator-form-properties")40    ).toBeVisible();41    const courtValue = await page.inputValue(42      ".wrapper-calculator-form-inner select >> nth=0"43    );44    const tournamentValue = await page.inputValue(45      ".wrapper-calculator-form-inner select >> nth=1"46    );47    const roundValue = await page.inputValue(48      ".wrapper-calculator-form-inner select >> nth=2"49    );50    const dateValue = await page.inputValue(51      ".wrapper-calculator-form-inner input"52    );53    await page.locator("text=Submit").click();54    await expect(page.locator("#compare-results")).toBeVisible();55    await expect(page.locator("#compare-results select >> nth=0")).toHaveValue(56      courtValue57    );58    await expect(page.locator("#compare-results select >> nth=1")).toHaveValue(59      tournamentValue60    );61    await expect(page.locator("#compare-results select >> nth=2")).toHaveValue(62      roundValue63    );64    await expect(page.locator("#compare-results input")).toHaveValue(dateValue);65    let matchesPlayed = await page.textContent(66      ".perfromance-stats-individual > div >> nth=1"67    );68    await page.locator(".circle-info-player-stats >> nth=0").click();69    await expect(page.locator(".get-player-matches >> nth=0")).toBeVisible();70    await page.locator("text=All matches").first().click();71    // +1 because of header72    await expect(page.locator(".player-matches")).toHaveCount(73      parseInt(matchesPlayed) + 174    );75    await expect(76      page.locator(".get-player-matches >> nth=0")77    ).not.toBeVisible();78    matchesPlayed = await page.textContent("#Matcheswon_count >> nth=0");79    await page.locator(".circle-info-player-stats >> nth=0").click();80    await expect(page.locator(".get-player-matches >> nth=0")).toBeVisible();81    await page.locator("#won").first().click();82    await expect(83      page.locator(".get-player-matches >> nth=0")84    ).not.toBeVisible();85    // +1 because of header86    await expect(page.locator(".player-matches")).toHaveCount(87      parseInt(matchesPlayed) + 188    );89    matchesPlayed = await page.textContent("#Matcheswon2-0_count >> nth=0");90    await page.locator(".circle-info-player-stats >> nth=0").click();91    await expect(page.locator(".get-player-matches >> nth=0")).toBeVisible();92    await page.locator("#won-2-0").first().click();93    await expect(94      page.locator(".get-player-matches >> nth=0")95    ).not.toBeVisible();96    // +1 because of header97    await expect(page.locator(".player-matches")).toHaveCount(98      parseInt(matchesPlayed) + 199    );100    let allMatchesPlayed = await page.textContent(101      ".perfromance-stats-individual > div >> nth=1"102    );103    let wonMatches = await page.textContent("#Matcheswon_count >> nth=0");104    await page.locator(".circle-info-player-stats >> nth=0").click();105    await expect(page.locator(".get-player-matches >> nth=0")).toBeVisible();106    await page.locator("#lost").first().click();107    await expect(108      page.locator(".get-player-matches >> nth=0")109    ).not.toBeVisible();110    // +1 because of header111    await expect(page.locator(".player-matches")).toHaveCount(112      parseInt(allMatchesPlayed) - parseInt(wonMatches) + 1113    );114  });115  // INDIVIDUAL TEST116  test("individual", async ({ page }) => {117    await expect(page.locator("text=Individual")).toHaveCSS(118      "backgroundColor",119      "rgb(100, 149, 237)"120    );121    await expect(page.locator("text=Compare")).toHaveCSS(122      "backgroundColor",123      "rgb(255, 255, 255)"124    );125    await expect(page.locator("#player-name-text-input")).toBeVisible();126    await page.fill("#player-name-text-input", "ba");127    await expect(page.locator("text=Sabalenka")).toBeVisible();128    await expect(page.locator("text=Badosa")).toBeVisible();129    await expect(page.locator("text=Bara")).toBeVisible();130    await page.locator("text=Badosa").click();131    await expect(132      page.locator(".wrapper-calculator-form-properties")133    ).toBeVisible();134    const courtValue = await page.inputValue(135      ".wrapper-calculator-form-inner select >> nth=0"136    );137    const tournamentValue = await page.inputValue(138      ".wrapper-calculator-form-inner select >> nth=1"139    );140    const roundValue = await page.inputValue(141      ".wrapper-calculator-form-inner select >> nth=2"142    );143    const dateValue = await page.inputValue(144      ".wrapper-calculator-form-inner input"145    );146    await page.locator("text=Submit").click();147    await expect(page.locator(".wrapper-calculator-results")).toBeVisible();148    await expect(149      page.locator(".calculator-results-form select >> nth=0")150    ).toHaveValue(courtValue);151    await expect(152      page.locator(".calculator-results-form select >> nth=1")153    ).toHaveValue(tournamentValue);154    await expect(155      page.locator(".calculator-results-form select >> nth=2")156    ).toHaveValue(roundValue);157    await expect(page.locator(".calculator-results-form input")).toHaveValue(158      dateValue159    );160    let matchesPlayed = await page.textContent(161      ".perfromance-stats-individual > div >> nth=1"162    );163    await page.locator(".circle-info-player-stats >> nth=0").click();164    await expect(page.locator(".get-player-matches >> nth=0")).toBeVisible();165    await page.locator("text=All matches").first().click();166    // +1 because of header167    await expect(page.locator(".player-matches")).toHaveCount(168      parseInt(matchesPlayed) + 1169    );170    await expect(171      page.locator(".get-player-matches >> nth=0")172    ).not.toBeVisible();173    matchesPlayed = await page.textContent("#Matcheswon_count >> nth=0");174    await page.locator(".circle-info-player-stats >> nth=0").click();175    await expect(page.locator(".get-player-matches >> nth=0")).toBeVisible();176    await page.locator("#won").first().click();177    await expect(178      page.locator(".get-player-matches >> nth=0")179    ).not.toBeVisible();180    // +1 because of header181    await expect(page.locator(".player-matches")).toHaveCount(182      parseInt(matchesPlayed) + 1183    );184    matchesPlayed = await page.textContent("#Matcheswon2-0_count >> nth=0");185    await page.locator(".circle-info-player-stats >> nth=0").click();186    await expect(page.locator(".get-player-matches >> nth=0")).toBeVisible();187    await page.locator("#won-2-0").first().click();188    await expect(189      page.locator(".get-player-matches >> nth=0")190    ).not.toBeVisible();191    // +1 because of header192    await expect(page.locator(".player-matches")).toHaveCount(193      parseInt(matchesPlayed) + 1194    );195    let allMatchesPlayed = await page.textContent(196      ".perfromance-stats-individual > div >> nth=1"197    );198    let wonMatches = await page.textContent("#Matcheswon_count >> nth=0");199    await page.locator(".circle-info-player-stats >> nth=0").click();200    await expect(page.locator(".get-player-matches >> nth=0")).toBeVisible();201    await page.locator("#lost").first().click();202    await expect(203      page.locator(".get-player-matches >> nth=0")204    ).not.toBeVisible();205    // +1 because of header206    await expect(page.locator(".player-matches")).toHaveCount(207      parseInt(allMatchesPlayed) - parseInt(wonMatches) + 1208    );209  });...seo.test.js
Source:seo.test.js  
...80      await page.goto(t.src);81      const link = page.locator(82        "meta[name='robots'][content='follow,noindex']"83      );84      await expect(link).toHaveCount(1);85    });86  });87});88test.describe("index links", () => {89  [90    {91      title: "latest page",92      src: "/gateway/latest/install-and-run/docker/",93    },94    {95      title: "index page",96      src: "/gateway/",97    },98    {99      title: "latest version of a plugin",100      src: "/hub/kong-inc/application-registration/",101    },102  ].forEach((t) => {103    test(t.title, async ({ page }) => {104      await page.goto(t.src);105      const link = page.locator("meta[name='robots'][content='follow,index']");106      await expect(link).toHaveCount(1);107    });108  });109});110test.describe("unversioned content", () => {111  [112    {113      title: "konnect",114      src: "/konnect/",115    },116    {117      title: "konnect-platform",118      src: "/konnect-platform/",119    },120    {121      title: "contributing",122      src: "/contributing/",123    },124  ].forEach((t) => {125    test(t.title, async ({ page }) => {126      await page.goto(t.src);127      const robotsIndex = page.locator(128        "meta[name='robots'][content='follow,index']"129      );130      await expect(robotsIndex).toHaveCount(1);131      // There's no need for a canonical link with unversioned content132      const canonical = await page.locator("link[rel='canonical']");133      await expect(canonical).toHaveCount(0);134    });135  });136});137test.describe("sitemap", () => {138  [139    "/konnect/",140    "/konnect-platform/",141    "/gateway/",142    "/mesh/",143    "/kubernetes-ingress-controller/",144    "/deck/",145    "/gateway/latest/install-and-run/docker/",146    "/mesh/latest/installation/ecs/",147    "/kubernetes-ingress-controller/latest/deployment/k4k8s/",...MultiSelect.test.js
Source:MultiSelect.test.js  
...7  const firstselect = page.locator('[name="test1"]')8  const secondselect = page.locator('[name="test2"]')9  const appleInSecond = page.locator('text="Apple"')10  const bananaInFirst = page.locator('text="banana"')11  await expect(appleInSecond).toHaveCount(0)12  await expect(bananaInFirst).toHaveCount(0)13  firstselect.focus()14  await expect(bananaInFirst).toHaveCount(1)15  secondselect.focus()16  await expect(bananaInFirst).toHaveCount(0)17  await expect(appleInSecond).toHaveCount(1)18  secondselect.press('Escape')19  await expect(appleInSecond).toHaveCount(0)20})21test('able to add and remove items', async ({ page }) => {22  const firstselect = page.locator('[name="test1"]')23  const secondselect = page.locator('[name="test2"]')24  const bananaInFirst = page.locator('text="banana"')25  const orangeInFirst = page.locator('text="orange"')26  const appleInSecond = page.locator('text="Apple"')27  const bananaInSecond = page.locator('text="Banana"')28  const orangeInSecond = page.locator('text="Orange"')29  await expect(bananaInFirst).toHaveCount(0)30  firstselect.focus()31  await expect(bananaInFirst).toHaveCount(1)32  bananaInFirst.click()33  await expect(orangeInFirst).toHaveCount(0)34  await expect(bananaInFirst).toHaveCount(1)35  bananaInFirst.click()36  await expect(bananaInFirst).toHaveCount(0)37  await expect(appleInSecond).toHaveCount(0)38  await expect(bananaInSecond).toHaveCount(1)39  secondselect.press('Backspace')40  secondselect.press('Escape')41  await expect(bananaInSecond).toHaveCount(0)42  secondselect.press('ArrowLeft')43  await expect(orangeInSecond).toHaveCount(1)44  await expect(orangeInSecond).toHaveClass(/hilited/)45})46test('typing in the input reduces the options in the menu', async ({ page }) => {47  const firstselect = page.locator('[name="test1"]')48  const bananaInFirst = page.locator('text="banana"')49  const orangeInFirst = page.locator('text="orange"')50  firstselect.type('ora')51  await expect(bananaInFirst).toHaveCount(0)52  await expect(orangeInFirst).toHaveCount(1)...PopupMenu.test.js
Source:PopupMenu.test.js  
...5})6test('menu toggles when button is clicked', async ({ page }) => {7  const button = page.locator('#test-button')8  const monday = page.locator('text="Monday"')9  await expect(button).toHaveCount(1)10  await expect(monday).toHaveCount(0)11  button.click()12  await expect(monday).toHaveCount(1)13  button.click()14  await expect(monday).toHaveCount(0)15  button.press('Enter')16  await expect(monday).toHaveCount(1)17  button.press('Enter')18  await expect(monday).toHaveCount(0)19})20test('has five menu items', async ({ page }) => {21  const button = page.locator('#test-button')22  await expect(button).toHaveCount(1)23  button.click()24  await expect(page.locator('text="Monday"')).toHaveCount(1)25  await expect(page.locator('text="Tuesday"')).toHaveCount(1)26  await expect(page.locator('text="Wednesday"')).toHaveCount(1)27  await expect(page.locator('text="Thursday"')).toHaveCount(1)28  await expect(page.locator('text="Friday"')).toHaveCount(1)29})30test('clicking a menu option selects it', async ({ page }) => {31  const button = page.locator('#test-button')32  await expect(button).toHaveCount(1)33  button.click()34  const option = page.locator('text="Monday"')35  await expect(option).toHaveCount(1)36  await expect(page.locator('text=selected: 1')).toHaveCount(0)37  option.click()38  await expect(page.locator('text=selected: 1')).toHaveCount(1)39})40test('may select a menu option with the keyboard', async ({ page }) => {41  const button = page.locator('#test-button')42  await expect(button).toHaveCount(1)43  button.click()44  const option = page.locator('text="Monday"')45  await expect(option).toHaveCount(1)46  await expect(page.locator('text=selected: 1')).toHaveCount(0)47  await button.press('ArrowDown')48  await button.press('Enter')49  await expect(page.locator('text=selected: 1')).toHaveCount(1)...add_elements.spec.js
Source:add_elements.spec.js  
...5  await page.locator('text=Add/Remove Elements').click();6  await expect(page).toHaveURL('https://the-internet.herokuapp.com/add_remove_elements/');7  await page.locator('text=Add Element').click();8  await expect(page.locator('text=Delete').first()).toBeVisible();9  await expect(page.locator('text=Delete')).toHaveCount(1);10  await page.locator('text=Add Element').click();11  await expect(page.locator('text=Delete').nth(1)).toBeVisible();12  await expect(page.locator('text=Delete')).toHaveCount(2);13  await page.locator('text=Add Element').click();14  await expect(page.locator('text=Delete').nth(2)).toBeVisible();15  await expect(page.locator('text=Delete')).toHaveCount(3);16  await page.locator('text=Add Element').click();17  await expect(page.locator('text=Delete').nth(3)).toBeVisible();18  await expect(page.locator('text=Delete')).toHaveCount(4);19  await page.locator('text=Delete').nth(3).click();20  await expect(page.locator('text=Delete').nth(3)).toBeHidden();21  await expect(page.locator('text=Delete')).toHaveCount(3);22  await page.locator('text=Delete').first().click();23  await expect(page.locator('text=Delete').nth(2)).toBeHidden();24  await expect(page.locator('text=Delete')).toHaveCount(2);...CollapsingTable.test.js
Source:CollapsingTable.test.js  
...3test.beforeEach(async ({ page }) => {4  await goto(page, '/collapsingtable')5})6test('has two rows in tbody', async ({ page }) => {7  await expect(page.locator('text=John Smith')).toHaveCount(1)8  await expect(page.locator('text=Mary Sue')).toHaveCount(1)9})10test('hides the numerated columns on a small screen', async ({ page }) => {11  await page.setViewportSize({ width: 400, height: 1024 })12  await expect(page.locator('text="one"')).toHaveCount(0)13})14test('menu toggles when button is clicked', async ({ page }) => {15  await page.setViewportSize({ width: 400, height: 1024 })16  const button = page.locator('th').last()17  const menuitemtwo = page.locator('text="two"')18  await expect(menuitemtwo).toHaveCount(0)19  button.click()20  await expect(menuitemtwo).toHaveCount(1)21  button.click()22  await expect(menuitemtwo).toHaveCount(0)23})24test('able to activate a new row', async ({ page }) => {25  await page.setViewportSize({ width: 400, height: 1024 })26  const button = page.locator('th').last()27  const menuitemtwo = page.locator('text="two"')28  await expect(menuitemtwo).toHaveCount(0)29  button.click()30  await expect(menuitemtwo).toHaveCount(1)31  menuitemtwo.click()32  await expect(page.locator('text="2"')).toHaveCount(3)...send.js
Source:send.js  
...9  async test() {10    await openChat(this.page);11    const message = this.page.locator(e.chatUserMessageText);12    // 0 messages13    await expect(message).toHaveCount(0);14    // send a message15    await this.type(e.chatBox, e.message);16    await this.waitAndClick(e.sendButton);17    await this.page.waitForSelector(e.chatUserMessageText);18    // 1 message19    await expect(message).toHaveCount(1);20  }21}...Using AI Code Generation
1const { test, expect } = require('@playwright/test');2test('test', async ({ page }) => {3  const elements = await page.$$('.navbar__inner .navbar__items li');4  expect(elements).toHaveCount(6);5});6{7  "scripts": {8  },9  "devDependencies": {10  }11}12const { test, expect } = require('@playwright/test');13test('test', async ({ page }) => {14  const elements = await page.$$('.navbar__inner .navbar__items li');15  expect(elements).toHaveCount(6);16});17{18  "scripts": {19  },20  "devDependencies": {21  }22}23const { test, expect } = require('@playwright/test');24test('test', async ({ page }) => {25  const elements = await page.$$('.navbar__inner .navbar__items li');26  expect(elements).toHaveCount(6);27});28{29  "scripts": {30  },31  "devDependencies": {32  }33}Using AI Code Generation
1const { test, expect } = require('@playwright/test');2test('test', async ({ page }) => {3  const searchButton = page.locator('#search');4  expect(searchButton).toHaveCount(1);5});6const { test, expect } = require('@playwright/test');7test('test', async ({ page }) => {8  const searchButton = page.locator('#search');9  expect(searchButton).toHaveText('Search');10});11const { test, expect } = require('@playwright/test');12test('test', async ({ page }) => {13  const searchButton = page.locator('#search');14  expect(searchButton).toBeVisible();15});16const { test, expect } = require('@playwright/test');17test('test', async ({ page }) => {Using AI Code Generation
1const { test, expect } = require('@playwright/test');2test('should have 2 elements', async ({ page }) => {3  const elements = await page.$$('text=Get started');4  expect(elements).toHaveLength(2);5});6const { test, expect } = require('@playwright/test');7test('should have 2 elements', async ({ page }) => {8  const elements = await page.$$('text=Get started');9  expect(elements).to.have.lengthOf(2);10});11const { test, expect } = require('@playwright/test');12test('should have 2 elements', async ({ page }) => {13  const elements = await page.$$('text=Get started');14  expect(elements).toHaveLength(2);15});16const { test, expect } = require('@playwright/test');17test('should have 2 elements', async ({ page }) => {18  const elements = await page.$$('text=Get started');19  expect(elements.length).toBe(2);20});21const { test, expect } = require('@playwright/test');22test('should have 2 elements', async ({ page }) => {23  const elements = await page.$$('text=Get started');24  expect(elements.length).to.be.equal(2);25});26const { test, expect } = require('@playwright/test');27test('should have 2 elements', async ({ page }) => {28  const elements = await page.$$('text=Get started');29  expect(elements.length).to.be.equal(2);30});31const { test, expect } = require('@playwright/test');32test('should have 2 elements', async ({Using AI Code Generation
1const { test, expect } = require('@playwright/test');2test('example test', async ({ page }) => {3  const selectors = await page.$$('css=div');4  expect(selectors).toHaveCount(1);5});6PASS test.js (6s)7  ✓ example test (6s)8    1 test passed (6s)9The method toHaveCount() is used to check the number of elements in the selector. The number of elements in the selector is compared with the expected count. If the number of elements in the selector is equalUsing AI Code Generation
1const { test, expect } = require('@playwright/test');2test('Playwright Internal API toHaveCount method', async ({ page }) => {3    await page.waitForSelector('input[name="q"]');4    const searchBox = await page.$('input[name="q"]');5    await searchBox.type('Playwright');6    await page.click('input[type="submit"]');7    await page.waitForSelector('div#search');8    const searchResults = await page.$$('div.g');9    expect(searchResults).toHaveCount(10);10});11const { test, expect } = require('@playwright/test');12test('Playwright Internal API toHaveCount method', async ({ page }) => {13    await page.waitForSelector('input[name="q"]');14    const searchBox = await page.$('input[name="q"]');15    await searchBox.type('Playwright');16    await page.click('input[type="submit"]');17    await page.waitForSelector('div#search');18    const searchResults = await page.$$('div.g');19    expect(searchResults).toHaveCount(10);20    const firstResult = await searchResults[0].textContent();21    expect(firstResult).toContain('Playwright');22});23const { test, expect } = require('@playwright/test');24test('Playwright Internal API toHaveCount method', async ({ page }) => {25    await page.waitForSelector('input[name="q"]');26    const searchBox = await page.$('input[name="q"]');Using AI Code Generation
1const { test, expect } = require('@playwright/test');2test('Test code to use toHaveCount method of Playwright Internal API', async ({ page }) => {3    const elements = await page.$$('text=Playwright');4    const elementCount = await elements.length;5    expect(elementCount).toHaveCount(1);6});LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
