Bird
Raised Fist0
Selenium Javatesting~20 mins

Drag and drop in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Drag and Drop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the result of this drag and drop Selenium code?
Consider the following Selenium Java code snippet that performs a drag and drop action. What will be the output if the drag and drop is successful?
Selenium Java
WebElement source = driver.findElement(By.id("drag-source"));
WebElement target = driver.findElement(By.id("drop-target"));
Actions actions = new Actions(driver);
actions.dragAndDrop(source, target).perform();
System.out.println(target.getText());
AThe text of the target element after the drag and drop, reflecting the dropped item
BThe text of the source element before drag and drop
CAn exception is thrown because dragAndDrop requires clickAndHold
DThe text of the target element remains unchanged
Attempts:
2 left
💡 Hint
Think about what happens to the target element's text after a successful drag and drop.
❓ locator
intermediate
2:00remaining
Which locator is best for identifying draggable and droppable elements?
You want to automate drag and drop in Selenium Java. Which locator strategy is best practice for identifying the source and target elements?
ABy.id("drag-source") and By.id("drop-target")
BBy.xpath("//div[@class='draggable']") and By.xpath("//div[@class='droppable']")
CBy.className("drag") and By.className("drop")
DBy.cssSelector("div:nth-child(1)") and By.cssSelector("div:nth-child(2)")
Attempts:
2 left
💡 Hint
IDs are unique and fastest to locate.
❓ assertion
advanced
2:00remaining
Which assertion correctly verifies a successful drag and drop?
After performing drag and drop, you want to assert that the target element contains the text "Dropped!". Which assertion is correct in JUnit?
AassertNull(target.getText());
BassertTrue(target.getText().contains("Dropped!"));
CassertFalse(target.getText().equals("Dropped!"));
DassertEquals("Dropped!", target.getText());
Attempts:
2 left
💡 Hint
You want to check exact text equality.
🔧 Debug
advanced
2:00remaining
Why does this drag and drop code fail with ElementNotInteractableException?
Given this code snippet, the drag and drop fails with ElementNotInteractableException. What is the most likely cause?
Selenium Java
WebElement source = driver.findElement(By.id("drag-source"));
WebElement target = driver.findElement(By.id("drop-target"));
Actions actions = new Actions(driver);
actions.clickAndHold(source).moveToElement(target).release().perform();
AThe target element locator is incorrect causing NoSuchElementException
BThe source element is not visible or not enabled for interaction
CThe Actions class does not support clickAndHold method
DThe perform() method is missing at the end
Attempts:
2 left
💡 Hint
ElementNotInteractableException means element cannot be interacted with currently.
❓ framework
expert
3:00remaining
In a Selenium Java test framework, where should drag and drop logic be placed for best design?
You are designing a Selenium Java test framework. Where is the best place to put the drag and drop code for reusability and maintainability?
ADirectly inside the test method where drag and drop is needed
BInside a utility/helper class with static methods for drag and drop
CInside the Page Object class representing the page containing draggable elements
DIn the main method of the test suite class
Attempts:
2 left
💡 Hint
Think about the Page Object Model design pattern.

Practice

(1/5)
1. What is the primary purpose of the dragAndDrop method in Selenium's Actions class?
easy
A. To click on an element without moving it
B. To simulate dragging an element and dropping it onto another element
C. To scroll the page to a specific element
D. To take a screenshot of the dragged element

Solution

  1. Step 1: Understand the dragAndDrop method

    The dragAndDrop method is designed to simulate the user action of clicking and holding an element, moving it to another element, and releasing it.
  2. Step 2: Compare with other actions

    Options A, C, and D describe different actions unrelated to drag and drop functionality.
  3. Final Answer:

    To simulate dragging an element and dropping it onto another element -> Option B
  4. Quick Check:

    dragAndDrop = simulate drag and drop [OK]
Hint: Remember dragAndDrop moves element from source to target [OK]
Common Mistakes:
  • Confusing dragAndDrop with click actions
  • Thinking dragAndDrop scrolls the page
  • Assuming dragAndDrop takes screenshots
