Beginner 10 min 8 scenarios

Button Automation Practice

Master button interactions in Selenium & Playwright — click, double-click, right-click, disabled state, click-and-hold, and browser navigation.

Scenario 1: Navigate to Home Page

Scenario 2: Get Button X & Y Coordinates

Scenario 3: Get Button Color

Scenario 4: Get Button Height & Width

Scenario 5: Disabled ButtonDISABLED

Scenario 6: Click and Hold for 1.5 sec

Scenario 7: Double Click Button

Scenario 8: Right Click Button

No action performed yet.

What You'll Learn

Selenium (Java)

  • click()
  • doubleClick()
  • contextClick()
  • isEnabled()
  • getLocation()
Tutorial video coming soon

Test Cases

Introduction

Buttons are the most commonly interacted element in web automation. Engineers must handle:

  1. Single click — basic click() action
  2. Double click — fires a different browser event
  3. Right click — opens the context menu
  4. Disabled button — assert it cannot be interacted with

Selenium uses the Actions class for advanced mouse interactions. Playwright has click options built in.

Key Methods Summary

Action Selenium (Java) Playwright (JS) Playwright (Python)
Single click element.click() locator.click() locator.click()
Double click actions.doubleClick(el).perform() locator.dblclick() locator.dblclick()
Right click actions.contextClick(el).perform() locator.click({ button: "right" }) locator.click(button="right")
Is disabled !isEnabled() toBeDisabled() to_be_disabled()
Get text getText() textContent() text_content()

1. Single Click

Selenium (Java)

driver.findElement(By.id("clickBtn")).click();
assertTrue(driver.findElement(By.id("clickResult")).isDisplayed());

Playwright (JS)

await page.locator("#clickBtn").click();
await expect(page.locator("#clickResult")).toBeVisible();

Playwright (Python)

page.locator("#clickBtn").click()
expect(page.locator("#clickResult")).to_be_visible()

2. Double Click

Selenium (Java)

WebElement btn = driver.findElement(By.id("doubleClickBtn"));
new Actions(driver).doubleClick(btn).perform();
 
assertEquals("Double clicked!", driver.findElement(By.id("doubleClickResult")).getText());

Playwright (JS)

await page.locator("#doubleClickBtn").dblclick();
await expect(page.locator("#doubleClickResult")).toHaveText("Double clicked!");

Playwright (Python)

page.locator("#doubleClickBtn").dblclick()
expect(page.locator("#doubleClickResult")).to_have_text("Double clicked!")

3. Right Click (Context Menu)

Selenium (Java)

WebElement btn = driver.findElement(By.id("rightClickBtn"));
new Actions(driver).contextClick(btn).perform();
 
assertTrue(driver.findElement(By.id("contextMenu")).isDisplayed());

Playwright (JS)

await page.locator("#rightClickBtn").click({ button: "right" });
await expect(page.locator("#contextMenu")).toBeVisible();

Playwright (Python)

page.locator("#rightClickBtn").click(button="right")
expect(page.locator("#contextMenu")).to_be_visible()

4. Disabled Button — verify it cannot be clicked

Selenium (Java)

WebElement btn = driver.findElement(By.id("disabledBtn"));
assertFalse(btn.isEnabled());
assertNotNull(btn.getAttribute("disabled"));

Playwright (JS)

await expect(page.locator("#disabledBtn")).toBeDisabled();

Playwright (Python)

expect(page.locator("#disabledBtn")).to_be_disabled()

5. Verify button text changes after click

Selenium (Java)

WebElement btn = driver.findElement(By.id("toggleBtn"));
String before = btn.getText();
btn.click();
assertNotEquals(before, btn.getText());

Playwright (JS)

const btn = page.locator("#toggleBtn");
const before = await btn.textContent();
await btn.click();
expect(await btn.textContent()).not.toBe(before);

Playwright (Python)

btn = page.locator("#toggleBtn")
before = btn.text_content()
btn.click()
assert btn.text_content() != before

6. Click using keyboard Enter

Selenium (Java)

driver.findElement(By.id("submitBtn")).sendKeys(Keys.ENTER);

Playwright (JS)

await page.locator("#submitBtn").press("Enter");

Playwright (Python)

page.locator("#submitBtn").press("Enter")

📄 Also Read: Top 10 Best Automation Practice Website