Bird
Raised Fist0
Selenium Javatesting~20 mins

Context click (right click) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Context Click Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that performs a context click on a web element. What will be printed if the context click is successful and the alert text is "Right click detected"?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com/context-menu");
WebElement box = driver.findElement(By.id("box"));
Actions actions = new Actions(driver);
actions.contextClick(box).perform();
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
System.out.println(alertText);
alert.accept();
driver.quit();
ARight click detected
BNo alert present
CElementNotInteractableException
DNullPointerException
Attempts:
2 left
💡 Hint
Think about what happens after a context click triggers an alert.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies the context click triggered the expected alert?
You want to verify that after performing a context click on an element, an alert with text "Context menu opened" appears. Which assertion is correct in Java using TestNG?
Selenium Java
WebElement element = driver.findElement(By.cssSelector(".menu"));
Actions actions = new Actions(driver);
actions.contextClick(element).perform();
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
AAssert.assertEquals(alertText, "Context menu opened");
BAssert.assertTrue(alertText.contains("Context menu"));
CAssert.assertNull(alertText);
DAssert.assertFalse(alertText.isEmpty());
Attempts:
2 left
💡 Hint
You want to check exact match of alert text.
🔧 Debug
advanced
2:00remaining
Why does this context click code throw ElementNotInteractableException?
Given this code snippet, why does it throw ElementNotInteractableException when performing context click?
Selenium Java
WebElement hiddenElement = driver.findElement(By.id("hidden"));
Actions actions = new Actions(driver);
actions.contextClick(hiddenElement).perform();
AThe driver is not initialized properly
BThe Actions class does not support contextClick on any element
CThe element is not visible or not enabled for interaction
DcontextClick requires double click before
Attempts:
2 left
💡 Hint
Think about element visibility and interactability.
❓ framework
advanced
2:00remaining
Which Selenium Java code snippet correctly performs a context click and verifies the context menu is displayed?
Select the code snippet that performs a right click on an element with id "menu" and asserts that the context menu with id "context-menu" is visible afterwards.
A
WebElement menu = driver.findElement(By.id("menu"));
Actions actions = new Actions(driver);
actions.click(menu).perform();
WebElement contextMenu = driver.findElement(By.id("context-menu"));
Assert.assertTrue(contextMenu.isDisplayed());
B
WebElement menu = driver.findElement(By.id("menu"));
Actions actions = new Actions(driver);
actions.contextClick(menu).build();
WebElement contextMenu = driver.findElement(By.id("context-menu"));
Assert.assertTrue(contextMenu.isDisplayed());
C
WebElement menu = driver.findElement(By.id("menu"));
Actions actions = new Actions(driver);
actions.doubleClick(menu).perform();
WebElement contextMenu = driver.findElement(By.id("context-menu"));
Assert.assertTrue(contextMenu.isDisplayed());
D
WebElement menu = driver.findElement(By.id("menu"));
Actions actions = new Actions(driver);
actions.contextClick(menu).perform();
WebElement contextMenu = driver.findElement(By.id("context-menu"));
Assert.assertTrue(contextMenu.isDisplayed());
Attempts:
2 left
💡 Hint
Remember to call perform() to execute the action.
🧠 Conceptual
expert
2:00remaining
What is the main reason to use context click in Selenium tests?
Why would a tester use context click (right click) in Selenium automated tests?
ATo submit a form without clicking a button
BTo simulate user interaction with context menus or right-click options in the web application
CTo switch between browser tabs
DTo scroll the page down automatically
Attempts:
2 left
💡 Hint
Think about what right click does in normal user behavior.

Practice

(1/5)
1. What does the contextClick() method in Selenium WebDriver do?
easy
A. Simulates typing text into a web element
B. Simulates a right-click on a web element
C. Simulates a drag and drop action
D. Simulates a double-click on a web element

Solution

  1. Step 1: Understand the purpose of contextClick()

    The contextClick() method is designed to simulate a right-click action on a web element, which is different from a normal click.
  2. Step 2: Compare with other mouse actions

    Double-click, drag and drop, and typing are different actions and have their own methods like doubleClick(), dragAndDrop(), and sendKeys().
  3. Final Answer:

    Simulates a right-click on a web element -> Option B
  4. Quick Check:

    contextClick() = right-click [OK]
Hint: Right-click action in Selenium uses contextClick() [OK]
Common Mistakes:
  • Confusing contextClick() with doubleClick()
  • Using click() instead of contextClick() for right-click
  • Trying to send keys for right-click action