2. Which of the following is the correct syntax to perform a drag and drop using Selenium Actions in Java?
easy
A. actions.dragAndDrop(source, target).perform();
B. actions.dragDrop(source, target).perform();
C. actions.dragAndDrop(source).to(target);
D. actions.drag(source).drop(target).execute();

Solution

  1. Step 1: Recall the correct method name and chaining

    The correct method is dragAndDrop(source, target) followed by perform() to execute the action.
  2. Step 2: Identify incorrect method names and chaining

    Options A, B, and C use incorrect method names or chaining syntax that do not exist in Selenium's Actions class.
  3. Final Answer:

    actions.dragAndDrop(source, target).perform(); -> Option A
  4. Quick Check:

    Correct method name + perform() = actions.dragAndDrop(source, target).perform(); [OK]
Hint: Use dragAndDrop(source, target).perform() exactly [OK]
Common Mistakes:
  • Using wrong method names like dragDrop
  • Missing perform() at the end
  • Incorrect chaining of methods
3. Given the following code snippet, what will be the result after execution?
WebElement source = driver.findElement(By.id("item1"));
WebElement target = driver.findElement(By.id("box1"));
Actions actions = new Actions(driver);
actions.clickAndHold(source).moveToElement(target).release().perform();
String text = target.getText();

If the drag and drop is successful, what should text contain?
medium
A. Null value
B. An empty string
C. The text of the dragged element (item1)
D. The text of the target element before drag

Solution

  1. Step 1: Understand the drag and drop effect

    Dragging source onto target usually moves or copies the source element's content into the target area.
  2. Step 2: Check what target.getText() returns after drop

    After a successful drop, the target element's text usually remains unchanged unless the page's DOM is updated by JavaScript. The getText() method returns the visible text of the target element, which typically does not change automatically after drag and drop.
  3. Final Answer:

    The text of the target element before drag -> Option D
  4. Quick Check:

    target.getText() usually unchanged after drag and drop unless DOM updates [OK]
Hint: After drop, target text usually remains the same unless DOM updates [OK]
Common Mistakes:
  • Assuming target text changes automatically
  • Expecting empty or null text after drop
  • Confusing source and target texts
4. Identify the error in the following Selenium Java code for drag and drop:
WebElement source = driver.findElement(By.id("drag"));
WebElement target = driver.findElement(By.id("drop"));
Actions actions = new Actions(driver);
actions.clickAndHold(source).moveToElement(target).perform();
medium
A. Missing release() before perform()
B. Incorrect locator strategy for elements
C. Actions class cannot be instantiated directly
D. No need to call perform() after actions

Solution

  1. Step 1: Review the drag and drop sequence

    Drag and drop requires clicking and holding the source, moving to target, then releasing the mouse button before performing.
  2. Step 2: Identify missing release()

    The code misses release(), so the drop action is incomplete and will not work as expected.
  3. Final Answer:

    Missing release() before perform() -> Option A
  4. Quick Check:

    Drag and drop needs release() [OK]
Hint: Always call release() before perform() in drag and drop [OK]
Common Mistakes:
  • Forgetting release() after moveToElement
  • Incorrect element locators (not in this code)
  • Not calling perform() at all
5. You want to verify that a drag and drop action moved an item from a list to a cart on a webpage. Which approach is best to confirm the drop was successful in Selenium Java?
hard
A. Check the page title has changed after drag and drop
B. Verify the source element is still visible after drop
C. Check that the cart element contains the dragged item's text after drop
D. Confirm the browser URL changed after drag and drop

Solution

  1. Step 1: Understand what changes after drag and drop

    The dragged item should appear inside the cart element, so its text should be present there.
  2. Step 2: Evaluate verification methods

    Checking source visibility (B) might not confirm drop; page title (C) and URL (D) usually do not change after drag and drop.
  3. Final Answer:

    Check that the cart element contains the dragged item's text after drop -> Option C
  4. Quick Check:

    Verify drop by checking target content [OK]
Hint: Verify drop by checking target element's content [OK]
Common Mistakes:
  • Checking unrelated page properties like title or URL
  • Assuming source element visibility confirms drop
  • Not verifying target element content
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