Have a question?
Message sent Close
View Categories

Playwright Built-in Locators

📄
GetByRole.spec.ts
import { test, expect } from '@playwright/test'

test('Get By Role Locator', async ({ page }) => {

  // Launch application
  await page.goto('https://vinothqaacademy.com/demo-site/')

  // Text Box 
  await page.getByRole('textbox', { name: 'First Name *' }).fill('Vinoth')

  // Radio button 
  await page.getByRole('radio', { name: 'Female' }).check();

  // Checkbox 
  await page.getByRole('checkbox', { name: 'Selenium WebDriver' }).check()
  await page.getByRole('checkbox', { name: 'DevOps' }).uncheck()

  // Button 
  await page.getByRole('button', { name: 'Submit' }).click()

  // Link
  await page.getByRole('link', { name: 'Tutorials' }).click()
  
})

📄
GetByText.spec.ts
test('Get By Text Locator', async ({ page }) => {

  // Launch application
  await page.goto('https://vinothqaacademy.com/demo-page-healthcare/')

  // Heading validation
  const formTitle = page.getByText('Patient Details Form')
  await expect(formTitle).toHaveText('Patient Details Form')
  console.log(await formTitle.textContent())

  // Link
  await page.getByText('Payment Link').click()

  // Partial Link
  await page.getByText('send for MRI').click()
})
📄
GetByLabel.spec.ts
test('Get By Label Locator', async ({ page }) => {

  // Launch application
  await page.goto('https://vinothqaacademy.com/demo-site/');

  // Enter your query
  await page.getByLabel('Enter your query ').fill('Is playwright easy to learn?');
});
📄
GetByPlaceHolder.spec.ts
test('Get By Place Holder Locator', async ({ page }) => {

  // Launch application
  await page.goto('https://vinothqaacademy.com/demo-site-keyboard-events/');

  // Search Box
  await page.getByPlaceholder('Type to search').fill('JAVA');
});
📄
GetByText.spec.ts
test('Get By Alt Text Locator', async ({ page }) => {

  // Launch application
  await page.goto('https://vinothqaacademy.com/');

  // Logo Image
  const logoImage = page.getByAltText('Vinoth Tech Solutions');
  await expect(logoImage).toHaveAttribute('alt', 'Vinoth Tech Solutions');
});
📄
GetByTitle.spec.ts
test('Get By Title Locator', async ({ page }) => {

  // Launch application
  await page.goto('https://vinothqaacademy.com/mouse-event/');

  // Enter First Name text field located by its title attribute
  const firstName = page.getByTitle('Enter First Name');
  await expect(firstName).toHaveAttribute('title', 'Enter First Name');
});
📄
GetByTestId.spec.ts
test('Get By Test ID Locator', async ({ page }) => {

    // Launch application
  await page.goto('https://vinothqaacademy.com/demo-site-create-account/')

  // Form - data-testid  
  await page.getByTestId('input-firstName').fill('Vinoth')
  await page.getByTestId('input-lastName').fill('R')
  await page.getByTestId('input-address').fill('XYZ')
  await page.getByTestId('input-city').fill('Chennai')
  await page.getByTestId('input-state').fill('Tamil Nadu')
  await page.getByTestId('input-zip').fill('600000')
  await page.getByTestId('input-phone').fill('6383544892')
  await page.getByTestId('input-email').fill('vinothtechsolutions@gmail.com')
  await page.getByTestId('btn-register').click()

})
// using name locator
use: {
    testIdAttribute:'name'
  },