2. Which of the following is the correct syntax to perform a right-click on a web element using Selenium Java?
easy
A. Actions.contextClick(element).perform();
B. driver.contextClick(element).perform();
C. new Actions(driver).contextClick(element).perform();
D. driver.actions().contextClick(element).perform();

Solution

  1. Step 1: Identify the correct Actions class usage

    In Selenium Java, Actions is instantiated with the WebDriver instance, e.g., new Actions(driver).
  2. Step 2: Use contextClick() on the Actions object and call perform()

    The method chain is new Actions(driver).contextClick(element).perform(); to execute the right-click.
  3. Final Answer:

    new Actions(driver).contextClick(element).perform(); -> Option C
  4. Quick Check:

    Actions object + contextClick + perform() = correct syntax [OK]
Hint: Always create Actions object with driver before contextClick() [OK]
Common Mistakes:
  • Calling contextClick() directly on driver
  • Using static call on Actions without new keyword
  • Missing perform() at the end
3. What will happen when the following Selenium Java code is executed?
WebElement element = driver.findElement(By.id("menu"));
new Actions(driver).contextClick(element).perform();
medium
A. A left-click is performed on the element with id 'menu'
B. Nothing happens because perform() is missing
C. An exception is thrown because contextClick() requires coordinates
D. A right-click is performed on the element with id 'menu'

Solution

  1. Step 1: Locate the element by id 'menu'

    The code finds the web element with id="menu" correctly using findElement(By.id("menu")).
  2. Step 2: Perform contextClick() on the found element

    The contextClick(element).perform() chain performs a right-click on that element.
  3. Final Answer:

    A right-click is performed on the element with id 'menu' -> Option D
  4. Quick Check:

    contextClick(element).perform() = right-click on element [OK]
Hint: contextClick(element).perform() triggers right-click on that element [OK]
Common Mistakes:
  • Thinking contextClick needs coordinates always
  • Confusing left-click with right-click
  • Forgetting to call perform()
4. Identify the error in the following Selenium Java code snippet that tries to right-click on a button:
WebElement button = driver.findElement(By.name("submit"));
Actions action = new Actions(driver);
action.contextClick().perform();
medium
A. contextClick() is called without specifying the element
B. Actions object is not created properly
C. perform() is missing after contextClick()
D. findElement locator is incorrect

Solution

  1. Step 1: Check contextClick() usage

    The method contextClick() requires either no argument (to right-click at current mouse location) or a WebElement argument to specify where to right-click. Here, no argument is passed but no mouse position is set.
  2. Step 2: Confirm if element is specified

    The code finds the button element but does not pass it to contextClick(), so it will not right-click on the button.
  3. Final Answer:

    contextClick() is called without specifying the element -> Option A
  4. Quick Check:

    contextClick() needs element or mouse position [OK]
Hint: Always pass element to contextClick() to right-click on it [OK]
Common Mistakes:
  • Calling contextClick() without element or mouse position
  • Forgetting perform() after contextClick()
  • Assuming findElement locator is wrong without checking
5. You want to test a custom context menu that appears on right-clicking a list item. Which Selenium Java code snippet correctly performs a right-click on the first list item and then clicks the 'Delete' option from the context menu?
WebElement listItem = driver.findElement(By.cssSelector("ul#items li:first-child"));
WebElement deleteOption = driver.findElement(By.id("delete"));
// What next?
hard
A. new Actions(driver).contextClick(listItem).perform(); deleteOption.click();
B. new Actions(driver).click(listItem).contextClick(deleteOption).perform();
C. new Actions(driver).contextClick(listItem).click(deleteOption).perform();
D. new Actions(driver).contextClick(deleteOption).click(listItem).perform();

Solution

  1. Step 1: Right-click on the first list item

    Use contextClick(listItem).perform() to open the custom context menu on the list item.
  2. Step 2: Click the 'Delete' option separately

    After the context menu appears, click the 'Delete' option using deleteOption.click(); as it is a normal click on a visible element.
  3. Final Answer:

    new Actions(driver).contextClick(listItem).perform(); deleteOption.click(); -> Option A
  4. Quick Check:

    Right-click then normal click separately for context menu actions [OK]
Hint: Right-click then click menu option separately for context menus [OK]
Common Mistakes:
  • Chaining click on menu option inside Actions after contextClick
  • Clicking wrong element first
  • Not calling perform() after contextClick
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard